# I piccoli assistenti globali: MAIN, REPO, FUNC, NOW, CLONE, COPY, QUERIFY e PROMISIFY

> Utilizzare globali di applicazione, date, copie profonde, righe di consultazione e aiutanti promesse.

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

`total5` crea alcuni oggetti globali per il tuo uso e alcuni helper che fanno risparmiare una riga di codice. Nulla di tutto ciò richiede configurazione.

- `MAIN`, `REPO` e `FUNC` sono oggetti vuoti che il framework crea una volta: tieni i dati dell'applicazione in `MAIN`, altri dati in `REPO` e le tue funzioni in `FUNC`. `TEMP` è un oggetto per dati temporanei che il framework sostituisce di tanto in tanto, quindi non conservarci nulla che ti serva dopo.
- `NOW` è la data corrente come `Date`. Viene aggiornata circa una volta al minuto (e da alcune chiamate), quindi usa `new Date()` quando ti serve l'ora esatta.
- `EMPTYARRAY` e `EMPTYOBJECT` sono valori vuoti congelati da restituire invece di crearne di nuovi: non possono essere modificati.
- `MODS` e `PLUGINS` sono oggetti con i moduli e i plugin caricati per id. `F` e `Total` sono lo stesso oggetto, il framework, e `print` è `console.log`. `NOOP` è una funzione che non fa nulla.
- `REQUIRE(name)` è `require`, che risolve anche i pacchetti dal `node_modules` dell'applicazione e accetta nomi come `node:path`.
- `CLONE(value)` fa una copia profonda: oggetti annidati, array e date vengono copiati. `COPY(from, to)` copia le proprietà del primo oggetto nel secondo e restituisce il secondo.
- `QUERIFY([url], object)` costruisce una query string da un oggetto: i valori vuoti e `null` vengono saltati e un array diventa un valore separato da virgole. Con un url restituisce `url?...`, senza restituisce `?...`.
- `PROMISIFY(fn, a, b, c)` chiama `fn(a, b, c, callback)` e restituisce una promise che si risolve con la risposta del callback o viene rifiutata con il suo errore.

```js controllers/helpers.js
MAIN.started = 'yes';
REPO.counter = 1;
FUNC.double = n => n * 2;

exports.install = function() {
	ROUTE('GET /helpers/', helpers);
	ROUTE('GET /promisify/', promisify);
};

function helpers($) {

	const source = { a: 1, nested: { b: [1, 2] }, date: new Date(2026, 0, 5) };
	const cloned = CLONE(source);
	const target = { x: 1 };
	const returned = COPY({ y: 2 }, target);

	$.json({
		shared: [MAIN.started, REPO.counter, FUNC.double(4), typeof TEMP],
		now: [NOW instanceof Date, Math.abs(Date.now() - NOW.getTime()) < 120000],
		emptyarray: [EMPTYARRAY.length, Object.isFrozen(EMPTYARRAY)],
		emptyobject: [Object.keys(EMPTYOBJECT).length, Object.isFrozen(EMPTYOBJECT)],
		registries: [typeof MODS, typeof PLUGINS],
		same: [F === Total, typeof print, NOOP() === undefined],
		require: typeof REQUIRE('node:path').join,
		clone: [cloned !== source, cloned.nested !== source.nested, cloned.nested.b !== source.nested.b, cloned.date instanceof Date, cloned.nested.b.join()],
		copy: [returned === target, target.x, target.y],
		querify: QUERIFY('/search/', { q: 'a b', page: 2, empty: '', nothing: null, list: [1, 2] }),
		querify2: QUERIFY({ q: 'x' })
	});
}

async function promisify($) {

	const add = (a, b, callback) => setImmediate(callback, null, a + b);
	const fail = (callback) => setImmediate(callback, 'It failed');

	const sum = await PROMISIFY(add, 2, 3);
	let error = null;

	try {
		await PROMISIFY(fail);
	} catch (e) {
		error = e.message || String(e);
	}

	$.json({ sum: sum, error: error });
}
```
