@gjsify/url 0.0.3 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -3
- package/lib/esm/index.js +442 -4
- package/lib/types/index.d.ts +72 -3
- package/package.json +16 -23
- package/src/index.spec.ts +1081 -85
- package/src/index.ts +555 -3
- package/tsconfig.json +27 -14
- package/tsconfig.tsbuildinfo +1 -0
- package/lib/cjs/index.js +0 -6
- package/test.gjs.mjs +0 -37603
- package/test.node.mjs +0 -379
- package/tsconfig.types.json +0 -8
package/test.node.mjs
DELETED
|
@@ -1,379 +0,0 @@
|
|
|
1
|
-
// ../../../node_modules/@girs/gjs/gjs.js
|
|
2
|
-
var imports = globalThis.imports || {};
|
|
3
|
-
|
|
4
|
-
// ../../gjs/unit/lib/esm/index.js
|
|
5
|
-
import nodeAssert from "assert";
|
|
6
|
-
var mainloop = globalThis?.imports?.mainloop;
|
|
7
|
-
var countTestsOverall = 0;
|
|
8
|
-
var countTestsFailed = 0;
|
|
9
|
-
var countTestsIgnored = 0;
|
|
10
|
-
var runtime = "";
|
|
11
|
-
var RED = "\x1B[31m";
|
|
12
|
-
var GREEN = "\x1B[32m";
|
|
13
|
-
var BLUE = "\x1B[34m";
|
|
14
|
-
var GRAY = "\x1B[90m";
|
|
15
|
-
var RESET = "\x1B[39m";
|
|
16
|
-
var print = globalThis.print || console.log;
|
|
17
|
-
var MatcherFactory = class {
|
|
18
|
-
constructor(actualValue, positive, negated) {
|
|
19
|
-
this.actualValue = actualValue;
|
|
20
|
-
this.positive = positive;
|
|
21
|
-
if (negated) {
|
|
22
|
-
this.not = negated;
|
|
23
|
-
} else {
|
|
24
|
-
this.not = new MatcherFactory(actualValue, !positive, this);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
not;
|
|
28
|
-
triggerResult(success, msg) {
|
|
29
|
-
if (success && !this.positive || !success && this.positive) {
|
|
30
|
-
++countTestsFailed;
|
|
31
|
-
throw new Error(msg);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
to(callback) {
|
|
35
|
-
this.triggerResult(
|
|
36
|
-
callback(this.actualValue),
|
|
37
|
-
` Expected callback to validate`
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
toBe(expectedValue) {
|
|
41
|
-
this.triggerResult(
|
|
42
|
-
this.actualValue === expectedValue,
|
|
43
|
-
` Expected values to match using ===
|
|
44
|
-
Expected: ${expectedValue} (${typeof expectedValue})
|
|
45
|
-
Actual: ${this.actualValue} (${typeof this.actualValue})`
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
toEqual(expectedValue) {
|
|
49
|
-
this.triggerResult(
|
|
50
|
-
this.actualValue == expectedValue,
|
|
51
|
-
` Expected values to match using ==
|
|
52
|
-
Expected: ${expectedValue} (${typeof expectedValue})
|
|
53
|
-
Actual: ${this.actualValue} (${typeof this.actualValue})`
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
toEqualArray(expectedValue) {
|
|
57
|
-
let success = Array.isArray(this.actualValue) && Array.isArray(expectedValue) && this.actualValue.length === expectedValue.length;
|
|
58
|
-
for (let i = 0; i < this.actualValue.length; i++) {
|
|
59
|
-
const actualVal = this.actualValue[i];
|
|
60
|
-
const expectedVal = expectedValue[i];
|
|
61
|
-
success = actualVal == expectedVal;
|
|
62
|
-
if (!success)
|
|
63
|
-
break;
|
|
64
|
-
}
|
|
65
|
-
this.triggerResult(
|
|
66
|
-
success,
|
|
67
|
-
` Expected array items to match using ==
|
|
68
|
-
Expected: ${expectedValue} (${typeof expectedValue})
|
|
69
|
-
Actual: ${this.actualValue} (${typeof this.actualValue})`
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
toMatch(expectedValue) {
|
|
73
|
-
if (typeof this.actualValue.match !== "function") {
|
|
74
|
-
throw new Error(`You can not use toMatch on type ${typeof this.actualValue}`);
|
|
75
|
-
}
|
|
76
|
-
this.triggerResult(
|
|
77
|
-
!!this.actualValue.match(expectedValue),
|
|
78
|
-
" Expected values to match using regular expression\n Expression: " + expectedValue + "\n Actual: " + this.actualValue
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
toBeDefined() {
|
|
82
|
-
this.triggerResult(
|
|
83
|
-
typeof this.actualValue !== "undefined",
|
|
84
|
-
` Expected value to be defined`
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
toBeUndefined() {
|
|
88
|
-
this.triggerResult(
|
|
89
|
-
typeof this.actualValue === "undefined",
|
|
90
|
-
` Expected value to be undefined`
|
|
91
|
-
);
|
|
92
|
-
}
|
|
93
|
-
toBeNull() {
|
|
94
|
-
this.triggerResult(
|
|
95
|
-
this.actualValue === null,
|
|
96
|
-
` Expected value to be null`
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
toBeTruthy() {
|
|
100
|
-
this.triggerResult(
|
|
101
|
-
this.actualValue,
|
|
102
|
-
` Expected value to be truthy`
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
toBeFalsy() {
|
|
106
|
-
this.triggerResult(
|
|
107
|
-
!this.actualValue,
|
|
108
|
-
` Expected value to be falsy`
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
toContain(needle) {
|
|
112
|
-
this.triggerResult(
|
|
113
|
-
this.actualValue instanceof Array && this.actualValue.indexOf(needle) !== -1,
|
|
114
|
-
` Expected ` + this.actualValue + ` to contain ` + needle
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
toBeLessThan(greaterValue) {
|
|
118
|
-
this.triggerResult(
|
|
119
|
-
this.actualValue < greaterValue,
|
|
120
|
-
` Expected ` + this.actualValue + ` to be less than ` + greaterValue
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
toBeGreaterThan(smallerValue) {
|
|
124
|
-
this.triggerResult(
|
|
125
|
-
this.actualValue > smallerValue,
|
|
126
|
-
` Expected ` + this.actualValue + ` to be greater than ` + smallerValue
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
toBeCloseTo(expectedValue, precision) {
|
|
130
|
-
const shiftHelper = Math.pow(10, precision);
|
|
131
|
-
this.triggerResult(
|
|
132
|
-
Math.round(this.actualValue * shiftHelper) / shiftHelper === Math.round(expectedValue * shiftHelper) / shiftHelper,
|
|
133
|
-
` Expected ` + this.actualValue + ` with precision ` + precision + ` to be close to ` + expectedValue
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
toThrow(ErrorType) {
|
|
137
|
-
let errorMessage = "";
|
|
138
|
-
let didThrow = false;
|
|
139
|
-
let typeMatch = true;
|
|
140
|
-
try {
|
|
141
|
-
this.actualValue();
|
|
142
|
-
didThrow = false;
|
|
143
|
-
} catch (e) {
|
|
144
|
-
errorMessage = e.message || "";
|
|
145
|
-
didThrow = true;
|
|
146
|
-
if (ErrorType) {
|
|
147
|
-
typeMatch = e instanceof ErrorType;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
const functionName = this.actualValue.name || typeof this.actualValue === "function" ? "[anonymous function]" : this.actualValue.toString();
|
|
151
|
-
this.triggerResult(
|
|
152
|
-
didThrow,
|
|
153
|
-
` Expected ${functionName} to ${this.positive ? "throw" : "not throw"} an exception ${!this.positive && errorMessage ? `, but an error with the message "${errorMessage}" was thrown` : ""}`
|
|
154
|
-
);
|
|
155
|
-
if (ErrorType) {
|
|
156
|
-
this.triggerResult(
|
|
157
|
-
typeMatch,
|
|
158
|
-
` Expected Error type '${ErrorType.name}', but the error is not an instance of it`
|
|
159
|
-
);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
};
|
|
163
|
-
var describe = async function(moduleName, callback) {
|
|
164
|
-
print("\n" + moduleName);
|
|
165
|
-
await callback();
|
|
166
|
-
beforeEachCb = null;
|
|
167
|
-
afterEachCb = null;
|
|
168
|
-
};
|
|
169
|
-
var beforeEachCb;
|
|
170
|
-
var afterEachCb;
|
|
171
|
-
var it = async function(expectation, callback) {
|
|
172
|
-
try {
|
|
173
|
-
if (typeof beforeEachCb === "function") {
|
|
174
|
-
await beforeEachCb();
|
|
175
|
-
}
|
|
176
|
-
await callback();
|
|
177
|
-
if (typeof afterEachCb === "function") {
|
|
178
|
-
await afterEachCb();
|
|
179
|
-
}
|
|
180
|
-
print(` ${GREEN}\u2714${RESET} ${GRAY}${expectation}${RESET}`);
|
|
181
|
-
} catch (e) {
|
|
182
|
-
print(` ${RED}\u274C${RESET} ${GRAY}${expectation}${RESET}`);
|
|
183
|
-
print(`${RED}${e.message}${RESET}`);
|
|
184
|
-
if (e.stack)
|
|
185
|
-
print(e.stack);
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
var expect = function(actualValue) {
|
|
189
|
-
++countTestsOverall;
|
|
190
|
-
const expecter = new MatcherFactory(actualValue, true);
|
|
191
|
-
return expecter;
|
|
192
|
-
};
|
|
193
|
-
var assert = function(success, message) {
|
|
194
|
-
++countTestsOverall;
|
|
195
|
-
if (!success) {
|
|
196
|
-
++countTestsFailed;
|
|
197
|
-
}
|
|
198
|
-
nodeAssert(success, message);
|
|
199
|
-
};
|
|
200
|
-
assert.strictEqual = function(actual, expected, message) {
|
|
201
|
-
++countTestsOverall;
|
|
202
|
-
try {
|
|
203
|
-
nodeAssert.strictEqual(actual, expected, message);
|
|
204
|
-
} catch (error) {
|
|
205
|
-
++countTestsFailed;
|
|
206
|
-
throw error;
|
|
207
|
-
}
|
|
208
|
-
};
|
|
209
|
-
assert.throws = function(promiseFn, ...args) {
|
|
210
|
-
++countTestsOverall;
|
|
211
|
-
let error;
|
|
212
|
-
try {
|
|
213
|
-
promiseFn();
|
|
214
|
-
} catch (e) {
|
|
215
|
-
error = e;
|
|
216
|
-
}
|
|
217
|
-
if (!error)
|
|
218
|
-
++countTestsFailed;
|
|
219
|
-
nodeAssert.throws(() => {
|
|
220
|
-
if (error)
|
|
221
|
-
throw error;
|
|
222
|
-
}, args[0], args[1]);
|
|
223
|
-
};
|
|
224
|
-
assert.deepStrictEqual = function(actual, expected, message) {
|
|
225
|
-
++countTestsOverall;
|
|
226
|
-
try {
|
|
227
|
-
nodeAssert.deepStrictEqual(actual, expected, message);
|
|
228
|
-
} catch (error) {
|
|
229
|
-
++countTestsFailed;
|
|
230
|
-
throw error;
|
|
231
|
-
}
|
|
232
|
-
};
|
|
233
|
-
var runTests = async function(namespaces) {
|
|
234
|
-
for (const subNamespace in namespaces) {
|
|
235
|
-
const namespace = namespaces[subNamespace];
|
|
236
|
-
if (typeof namespace === "function") {
|
|
237
|
-
await namespace();
|
|
238
|
-
} else if (typeof namespace === "object") {
|
|
239
|
-
await runTests(namespace);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
};
|
|
243
|
-
var printResult = () => {
|
|
244
|
-
if (countTestsIgnored) {
|
|
245
|
-
print(`
|
|
246
|
-
${BLUE}\u2714 ${countTestsIgnored} ignored test${countTestsIgnored > 1 ? "s" : ""}${RESET}`);
|
|
247
|
-
}
|
|
248
|
-
if (countTestsFailed) {
|
|
249
|
-
print(`
|
|
250
|
-
${RED}\u274C ${countTestsFailed} of ${countTestsOverall} tests failed${RESET}`);
|
|
251
|
-
} else {
|
|
252
|
-
print(`
|
|
253
|
-
${GREEN}\u2714 ${countTestsOverall} completed${RESET}`);
|
|
254
|
-
}
|
|
255
|
-
};
|
|
256
|
-
var getRuntime = async () => {
|
|
257
|
-
if (runtime && runtime !== "Unknown") {
|
|
258
|
-
return runtime;
|
|
259
|
-
}
|
|
260
|
-
if (globalThis.Deno?.version?.deno) {
|
|
261
|
-
return "Deno " + globalThis.Deno?.version?.deno;
|
|
262
|
-
} else {
|
|
263
|
-
let process = globalThis.process;
|
|
264
|
-
if (!process) {
|
|
265
|
-
try {
|
|
266
|
-
process = await import("process");
|
|
267
|
-
} catch (error) {
|
|
268
|
-
console.error(error);
|
|
269
|
-
console.warn(error.message);
|
|
270
|
-
runtime = "Unknown";
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
if (process?.versions?.gjs) {
|
|
274
|
-
runtime = "Gjs " + process.versions.gjs;
|
|
275
|
-
} else if (process?.versions?.node) {
|
|
276
|
-
runtime = "Node.js " + process.versions.node;
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
return runtime || "Unknown";
|
|
280
|
-
};
|
|
281
|
-
var printRuntime = async () => {
|
|
282
|
-
const runtime2 = await getRuntime();
|
|
283
|
-
print(`
|
|
284
|
-
Running on ${runtime2}`);
|
|
285
|
-
};
|
|
286
|
-
var run = async (namespaces) => {
|
|
287
|
-
printRuntime().then(async () => {
|
|
288
|
-
return runTests(namespaces).then(() => {
|
|
289
|
-
printResult();
|
|
290
|
-
print();
|
|
291
|
-
mainloop?.quit();
|
|
292
|
-
});
|
|
293
|
-
});
|
|
294
|
-
mainloop?.run();
|
|
295
|
-
};
|
|
296
|
-
|
|
297
|
-
// src/index.spec.ts
|
|
298
|
-
import { fileURLToPath, URL } from "url";
|
|
299
|
-
var index_spec_default = async () => {
|
|
300
|
-
var fileURLToPathTestCases = [
|
|
301
|
-
// Lowercase ascii alpha
|
|
302
|
-
{ path: "/foo", fileURL: "file:///foo" },
|
|
303
|
-
// Uppercase ascii alpha
|
|
304
|
-
{ path: "/FOO", fileURL: "file:///FOO" },
|
|
305
|
-
// dir
|
|
306
|
-
{ path: "/dir/foo", fileURL: "file:///dir/foo" },
|
|
307
|
-
// trailing separator
|
|
308
|
-
{ path: "/dir/", fileURL: "file:///dir/" },
|
|
309
|
-
// dot
|
|
310
|
-
{ path: "/foo.mjs", fileURL: "file:///foo.mjs" },
|
|
311
|
-
// space
|
|
312
|
-
{ path: "/foo bar", fileURL: "file:///foo%20bar" },
|
|
313
|
-
// question mark
|
|
314
|
-
{ path: "/foo?bar", fileURL: "file:///foo%3Fbar" },
|
|
315
|
-
// number sign
|
|
316
|
-
{ path: "/foo#bar", fileURL: "file:///foo%23bar" },
|
|
317
|
-
// ampersand
|
|
318
|
-
{ path: "/foo&bar", fileURL: "file:///foo&bar" },
|
|
319
|
-
// equals
|
|
320
|
-
{ path: "/foo=bar", fileURL: "file:///foo=bar" },
|
|
321
|
-
// colon
|
|
322
|
-
{ path: "/foo:bar", fileURL: "file:///foo:bar" },
|
|
323
|
-
// semicolon
|
|
324
|
-
{ path: "/foo;bar", fileURL: "file:///foo;bar" },
|
|
325
|
-
// percent
|
|
326
|
-
{ path: "/foo%bar", fileURL: "file:///foo%25bar" },
|
|
327
|
-
// backslash
|
|
328
|
-
{ path: "/foo\\bar", fileURL: "file:///foo%5Cbar" },
|
|
329
|
-
// backspace
|
|
330
|
-
{ path: "/foo\bbar", fileURL: "file:///foo%08bar" },
|
|
331
|
-
// tab
|
|
332
|
-
{ path: "/foo bar", fileURL: "file:///foo%09bar" },
|
|
333
|
-
// newline
|
|
334
|
-
{ path: "/foo\nbar", fileURL: "file:///foo%0Abar" },
|
|
335
|
-
// carriage return
|
|
336
|
-
{ path: "/foo\rbar", fileURL: "file:///foo%0Dbar" },
|
|
337
|
-
// latin1
|
|
338
|
-
{ path: "/f\xF3\xF3b\xE0r", fileURL: "file:///f%C3%B3%C3%B3b%C3%A0r" },
|
|
339
|
-
// Euro sign (BMP code point)
|
|
340
|
-
{ path: "/\u20AC", fileURL: "file:///%E2%82%AC" },
|
|
341
|
-
// Rocket emoji (non-BMP code point)
|
|
342
|
-
{ path: "/\u{1F680}", fileURL: "file:///%F0%9F%9A%80" }
|
|
343
|
-
];
|
|
344
|
-
for (const fileURLToPathTestCase of fileURLToPathTestCases) {
|
|
345
|
-
await describe("url.fileURLToPath(" + fileURLToPathTestCase.fileURL + ")", async () => {
|
|
346
|
-
await it(`should return ${fileURLToPathTestCase.path} from string`, async () => {
|
|
347
|
-
var fromString = fileURLToPath(fileURLToPathTestCase.fileURL);
|
|
348
|
-
expect(fromString).toEqual(fileURLToPathTestCase.path);
|
|
349
|
-
});
|
|
350
|
-
await it(`should return ${fileURLToPathTestCase.path} from URL`, async () => {
|
|
351
|
-
var fromURL = fileURLToPath(new URL(fileURLToPathTestCase.fileURL));
|
|
352
|
-
expect(fromURL).toEqual(fileURLToPathTestCase.path);
|
|
353
|
-
});
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
for (const val in [
|
|
357
|
-
"https://host/y",
|
|
358
|
-
"file://host/a",
|
|
359
|
-
new URL("https://host/y"),
|
|
360
|
-
"file:///a%2F/",
|
|
361
|
-
"",
|
|
362
|
-
null,
|
|
363
|
-
void 0,
|
|
364
|
-
1,
|
|
365
|
-
{},
|
|
366
|
-
true
|
|
367
|
-
]) {
|
|
368
|
-
await describe("url.fileURLToPath(" + val + ")", async () => {
|
|
369
|
-
await it(`should throw an Error`, async () => {
|
|
370
|
-
expect(() => {
|
|
371
|
-
fileURLToPath(val);
|
|
372
|
-
}).toThrow();
|
|
373
|
-
});
|
|
374
|
-
});
|
|
375
|
-
}
|
|
376
|
-
};
|
|
377
|
-
|
|
378
|
-
// src/test.ts
|
|
379
|
-
run({ indexTestSuite: index_spec_default });
|