The small global helpers: MAIN, REPO, FUNC, NOW, CLONE, COPY, QUERIFY and PROMISIFY
total5 creates a few global objects for your own use and a few helpers that save a line of code. Nothing here needs setup.
MAIN,REPOandFUNCare empty objects that the framework creates once: keep application data inMAIN, other data inREPOand your own functions inFUNC.TEMPis an object for temporary data that the framework replaces from time to time, so do not keep anything in it that you need later.NOWis the current date as aDate. It is refreshed about once a minute (and by some calls), so usenew Date()when you need the exact time.EMPTYARRAYandEMPTYOBJECTare frozen empty values to return instead of creating new ones: they cannot be changed.MODSandPLUGINSare objects with the loaded modules and plugins by id.FandTotalare the same object, the framework, andprintisconsole.log.NOOPis a function that does nothing.REQUIRE(name)isrequirethat also resolves packages from thenode_modulesof the application and accepts names such asnode:path.CLONE(value)makes a deep copy: nested objects, arrays and dates are copied.COPY(from, to)copies the properties of the first object into the second and returns the second.QUERIFY([url], object)builds a query string from an object: empty values andnullare skipped and an array becomes a comma separated value. With an url it returnsurl?..., without one it returns?....PROMISIFY(fn, a, b, c)callsfn(a, b, c, callback)and returns a promise that resolves with the response of the callback or rejects with its error.
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 });
}