summaryrefslogtreecommitdiff
path: root/src/sjs.js
blob: 799f9649f1d5816ea1345850c33fb074f45d7b1b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
export const getIn = (obj, path) =>
	path.length === 0
		? obj
		: getIn(obj?.[path[0]], path.slice(1));

export const merge = (lhs, rhs) => {
	if (lhs === undefined) {
		return rhs;
	}

	if (rhs === undefined) {
		return lfs;
	}

	if (typeof lhs !== "object") {
		return rhs;
	}

	const lkeys = Object.keys(lhs);
	const rkeys = Object.keys(rhs);
	const allKeys = lkeys.concat(rkeys);
	return [...new Set(allKeys)].reduce(
		(o, key) => ({ ...o, [key]: merge(lhs[key], rhs[key]) }),
		{},
	);
};

export const compareObject = (lhs, rhs) => {
	const lstr = Object.entries(lhs).toSorted();
	const rstr = Object.entries(rhs).toSorted();

	if (lstr < rstr) {
		return -1;
	}

	if (lstr > rstr) {
		return 1;
	}

	throw new Error("unreachable");
};

const FROM = /[&<>'"]/g;

const ESCAPES = {
	'&': '&amp;',
	'<': '&lt;',
	'>': '&gt;',
	"'": '&#39;',
	'"': '&quot;'
};

const mappingFn = c => ESCAPES[c];

export const escape = s =>
	String.prototype.replace.call(s, FROM, mappingFn);

export const eq = (a, b) => {
	if (a === b) {
		return true;
	}

	if (a === null || b === null) {
		return false;
	}

	if (typeof a != "object" || typeof b != "object") {
		return false;
	}

	if (Array.isArray(a) !== Array.isArray(b)) {
		return false;
	}

	if (Object.keys(a).length !== Object.keys(b).length) {
		return false;
	}

	for (const k in a) {
		if (!b.hasOwnProperty(k)) {
			return false;
		}
		if (!eq(a[k], b[k])) {
			return false;
		}
	}
	return true;
};

export const keys = (ks, obj) =>
	ks.reduce(
		(ret, k) =>
			obj.hasOwnProperty(k) ?  {...ret, [k]: obj[k]} : ret,
		{},
	);

export const difference = (a, b) => {
	const diff = new Set(a);
	for (const el of b) {
		diff.delete(el);
	}
	return diff;
};

export const assocIn = (obj, path, value) =>
	path.length === 0 ?  obj :
		path.length === 1 ? { ...obj, [path[0]]: value } :
			{
				...obj,
				[path[0]]: assocIn(
					(obj[path[0]] || {}),
					path.slice(1),
					value
				)
			};

export const dissoc = (obj, key) => {
	const ret = { ...obj };
	delete ret[key]
	return ret;
};

export const findFirst = (arr, fn) => {
	for (const x of arr) {
		const ret = fn(x);
		if (ret) {
			return ret;
		}
	}

	return null;
};

export const partial = (fn, ...startArgs) =>
	(...endArgs) =>
		fn(...startArgs, ...endArgs);

export const strSortFn = (a, b) => a.localeCompare(b, "POSIX");

export const undefinedAsNull = x => x === undefined ? null : x;

export const first   = a => a[0];
export const rest    = a => a.slice(1);
export const butlast = a => a.slice(a, a.length - 1);
export const last    = a => a[a.length - 1];

export const take = function*(n, gen) {
	let i = 0n;
	for (const x of gen) {
		if (i >= n) {
			break;
		}
		i++;
		yield x;
	}
};

export const range = function*(x, y, step = 1n) {
	if (x === undefined) {
		let i = 0n;
		while (true) {
			yield i++;
		}
	}
	const [from, to] = y === undefined ?
		[0n, x] :
		[x, y];
	const fromn = BigInt(from);
	const ton   = BigInt(to);
	const stepn = BigInt(step);
	if (stepn === 0n) {
		while (true) {
			yield fromn;
		}
	}
	if (step < 0n) {
		for (let i = fromn; i > ton; i+= stepn) {
			yield i;
		}
	} else {
		for (let i = fromn; i < ton; i += stepn) {
			yield i;
		}
	}
};

const t = ({ colors, err, assertEq }) => ({
	assertEq,
	tap: x => {
		err(`tap: ${x}\n`);
		return x;
	},
	start: msg => {
		err(`${msg}:\n`);
	},
	testing: async (msg, fn) => {
		err(`${colors.yellow("testing")}: ${msg}... `);
		try {
			await fn();
		} catch (e) {
			err(`${colors.red("ERR")}.\n`);
			throw e;
		}
		err(`${colors.green("OK")}.\n`);
	},
});

class UnimplementedError extends Error {}

export const getJSImpl = () => {
	if (typeof scriptArgs !== "undefined") {
		return "qjs";
	}
	if (typeof Deno !== "undefined") {
		return "deno";
	}
	if (typeof process !== "undefined") {
		return "node";
	}
	// FIXME: add browser and bun
	return "unknown";
};

export const JSImpl = getJSImpl();

const mappings = {
	ARGV: {
		qjs: () => scriptArgs,
		node: () => process.argv.slice(1),
		deno: () => Deno.mainModule.substring("file://".length),
		unknown: () => {
			throw new UnimplementedError(`ARGV["${JSImpl}"]`);
		},
	},
	exit: {
		qjs: async () => {
			const { exit } = await import("std");
			return exit;
		},
		node: async () => process.exit,
		deno: async () => Deno.exit,
		unknown: () => {
			throw new UnimplementedError(`exit["${JSImpl}"]`);
		},
	},
	tconf: {
		qjs:  async () => await mappings.testConf.unknown(),
		deno: async () => await mappings.testConf.node(),
		browser: async () => {
			class DOMAssertionError extends Error {}

			const w = s => document.querySelector("#test-report").innerHTML += s;

			const red    = s => `<span style="color: red;          ">${s}</span>`;
			const green  = s => `<span style="color: green;        ">${s}</span>`;
			const yellow = s => `<span style="color: darkgoldenrod;">${s}</span>`;
			return {
				testStart: msg => w(`${msg}:\n`),
				testing: msg => w(`${yellow("testing")}: ${msg}...`),
				testOK: () => w(` ${green("OK")}.\n`),
				assert: (x, msg = "") => {
					if (!x) {
						w(` ${red("ERROR")}.\n`);
						w("\nSee console for details.\n");
						w("If possible, do report them.");
						throw new DOMAssertionError(msg);
					}
				},
			};
		},

		node: async () => {
			const assert  = await import("node:assert/strict");
			const process = await import("node:process");

			const err = x => process.stderr.write(x);
			const red    = s => `\x1b[31m${s}\x1b[0m`;
			const green  = s => `\x1b[32m${s}\x1b[0m`;
			const yellow = s => `\x1b[33m${s}\x1b[0m`;

			return {
				err,
				assertEq: assert.deepEqual,
				colors: {
					red,
					green,
					yellow,
				},
			};
		},
		unknown: () => { throw new UnimplementedError(`exit["${JSImpl}"]`); },
	},
};

export const exit  = await mappings.exit[ JSImpl]();
export const ARGV  = await mappings.ARGV[ JSImpl]();
export const tconf = await mappings.tconf[JSImpl]();

export const runTests = async tests => {
	const tFinal = t(tconf);
	for (const testFn of tests) {
		await testFn(tFinal);
	}
};