wasm-rquickjs-cli 0.5.2

Tool for wrapping JavaScript modules as WebAssembly components using the QuickJS engine
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// test1: Basic DateTimeFormat - NestJS ConsoleLogger pattern
export function test1() {
    try {
        // NestJS ConsoleLogger pattern
        const formatted = new Intl.DateTimeFormat(undefined, {
            year: 'numeric',
            hour: 'numeric',
            minute: 'numeric',
            second: 'numeric',
            day: '2-digit',
            month: '2-digit',
        }).format(1741600245000); // 2025-03-10T14:30:45.000Z

        console.log("NestJS formatted:", formatted);

        // Verify it contains expected components
        if (!formatted.includes("2025")) {
            console.log("FAIL: missing year 2025");
            return false;
        }

        // node-cron pattern: get default timezone
        const tz = new Intl.DateTimeFormat().resolvedOptions().timeZone;
        console.log("Default timezone:", tz);
        if (tz !== "UTC") {
            console.log("FAIL: expected UTC timezone");
            return false;
        }

        // resolvedOptions
        const ro = new Intl.DateTimeFormat('en-US', { timeZone: 'UTC' }).resolvedOptions();
        console.log("Resolved locale:", ro.locale);
        if (ro.locale !== "en-US") {
            console.log("FAIL: expected en-US locale");
            return false;
        }

        // supportedLocalesOf
        const sl = Intl.DateTimeFormat.supportedLocalesOf(['en', 'fr']);
        console.log("Supported locales:", JSON.stringify(sl));

        console.log("test1 PASSED");
        return true;
    } catch (e) {
        console.log("test1 FAILED:", e.message, e.stack);
        return false;
    }
}

// test2: DateTimeFormat.formatToParts
export function test2() {
    try {
        const dtf = new Intl.DateTimeFormat('en-US', {
            year: 'numeric',
            month: '2-digit',
            day: '2-digit',
            hour: 'numeric',
            minute: '2-digit',
            second: '2-digit',
            timeZone: 'UTC',
            hour12: true,
        });

        const ts = Date.UTC(2025, 2, 10, 14, 30, 45); // March 10, 2025 14:30:45 UTC
        const parts = dtf.formatToParts(ts);
        console.log("Parts:", JSON.stringify(parts));

        // Verify key parts exist
        const types = parts.map(p => p.type);
        if (!types.includes("month") || !types.includes("day") || !types.includes("year")) {
            console.log("FAIL: missing date parts");
            return false;
        }
        if (!types.includes("hour") || !types.includes("minute") || !types.includes("second")) {
            console.log("FAIL: missing time parts");
            return false;
        }

        // Verify month is "03"
        const monthPart = parts.find(p => p.type === "month");
        if (monthPart.value !== "03") {
            console.log("FAIL: expected month 03, got", monthPart.value);
            return false;
        }

        // Verify day is "10"
        const dayPart = parts.find(p => p.type === "day");
        if (dayPart.value !== "10") {
            console.log("FAIL: expected day 10, got", dayPart.value);
            return false;
        }

        // Verify hour is "2" (2 PM in 12-hour)
        const hourPart = parts.find(p => p.type === "hour");
        if (hourPart.value !== "2") {
            console.log("FAIL: expected hour 2, got", hourPart.value);
            return false;
        }

        // Verify dayPeriod is "PM"
        const periodPart = parts.find(p => p.type === "dayPeriod");
        if (!periodPart || periodPart.value !== "PM") {
            console.log("FAIL: expected dayPeriod PM, got", periodPart);
            return false;
        }

        console.log("test2 PASSED");
        return true;
    } catch (e) {
        console.log("test2 FAILED:", e.message, e.stack);
        return false;
    }
}

