# Usare gli helper integrati per string, number, date e array

> Total.js estende String, Number, Date e Array con helper per slug, hash, formattazione, parsing, raggruppamento e ricerca, e offre U.extend() e U.clone() per gli oggetti.

Framework: total5
Product: Total.js 5 (total5@0.0.19)
Language: it
Content-SHA256: 4c6131a44b0437538fe59d97696f34273f66007e465bdd7459b83746e5c22773
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: total5-installed@total5@0.0.19
Review: not-recorded

Total.js aggiunge un insieme di metodi ai tipi integrati di JavaScript. Sono disponibili in tutta l'applicazione, quindi vale la pena conoscerli prima di scrivere un helper tuo. Quelli qui sotto sono esercitati dall'esempio.

**String:** `slug([max])` crea un testo adatto agli URL (minuscolo, senza accenti né simboli); `format(a, b)` sostituisce `{0}`, `{1}`; `encode()` / `decode()` eseguono l'escape e il ripristino dell'HTML; `padLeft(length, char)` / `padRight()`; `parseInt2()` e `parseFloat2()` leggono un numero da un testo e restituiscono `0` se non ce n'è; `isEmail()`, `isURL()` e `isJSON()` validano; `parseJSON()` interpreta il JSON e restituisce `null` in caso di errore; `md5()`, `sha1()` e `sha256()` restituiscono hash esadecimali; `capitalize()` mette in maiuscolo la prima lettera di ogni parola.

**Number:** `format(decimals, thousands, decimal)`, ad esempio `(1234567.891).format(2)` dà `1 234 567.89`.

**Date:** `format('yyyy-MM-dd HH:mm')`, `add('1 day')` e `diff(date, 'days')`.

**Array:** `take(n)` e `skip(n)`, `first()` e `last()`, `trim()` (rifila i testi e rimuove i vuoti), `findItem(field, value)` e `findIndex(field, value)`, `remove(value)` (rimuove ogni corrispondenza e restituisce l'array), `group(field)` (restituisce `[{ name, items }]`) e `quicksort(field)`.

**Oggetti:** `U.extend(target, source)` unisce `source` in `target` e lo restituisce; `U.clone(object)` crea una copia profonda.

```js controllers/utilities.js
exports.install = function() {
	ROUTE('GET /utils/', utils);
};

function utils($) {

	var items = [{ type: 'a', n: 1 }, { type: 'b', n: 2 }, { type: 'a', n: 3 }];
	var original = { a: { b: 1 } };
	var copy = U.clone(original);
	copy.a.b = 2;

	$.json({
		slug: 'Hello Wörld & Co!'.slug(),
		slugmax: 'Hello brave new world'.slug(11),
		format: '{0} has {1} items'.format('Ann', 3),
		encode: '<a href="x">&</a>'.encode(),
		decode: '&lt;b&gt;'.decode(),
		pad: ['7'.padLeft(3, '0'), '7'.padRight(3, '0')],
		numbers: ['abc12x'.parseInt2(), '3,5kg'.parseFloat2(), 'none'.parseInt2()],
		is: ['a@b.cz'.isEmail(), 'nope'.isEmail(), 'https://totaljs.com'.isURL(), '{"a":1}'.isJSON(), 'abc'.isJSON()],
		json: ['{"a":1}'.parseJSON(), 'not json'.parseJSON()],
		md5: 'abc'.md5(),
		sha1: 'abc'.sha1(),
		capitalize: 'hello big world'.capitalize(),
		number: [(1234567.891).format(2), (1234567.891).format(2, ' ', ',')],
		date: [new Date(2026, 0, 5, 9, 7).format('yyyy-MM-dd HH:mm'), new Date(2026, 0, 31).add('1 day').format('yyyy-MM-dd'), new Date(2026, 0, 5).diff(new Date(2026, 0, 1), 'days')],
		take: [[1, 2, 3, 4, 5].take(2), [1, 2, 3, 4, 5].skip(3), [1, 2, 3].first(), [1, 2, 3].last()],
		trim: ['a', '', ' b '].trim(),
		find: [[{ id: 1 }, { id: 2 }].findItem('id', 2), [{ id: 1 }, { id: 2 }].findIndex('id', 2)],
		remove: [1, 2, 3, 2].remove(2),
		group: items.group('type'),
		sort: [{ n: 'b' }, { n: 'a' }].quicksort('n'),
		extend: U.extend({ a: 1 }, { b: 2 }),
		clone: [original.a.b, copy.a.b]
	});
}
```
