@gjsify/util 0.0.2

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 ADDED
@@ -0,0 +1,6 @@
1
+ # @gjsify/util
2
+
3
+ Node.js util module for Gjs
4
+ ## Inspirations and credits
5
+ * https://github.com/browserify/node-util
6
+ * https://github.com/denoland/deno_std/blob/main/node/util.ts
@@ -0,0 +1,36 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var src_exports = {};
30
+ __export(src_exports, {
31
+ default: () => src_default
32
+ });
33
+ module.exports = __toCommonJS(src_exports);
34
+ __reExport(src_exports, require("@gjsify/deno_std/node/util"), module.exports);
35
+ var import_util = __toESM(require("@gjsify/deno_std/node/util"), 1);
36
+ var src_default = import_util.default;
@@ -0,0 +1,36 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var types_exports = {};
30
+ __export(types_exports, {
31
+ default: () => types_default
32
+ });
33
+ module.exports = __toCommonJS(types_exports);
34
+ __reExport(types_exports, require("@gjsify/deno_std/node/util/types"), module.exports);
35
+ var import_types = __toESM(require("@gjsify/deno_std/node/util/types"), 1);
36
+ var types_default = import_types.default;
@@ -0,0 +1,6 @@
1
+ export * from "@gjsify/deno_std/node/util";
2
+ import util from "@gjsify/deno_std/node/util";
3
+ var src_default = util;
4
+ export {
5
+ src_default as default
6
+ };
@@ -0,0 +1,6 @@
1
+ export * from "@gjsify/deno_std/node/util/types";
2
+ import types from "@gjsify/deno_std/node/util/types";
3
+ var types_default = types;
4
+ export {
5
+ types_default as default
6
+ };
@@ -0,0 +1,3 @@
1
+ export * from '@gjsify/deno_std/node/util';
2
+ import util from '@gjsify/deno_std/node/util';
3
+ export default util;
@@ -0,0 +1,3 @@
1
+ export * from '@gjsify/deno_std/node/util/types';
2
+ import types from '@gjsify/deno_std/node/util/types';
3
+ export default types;
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@gjsify/util",
3
+ "version": "0.0.2",
4
+ "description": "Node.js util module for Gjs",
5
+ "type": "module",
6
+ "main": "lib/cjs/index.js",
7
+ "module": "lib/esm/index.js",
8
+ "types": "lib/types/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": {
12
+ "types": "./lib/types/index.d.ts",
13
+ "default": "./lib/esm/index.js"
14
+ },
15
+ "require": {
16
+ "types": "./lib/types/index.d.ts",
17
+ "default": "./lib/cjs/index.js"
18
+ }
19
+ }
20
+ },
21
+ "scripts": {
22
+ "clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo || exit 0",
23
+ "print:name": "echo '@gjsify/util'",
24
+ "build": "yarn print:name && yarn build:gjsify && yarn build:types",
25
+ "build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
26
+ "build:types": "tsc --project tsconfig.types.json || exit 0",
27
+ "build:test": "yarn build:test:gjs && yarn build:test:node",
28
+ "build:test:gjs": "gjsify build src/test.ts --app gjs --outfile test.gjs.mjs",
29
+ "build:test:node": "gjsify build src/test.ts --app node --outfile test.node.mjs",
30
+ "test": "yarn print:name && yarn build:gjsify && yarn build:test && yarn test:node && yarn test:gjs",
31
+ "test:gjs": "gjs -m test.gjs.mjs",
32
+ "test:node": "node test.node.mjs"
33
+ },
34
+ "keywords": [
35
+ "gjs",
36
+ "node",
37
+ "util"
38
+ ],
39
+ "devDependencies": {
40
+ "@gjsify/cli": "^0.0.2",
41
+ "@gjsify/unit": "^0.0.2",
42
+ "@types/inherits": "^0.0.30",
43
+ "@types/node": "^20.3.1",
44
+ "typescript": "^5.1.3"
45
+ },
46
+ "dependencies": {
47
+ "@gjsify/deno_std": "^0.0.2",
48
+ "is-arguments": "^1.1.1",
49
+ "is-generator-function": "^1.0.10",
50
+ "is-typed-array": "^1.1.10",
51
+ "util": "^0.12.5",
52
+ "which-typed-array": "^1.1.9"
53
+ }
54
+ }
@@ -0,0 +1,256 @@
1
+ import { describe, it, expect } from '@gjsify/unit';
2
+ import * as util from 'util';
3
+
4
+ // https://github.com/chalk/ansi-regex/blob/02fa893d619d3da85411acc8fd4e2eea0e95a9d9/index.js
5
+ const ANSI_PATTERN = new RegExp(
6
+ [
7
+ "[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
8
+ "(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))",
9
+ ].join("|"),
10
+ "g",
11
+ );
12
+
13
+ // TODO build deno_std/fmt for this method
14
+ function stripColor(string: string): string {
15
+ return string.replace(ANSI_PATTERN, "");
16
+ }
17
+
18
+
19
+ export default async () => {
20
+ // See packages/deno/deno_std/original/node/util_test.ts
21
+ await describe('[util] format', async () => {
22
+ await it('should return the right result', async () => {
23
+ expect(util.format("%o", [10, 11])).toBe("[ 10, 11, [length]: 2 ]");
24
+ });
25
+ });
26
+
27
+
28
+ await describe("[util] inspect.custom", async () => {
29
+ await it('should be the symbol for nodejs.util.inspect.custom', async () => {
30
+ expect(util.inspect.custom.description).toEqual("nodejs.util.inspect.custom");
31
+ });
32
+ });
33
+
34
+ await describe("[util] inspect", async () => {
35
+
36
+ await it('should return the right results', async () => {
37
+ expect(stripColor(util.inspect({ foo: 123 }))).toBe("{ foo: 123 }");
38
+ expect(stripColor(util.inspect("Deno's logo is so cute."))).toBe(`"Deno's logo is so cute."`);
39
+ expect(stripColor(util.inspect([1, 2, 3, 4, 5, 6, 7]))).toBe(`[
40
+ 1, 2, 3, 4,
41
+ 5, 6, 7
42
+ ]`
43
+ );
44
+ });
45
+ });
46
+
47
+ await describe("[util] isBoolean", async () => {
48
+
49
+ await it('should return the right results', async () => {
50
+ expect(util.isBoolean(true)).toBeTruthy();
51
+ // TODO: this is true in Gjs / Deno but false in Node.js
52
+ // expect(util.isBoolean(new Boolean())).toBeTruthy();
53
+ // expect(util.isBoolean(new Boolean(true))).toBeTruthy();
54
+ expect(util.isBoolean(false)).toBeTruthy();
55
+ expect(util.isBoolean("deno")).toBeFalsy();
56
+ expect(util.isBoolean("true")).toBeFalsy();
57
+ });
58
+ });
59
+
60
+ await describe("[util] isNull", async () => {
61
+ await it('should return the right results', async () => {
62
+ let n;
63
+ expect(util.isNull(null)).toBeTruthy();
64
+ expect(util.isNull(n)).toBeFalsy();
65
+ expect(util.isNull(0)).toBeFalsy();
66
+ expect(util.isNull({})).toBeFalsy();
67
+ });
68
+ });
69
+
70
+ await describe("[util] isNullOrUndefined", async () => {
71
+ await it('should return the right results', async () => {
72
+ let n;
73
+ expect(util.isNullOrUndefined(null)).toBeTruthy();
74
+ expect(util.isNullOrUndefined(n)).toBeTruthy();
75
+ expect(util.isNullOrUndefined({})).toBeFalsy();
76
+ expect(util.isNullOrUndefined("undefined")).toBeFalsy();
77
+ });
78
+ });
79
+
80
+ await describe("[util] isNumber", async () => {
81
+ await it('should return the right results', async () => {
82
+ expect(util.isNumber(666)).toBeTruthy();
83
+ // TODO: this is true in Gjs / Deno but false in Node.js
84
+ // expect(util.isNumber(new Number(666))).toBeTruthy();
85
+ expect(util.isNumber("999")).toBeFalsy();
86
+ expect(util.isNumber(null)).toBeFalsy();
87
+ });
88
+ });
89
+
90
+ await describe("[util] isString", async () => {
91
+ await it('should return the right results', async () => {
92
+ expect(util.isString("deno")).toBeTruthy();
93
+ // TODO: this is true in Gjs / Deno but false in Node.js
94
+ // expect(util.isString(new String("DIO"))).toBeTruthy();
95
+ expect(util.isString(1337)).toBeFalsy();
96
+ });
97
+ });
98
+
99
+ await describe("[util] isSymbol", async () => {
100
+ await it('should return the right results', async () => {
101
+ expect(util.isSymbol(Symbol())).toBeTruthy();
102
+ expect(util.isSymbol(123)).toBeFalsy();
103
+ expect(util.isSymbol("string")).toBeFalsy();
104
+ });
105
+ });
106
+
107
+ await describe("[util] isUndefined", async () => {
108
+ await it('should return the right results', async () => {
109
+ let t;
110
+ expect(util.isUndefined(t)).toBeTruthy();
111
+ expect(util.isUndefined("undefined")).toBeFalsy();
112
+ expect(util.isUndefined({})).toBeFalsy();
113
+ });
114
+ });
115
+
116
+ await describe("[util] isObject", async () => {
117
+ await it('should return the right results', async () => {
118
+ const dio = { stand: "Za Warudo" };
119
+ expect(util.isObject(dio)).toBeTruthy();
120
+ expect(util.isObject(new RegExp(/Toki Wo Tomare/))).toBeTruthy();
121
+ expect(util.isObject("Jotaro")).toBeFalsy();
122
+ });
123
+ });
124
+
125
+ await describe("[util] isError", async () => {
126
+ await it('should return the right results', async () => {
127
+ const java = new Error();
128
+ const nodejs = new TypeError();
129
+ const deno = "Future";
130
+ expect(util.isError(java)).toBeTruthy();
131
+ expect(util.isError(nodejs)).toBeTruthy();
132
+ expect(util.isError(deno)).toBeFalsy();
133
+ });
134
+ });
135
+
136
+ await describe("[util] isFunction", async () => {
137
+ await it('should return the right results', async () => {
138
+ const f = function () { };
139
+ expect(util.isFunction(f)).toBeTruthy();
140
+ expect(util.isFunction({})).toBeFalsy();
141
+ expect(util.isFunction(new RegExp(/f/))).toBeFalsy();
142
+ });
143
+ });
144
+
145
+ await describe("[util] isRegExp", async () => {
146
+ await it('should return the right results', async () => {
147
+ expect(util.isRegExp(new RegExp(/f/))).toBeTruthy();
148
+ expect(util.isRegExp(/fuManchu/)).toBeTruthy();
149
+ expect(util.isRegExp({ evil: "eye" })).toBeFalsy();
150
+ expect(util.isRegExp(null)).toBeFalsy();
151
+ });
152
+ });
153
+
154
+ await describe("[util] isArray", async () => {
155
+ await it('should return the right results', async () => {
156
+ expect(util.isArray([])).toBeTruthy();
157
+ expect(util.isArray({ yaNo: "array" })).toBeFalsy();
158
+ expect(util.isArray(null)).toBeFalsy();
159
+ });
160
+ });
161
+
162
+ await describe("[util] isPrimitive", async () => {
163
+ await it('should return the right results', async () => {
164
+ const stringType = "hasti";
165
+ const booleanType = true;
166
+ const integerType = 2;
167
+ const symbolType = Symbol("anything");
168
+
169
+ const functionType = function doBest() { };
170
+ const objectType = { name: "ali" };
171
+ const arrayType = [1, 2, 3];
172
+
173
+ expect(util.isPrimitive(stringType)).toBeTruthy();
174
+ expect(util.isPrimitive(booleanType)).toBeTruthy();
175
+ expect(util.isPrimitive(integerType)).toBeTruthy();
176
+ expect(util.isPrimitive(symbolType)).toBeTruthy();
177
+ expect(util.isPrimitive(null)).toBeTruthy();
178
+ expect(util.isPrimitive(undefined)).toBeTruthy();
179
+ expect(util.isPrimitive(functionType)).toBeFalsy();
180
+ expect(util.isPrimitive(arrayType)).toBeFalsy();
181
+ expect(util.isPrimitive(objectType)).toBeFalsy();
182
+ });
183
+ });
184
+
185
+ await describe("[util] TextDecoder", async () => {
186
+ await it('should return the right results', async () => {
187
+ expect(util.TextDecoder === TextDecoder).toBeTruthy();
188
+ const td: util.TextDecoder = new util.TextDecoder();
189
+ expect(td instanceof TextDecoder).toBeTruthy();
190
+ });
191
+ });
192
+
193
+ await describe("[util] TextEncoder", async () => {
194
+ await it('should return the right results', async () => {
195
+ expect(util.TextEncoder === TextEncoder).toBeTruthy();
196
+ const te: util.TextEncoder = new util.TextEncoder();
197
+ expect(te instanceof TextEncoder).toBeTruthy();
198
+ });
199
+ });
200
+
201
+ await describe("[util] isDate", async () => {
202
+ await it('should return the right results', async () => {
203
+ // Test verifies the method is exposed. See _util/_util_types_test for details
204
+ expect(util.types.isDate(new Date())).toBeTruthy();
205
+ });
206
+ });
207
+
208
+ await describe("[util] getSystemErrorName()", async () => {
209
+ await it('should return the right results', async () => {
210
+ type FnTestInvalidArg = (code?: unknown) => void;
211
+
212
+ expect(
213
+ () => (util.getSystemErrorName as FnTestInvalidArg)(),
214
+ ).toThrow();
215
+
216
+ try {
217
+ (util.getSystemErrorName as FnTestInvalidArg)()
218
+ } catch (error) {
219
+ expect(error instanceof Error).toBeTruthy();
220
+ expect(error instanceof TypeError).toBeTruthy();
221
+ }
222
+
223
+ expect(
224
+ () => (util.getSystemErrorName as FnTestInvalidArg)(1),
225
+ ).toThrow();
226
+
227
+ try {
228
+ (util.getSystemErrorName as FnTestInvalidArg)(1)
229
+ } catch (error) {
230
+ expect(error instanceof Error).toBeTruthy();
231
+ expect(error instanceof RangeError).toBeTruthy();
232
+ }
233
+
234
+ // FIXME: Returns undefined on Deno
235
+ expect(util.getSystemErrorName(-424242)).toBe('Unknown system error -424242');
236
+
237
+ // TODO
238
+ const os = globalThis.process?.platform || (globalThis as any).Deno?.build?.os || 'linux';
239
+
240
+ switch (os) {
241
+ case "win32":
242
+ case "windows" as any:
243
+ expect(util.getSystemErrorName(-4091)).toBe("EADDRINUSE");
244
+ break;
245
+
246
+ case "darwin":
247
+ expect(util.getSystemErrorName(-48)).toBe("EADDRINUSE");
248
+ break;
249
+
250
+ case "linux":
251
+ expect(util.getSystemErrorName(-98)).toBe("EADDRINUSE");
252
+ break;
253
+ }
254
+ });
255
+ });
256
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+
2
+ export * from '@gjsify/deno_std/node/util'
3
+ import util from '@gjsify/deno_std/node/util'
4
+ export default util
package/src/test.ts ADDED
@@ -0,0 +1,6 @@
1
+
2
+ import { run } from '@gjsify/unit';
3
+
4
+ import testSuite from './index.spec.js';
5
+
6
+ run({testSuite});
@@ -0,0 +1,4 @@
1
+
2
+ export * from '@gjsify/deno_std/node/util/types'
3
+ import types from '@gjsify/deno_std/node/util/types'
4
+ export default types