// test3: NumberFormat
export function test3() {
    try {
        // Basic decimal
        const nf = new Intl.NumberFormat('en-US');
        const result = nf.format(1234567.89);
        console.log("Decimal:", result);
        if (!result.includes("1,234,567")) {
            console.log("FAIL: expected comma grouping");
            return false;
        }

        // Currency
        const cf = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' });
        const cResult = cf.format(1234.50);
        console.log("Currency:", cResult);
        if (!cResult.includes("$") || !cResult.includes("1,234")) {
            console.log("FAIL: expected $ and grouping");
            return false;
        }

        // Percent
        const pf = new Intl.NumberFormat('en-US', { style: 'percent' });
        const pResult = pf.format(0.75);
        console.log("Percent:", pResult);
        if (!pResult.includes("75") || !pResult.includes("%")) {
            console.log("FAIL: expected 75%");
            return false;
        }

        // minimumIntegerDigits (zero-padding) - Luxon uses this
        const padFmt = new Intl.NumberFormat('en-US', {
            minimumIntegerDigits: 2,
            useGrouping: false,
        });
        if (padFmt.format(5) !== "05") {
            console.log("FAIL: expected 05, got", padFmt.format(5));
            return false;
        }

        // formatToParts
        const parts = nf.formatToParts(1234.5);
        console.log("Parts:", JSON.stringify(parts));
        const types = parts.map(p => p.type);
        if (!types.includes("integer") || !types.includes("decimal") || !types.includes("fraction")) {
            console.log("FAIL: missing number parts");
            return false;
        }

        console.log("test3 PASSED");
        return true;
    } catch (e) {
        console.log("test3 FAILED:", e.message, e.stack);
        return false;
    }
}

// test4: Collator + PluralRules + statics
export function test4() {
    try {
        // Collator
        const col = new Intl.Collator('en-US');
        if (col.compare('a', 'b') >= 0) {
            console.log("FAIL: a should be < b");
            return false;
        }
        if (col.compare('b', 'a') <= 0) {
            console.log("FAIL: b should be > a");
            return false;
        }
        if (col.compare('a', 'a') !== 0) {
            console.log("FAIL: a should equal a");
            return false;
        }

        // Case-insensitive collation
        const ciCol = new Intl.Collator('en-US', { sensitivity: 'base' });
        if (ciCol.compare('A', 'a') !== 0) {
            console.log("FAIL: A should equal a with base sensitivity");
            return false;
        }

        // PluralRules
        const pr = new Intl.PluralRules('en-US');
        if (pr.select(1) !== "one") {
            console.log("FAIL: 1 should be 'one'");
            return false;
        }
        if (pr.select(2) !== "other") {
            console.log("FAIL: 2 should be 'other'");
            return false;
        }

        // Ordinal
        const ord = new Intl.PluralRules('en-US', { type: 'ordinal' });
        if (ord.select(1) !== "one") {
            console.log("FAIL: 1st should be 'one'");
            return false;
        }
        if (ord.select(2) !== "two") {
            console.log("FAIL: 2nd should be 'two'");
            return false;
        }
        if (ord.select(3) !== "few") {
            console.log("FAIL: 3rd should be 'few'");
            return false;
        }
        if (ord.select(4) !== "other") {
            console.log("FAIL: 4th should be 'other'");
            return false;
        }

        // getCanonicalLocales
        const cl = Intl.getCanonicalLocales('en');
        console.log("Canonical locales:", JSON.stringify(cl));

        // supportedValuesOf
        const calendars = Intl.supportedValuesOf('calendar');
        console.log("Calendars:", JSON.stringify(calendars));
        if (!calendars.includes("gregory")) {
            console.log("FAIL: should include gregory");
            return false;
        }

        // ListFormat and Segmenter should be defined
        if (typeof Intl.ListFormat !== "function") {
            console.log("FAIL: ListFormat should be a function");
            return false;
        }
        if (typeof Intl.Segmenter !== "function") {
            console.log("FAIL: Segmenter should be a function");
            return false;
        }

        // RelativeTimeFormat should be defined
        if (typeof Intl.RelativeTimeFormat !== "function") {
            console.log("FAIL: RelativeTimeFormat should be a function");
            return false;
        }

        console.log("test4 PASSED");
        return true;
    } catch (e) {
        console.log("test4 FAILED:", e.message, e.stack);
        return false;
    }
}

