ForgeTotal.js 5
de

Total.js 5

Die kleinen globalen Helfer: MAIN, REPO, FUNC, NOW, CLONE, COPY, QUERIFY und PROMISIFY

Total.js 5 · total5@0.0.19 globalsutilitieshelpers Mit Total.js 5 ausgeführt

total5 legt ein paar globale Objekte für deinen Gebrauch an und ein paar Helfer, die eine Codezeile sparen. Nichts davon braucht eine Einrichtung.

  • MAIN, REPO und FUNC sind leere Objekte, die das Framework einmal anlegt: Halte Anwendungsdaten in MAIN, andere Daten in REPO und deine eigenen Funktionen in FUNC. TEMP ist ein Objekt für temporäre Daten, das das Framework von Zeit zu Zeit ersetzt, speichere darin also nichts, was du später brauchst.
  • NOW ist das aktuelle Datum als Date. Es wird etwa einmal pro Minute (und durch einige Aufrufe) aktualisiert, verwende also new Date(), wenn du die genaue Zeit brauchst.
  • EMPTYARRAY und EMPTYOBJECT sind eingefrorene leere Werte, die man zurückgeben kann, statt neue zu erzeugen: Sie lassen sich nicht ändern.
  • MODS und PLUGINS sind Objekte mit den geladenen Modulen und Plugins nach ID. F und Total sind dasselbe Objekt, das Framework, und print ist console.log. NOOP ist eine Funktion, die nichts tut.
  • REQUIRE(name) ist require, das zusätzlich Pakete aus dem node_modules der Anwendung auflöst und Namen wie node:path akzeptiert.
  • CLONE(value) macht eine tiefe Kopie: Verschachtelte Objekte, Arrays und Datumswerte werden kopiert. COPY(from, to) kopiert die Eigenschaften des ersten Objekts in das zweite und gibt das zweite zurück.
  • QUERIFY([url], object) baut aus einem Objekt einen Query-String: Leere Werte und null werden übersprungen, und ein Array wird zu einem kommagetrennten Wert. Mit einer URL gibt es url?... zurück, ohne ?....
  • PROMISIFY(fn, a, b, c) ruft fn(a, b, c, callback) auf und gibt ein Promise zurück, das mit der Antwort des Callbacks aufgelöst oder mit dessen Fehler abgelehnt wird.
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 });
}