I piccoli assistenti globali: MAIN, REPO, FUNC, NOW, CLONE, COPY, QUERIFY e PROMISIFY
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,REPOeFUNCsono oggetti vuoti che il framework crea una volta: tieni i dati dell'applicazione inMAIN, altri dati inREPOe le tue funzioni inFUNC.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 comeDate. Viene aggiornata circa una volta al minuto (e da alcune chiamate), quindi usanew Date()quando ti serve l'ora esatta.EMPTYARRAYeEMPTYOBJECTsono valori vuoti congelati da restituire invece di crearne di nuovi: non possono essere modificati.MODSePLUGINSsono oggetti con i moduli e i plugin caricati per id.FeTotalsono lo stesso oggetto, il framework, eprintèconsole.log.NOOPè una funzione che non fa nulla.REQUIRE(name)èrequire, che risolve anche i pacchetti dalnode_modulesdell'applicazione e accetta nomi comenode: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 enullvengono saltati e un array diventa un valore separato da virgole. Con un url restituisceurl?..., senza restituisce?....PROMISIFY(fn, a, b, c)chiamafn(a, b, c, callback)e restituisce una promise che si risolve con la risposta del callback o viene rifiutata con il suo errore.
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 });
}