// test5: ListFormat
export function test5() {
    try {
        // Basic conjunction
        const lf = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' });
        const r1 = lf.format(['Alice', 'Bob', 'Carol']);
        console.log("Conjunction:", r1);
        if (r1 !== "Alice, Bob, and Carol") {
            console.log("FAIL: expected 'Alice, Bob, and Carol', got", r1);
            return false;
        }

        // Disjunction
        const lfOr = new Intl.ListFormat('en', { style: 'long', type: 'disjunction' });
        const r2 = lfOr.format(['Alice', 'Bob']);
        console.log("Disjunction:", r2);
        if (r2 !== "Alice or Bob") {
            console.log("FAIL: expected 'Alice or Bob', got", r2);
            return false;
        }

        // Empty and single item
        if (lf.format([]) !== "") {
            console.log("FAIL: empty list should format to ''");
            return false;
        }
        if (lf.format(['Alice']) !== "Alice") {
            console.log("FAIL: single item should format to 'Alice'");
            return false;
        }

        // formatToParts
        const parts = lf.formatToParts(['A', 'B', 'C']);
        console.log("Parts:", JSON.stringify(parts));
        const types = parts.map(p => p.type);
        if (types.filter(t => t === "element").length !== 3) {
            console.log("FAIL: expected 3 element parts");
            return false;
        }
        if (types.filter(t => t === "literal").length !== 2) {
            console.log("FAIL: expected 2 literal parts");
            return false;
        }

        // resolvedOptions
        const ro = lf.resolvedOptions();
        if (ro.type !== "conjunction" || ro.style !== "long") {
            console.log("FAIL: resolvedOptions mismatch");
            return false;
        }

        // Can be called without new (factory function)
        const lfNoNew = Intl.ListFormat('en', { style: 'long', type: 'conjunction' });
        if (lfNoNew.format(['X', 'Y']) !== "X and Y") {
            console.log("FAIL: ListFormat without new should work");
            return false;
        }

        console.log("test5 PASSED");
        return true;
    } catch (e) {
        console.log("test5 FAILED:", e.message, e.stack);
        return false;
    }
}

// test6: Segmenter
export function test6() {
    try {
        // Grapheme segmentation
        const seg = new Intl.Segmenter('en', { granularity: 'grapheme' });
        const segments = [...seg.segment('Hello')];
        console.log("Graphemes:", JSON.stringify(segments.map(s => s.segment)));
        if (segments.length !== 5) {
            console.log("FAIL: expected 5 graphemes, got", segments.length);
            return false;
        }
        if (segments[0].segment !== 'H' || segments[0].index !== 0) {
            console.log("FAIL: first grapheme should be 'H' at index 0");
            return false;
        }
        if (segments[4].segment !== 'o' || segments[4].index !== 4) {
            console.log("FAIL: last grapheme should be 'o' at index 4");
            return false;
        }

        // Word segmentation
        const wordSeg = new Intl.Segmenter('en', { granularity: 'word' });
        const words = [...wordSeg.segment('Hello, World!')];
        console.log("Words:", JSON.stringify(words.map(s => ({ seg: s.segment, wl: s.isWordLike }))));
        // Should have word-like segments
        const wordLike = words.filter(s => s.isWordLike);
        if (wordLike.length < 2) {
            console.log("FAIL: expected at least 2 word-like segments");
            return false;
        }
        if (wordLike[0].segment !== 'Hello') {
            console.log("FAIL: first word should be 'Hello', got", wordLike[0].segment);
            return false;
        }

        // Sentence segmentation
        const sentSeg = new Intl.Segmenter('en', { granularity: 'sentence' });
        const sentences = [...sentSeg.segment('Hello. World!')];
        console.log("Sentences:", JSON.stringify(sentences.map(s => s.segment)));
        if (sentences.length < 2) {
            console.log("FAIL: expected at least 2 sentences, got", sentences.length);
            return false;
        }

        // containing()
        const segs = wordSeg.segment('Hello, World!');
        const c0 = segs.containing(0);
        console.log("Containing(0):", JSON.stringify(c0));
        if (!c0 || c0.segment !== 'Hello') {
            console.log("FAIL: containing(0) should return 'Hello'");
            return false;
        }
        if (c0.input !== 'Hello, World!') {
            console.log("FAIL: input should be original string");
            return false;
        }

        // containing() out of range
        if (segs.containing(99) !== undefined) {
            console.log("FAIL: containing(99) should return undefined");
            return false;
        }

        // resolvedOptions
        const ro = seg.resolvedOptions();
        if (ro.granularity !== "grapheme") {
            console.log("FAIL: resolvedOptions granularity mismatch");
            return false;
        }

        // Can be called without new (factory function)
        const segNoNew = Intl.Segmenter('en', { granularity: 'grapheme' });
        if ([...segNoNew.segment('ab')].length !== 2) {
            console.log("FAIL: Segmenter without new should work");
            return false;
        }

        // Default granularity (used by string-width/yargs)
        const defaultSeg = new Intl.Segmenter();
        const defSegs = [...defaultSeg.segment('abc')];
        if (defSegs.length !== 3) {
            console.log("FAIL: default segmenter should produce 3 graphemes");
            return false;
        }

        console.log("test6 PASSED");
        return true;
    } catch (e) {
        console.log("test6 FAILED:", e.message, e.stack);
        return false;
    }
}