@douglasneuroinformatics/libjs 1.2.1 → 1.3.1
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/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/object.d.ts +5 -0
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +3 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/zod.d.ts +6 -0
- package/dist/zod.d.ts.map +1 -0
- package/dist/zod.js +28 -0
- package/package.json +18 -13
- package/dist/array.test.d.ts +0 -2
- package/dist/array.test.d.ts.map +0 -1
- package/dist/array.test.js +0 -24
- package/dist/datetime.test.d.ts +0 -2
- package/dist/datetime.test.d.ts.map +0 -1
- package/dist/datetime.test.js +0 -132
- package/dist/douglasneuroinformatics-libjs-1.2.1.tgz +0 -0
- package/dist/json.test.d.ts +0 -2
- package/dist/json.test.d.ts.map +0 -1
- package/dist/json.test.js +0 -57
- package/dist/number.test.d.ts +0 -2
- package/dist/number.test.d.ts.map +0 -1
- package/dist/number.test.js +0 -69
- package/dist/object.test.d.ts +0 -2
- package/dist/object.test.d.ts.map +0 -1
- package/dist/object.test.js +0 -84
- package/dist/random.test.d.ts +0 -2
- package/dist/random.test.d.ts.map +0 -1
- package/dist/random.test.js +0 -53
- package/dist/range.test.d.ts +0 -2
- package/dist/range.test.d.ts.map +0 -1
- package/dist/range.test.js +0 -11
- package/dist/string.test.d.ts +0 -2
- package/dist/string.test.d.ts.map +0 -1
- package/dist/string.test.js +0 -53
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/object.d.ts
CHANGED
|
@@ -24,4 +24,9 @@ export declare function isPlainObject(value: unknown): value is {
|
|
|
24
24
|
export declare function isAllUndefined<T extends {
|
|
25
25
|
[key: string]: unknown;
|
|
26
26
|
}>(obj: T): boolean;
|
|
27
|
+
export declare function filterObject<T extends {
|
|
28
|
+
[key: string]: unknown;
|
|
29
|
+
}>(obj: T, callback: (value: T[keyof T], key: keyof T) => boolean): {
|
|
30
|
+
[k: string]: unknown;
|
|
31
|
+
};
|
|
27
32
|
//# sourceMappingURL=object.d.ts.map
|
package/dist/object.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,MAAM,IAAI,QAAQ,CAAC;KACnD,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAChE,CAAC,CAAC;AAEH,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,EAAE,SAAS,SAAS,OAAO,GAAG,IAAI,EAC3E,GAAG,EAAE,CAAC,EACN,OAAO,CAAC,EAAE;IAAE,YAAY,EAAE,SAAS,CAAA;CAAE,GACpC,SAAS,SAAS,IAAI,GAAG,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAQ9C;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAGxD;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAE5D;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,CAUjF;AAED,wBAAgB,cAAc,CAAC,CAAC,SAAS;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,EAAE,GAAG,EAAE,CAAC,WAE1E"}
|
|
1
|
+
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,MAAM,IAAI,QAAQ,CAAC;KACnD,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAChE,CAAC,CAAC;AAEH,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,EAAE,SAAS,SAAS,OAAO,GAAG,IAAI,EAC3E,GAAG,EAAE,CAAC,EACN,OAAO,CAAC,EAAE;IAAE,YAAY,EAAE,SAAS,CAAA;CAAE,GACpC,SAAS,SAAS,IAAI,GAAG,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAQ9C;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAGxD;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAE5D;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,CAUjF;AAED,wBAAgB,cAAc,CAAC,CAAC,SAAS;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,EAAE,GAAG,EAAE,CAAC,WAE1E;AAED,wBAAgB,YAAY,CAAC,CAAC,SAAS;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,EAC/D,GAAG,EAAE,CAAC,EACN,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,KAAK,OAAO;;EAGvD"}
|
package/dist/object.js
CHANGED
|
@@ -38,3 +38,6 @@ export function isPlainObject(value) {
|
|
|
38
38
|
export function isAllUndefined(obj) {
|
|
39
39
|
return Object.values(obj).every((value) => value === undefined);
|
|
40
40
|
}
|
|
41
|
+
export function filterObject(obj, callback) {
|
|
42
|
+
return Object.fromEntries(Object.entries(obj).filter(([key, value]) => callback(value, key)));
|
|
43
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["../src/array.ts","../src/datetime.ts","../src/index.ts","../src/json.ts","../src/number.ts","../src/object.ts","../src/random.ts","../src/range.ts","../src/string.ts","../src/types.ts","../src/zod.ts"],"version":"5.6.3"}
|
package/dist/zod.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/** Used to determine if object is of type `ZodType` independent of specific instances or library versions */
|
|
3
|
+
export declare function isZodType(arg: unknown): arg is z.ZodTypeAny;
|
|
4
|
+
export declare const $BooleanLike: z.ZodEffects<z.ZodBoolean, boolean, unknown>;
|
|
5
|
+
export declare const $NumberLike: z.ZodEffects<z.ZodNumber, number, unknown>;
|
|
6
|
+
//# sourceMappingURL=zod.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod.d.ts","sourceRoot":"","sources":["../src/zod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAKxB,6GAA6G;AAC7G,wBAAgB,SAAS,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,CAAC,CAAC,UAAU,CAM3D;AAED,eAAO,MAAM,YAAY,8CASV,CAAC;AAEhB,eAAO,MAAM,WAAW,4CAKV,CAAC"}
|
package/dist/zod.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { isNumberLike, parseNumber } from './number.js';
|
|
3
|
+
import { isObject } from './object.js';
|
|
4
|
+
/** Used to determine if object is of type `ZodType` independent of specific instances or library versions */
|
|
5
|
+
export function isZodType(arg) {
|
|
6
|
+
let prototype = null;
|
|
7
|
+
if (isObject(arg) && isObject(arg.constructor)) {
|
|
8
|
+
prototype = Reflect.getPrototypeOf(arg.constructor);
|
|
9
|
+
}
|
|
10
|
+
return Boolean(prototype && Reflect.get(prototype, 'name') === 'ZodType');
|
|
11
|
+
}
|
|
12
|
+
export const $BooleanLike = z.preprocess((arg) => {
|
|
13
|
+
if (typeof arg === 'string') {
|
|
14
|
+
if (arg.trim().toLowerCase() === 'true') {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
else if (arg.trim().toLowerCase() === 'false') {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return arg;
|
|
22
|
+
}, z.boolean());
|
|
23
|
+
export const $NumberLike = z.preprocess((arg) => {
|
|
24
|
+
if (isNumberLike(arg)) {
|
|
25
|
+
return parseNumber(arg);
|
|
26
|
+
}
|
|
27
|
+
return arg;
|
|
28
|
+
}, z.number());
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@douglasneuroinformatics/libjs",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
5
|
-
"packageManager": "pnpm@
|
|
4
|
+
"version": "1.3.1",
|
|
5
|
+
"packageManager": "pnpm@10.3.0",
|
|
6
6
|
"description": "A collection of utility functions and types for Node.js and the browser",
|
|
7
7
|
"author": "Joshua Unrau",
|
|
8
8
|
"license": "Apache-2.0",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"dist"
|
|
21
21
|
],
|
|
22
22
|
"scripts": {
|
|
23
|
-
"build": "tsc",
|
|
23
|
+
"build": "rm -rf dist && tsc -b tsconfig.build.json",
|
|
24
24
|
"build:docs": "typedoc",
|
|
25
25
|
"format": "prettier --write src",
|
|
26
26
|
"lint": "tsc --noEmit && eslint --fix src",
|
|
@@ -29,26 +29,26 @@
|
|
|
29
29
|
"test:coverage": "vitest run --coverage"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"type-fest": "^4.
|
|
32
|
+
"type-fest": "^4.34.1",
|
|
33
|
+
"zod": "^3.22.6"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
35
|
-
"@commitlint/cli": "^19.6.1",
|
|
36
|
-
"@commitlint/config-conventional": "^19.6.0",
|
|
37
36
|
"@douglasneuroinformatics/eslint-config": "^5.2.4",
|
|
38
37
|
"@douglasneuroinformatics/prettier-config": "^0.0.1",
|
|
39
|
-
"@douglasneuroinformatics/semantic-release": "^0.
|
|
38
|
+
"@douglasneuroinformatics/semantic-release": "^0.2.1",
|
|
40
39
|
"@douglasneuroinformatics/tsconfig": "^1.0.2",
|
|
41
|
-
"@
|
|
42
|
-
"
|
|
40
|
+
"@types/node": "22.x",
|
|
41
|
+
"@vitest/coverage-v8": "^3.0.5",
|
|
42
|
+
"eslint": "^9.20.1",
|
|
43
43
|
"husky": "^9.1.7",
|
|
44
|
-
"prettier": "^3.
|
|
45
|
-
"typedoc": "^0.27.
|
|
44
|
+
"prettier": "^3.5.0",
|
|
45
|
+
"typedoc": "^0.27.7",
|
|
46
46
|
"typescript": "5.6.x",
|
|
47
|
-
"vitest": "^
|
|
47
|
+
"vitest": "^3.0.5"
|
|
48
48
|
},
|
|
49
49
|
"commitlint": {
|
|
50
50
|
"extends": [
|
|
51
|
-
"@commitlint
|
|
51
|
+
"@douglasneuroinformatics/semantic-release/commitlint-config"
|
|
52
52
|
]
|
|
53
53
|
},
|
|
54
54
|
"prettier": "@douglasneuroinformatics/prettier-config",
|
|
@@ -56,5 +56,10 @@
|
|
|
56
56
|
"extends": [
|
|
57
57
|
"@douglasneuroinformatics/semantic-release"
|
|
58
58
|
]
|
|
59
|
+
},
|
|
60
|
+
"pnpm": {
|
|
61
|
+
"onlyBuiltDependencies": [
|
|
62
|
+
"esbuild"
|
|
63
|
+
]
|
|
59
64
|
}
|
|
60
65
|
}
|
package/dist/array.test.d.ts
DELETED
package/dist/array.test.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"array.test.d.ts","sourceRoot":"","sources":["../src/array.test.ts"],"names":[],"mappings":""}
|
package/dist/array.test.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { hasDuplicates, isUnique } from './array.js';
|
|
3
|
-
describe('isUnique', () => {
|
|
4
|
-
it('should return true for a unique array', () => {
|
|
5
|
-
expect(isUnique([1, 2, 3])).toBe(true);
|
|
6
|
-
});
|
|
7
|
-
it('should return true for an empty array', () => {
|
|
8
|
-
expect(isUnique([])).toBe(true);
|
|
9
|
-
});
|
|
10
|
-
it('should return false for an array with duplicate values', () => {
|
|
11
|
-
expect(isUnique([1, 2, 2])).toBe(false);
|
|
12
|
-
});
|
|
13
|
-
});
|
|
14
|
-
describe('hasDuplicates', () => {
|
|
15
|
-
it('should return false for a unique array', () => {
|
|
16
|
-
expect(hasDuplicates([1, 2, 3])).toBe(false);
|
|
17
|
-
});
|
|
18
|
-
it('should return false for an empty array', () => {
|
|
19
|
-
expect(hasDuplicates([])).toBe(false);
|
|
20
|
-
});
|
|
21
|
-
it('should return true for an array with duplicate values', () => {
|
|
22
|
-
expect(hasDuplicates([1, 2, 2])).toBe(true);
|
|
23
|
-
});
|
|
24
|
-
});
|
package/dist/datetime.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"datetime.test.d.ts","sourceRoot":"","sources":["../src/datetime.test.ts"],"names":[],"mappings":""}
|
package/dist/datetime.test.js
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import { parseDuration, sleep, toBasicISOString, toLocalISOString, yearsPassed } from './datetime.js';
|
|
3
|
-
describe('toLocalISOString', () => {
|
|
4
|
-
let getTimezoneOffset;
|
|
5
|
-
beforeEach(() => {
|
|
6
|
-
getTimezoneOffset = vi.spyOn(Date.prototype, 'getTimezoneOffset');
|
|
7
|
-
});
|
|
8
|
-
afterEach(() => {
|
|
9
|
-
vi.clearAllMocks();
|
|
10
|
-
});
|
|
11
|
-
it('should return the correct string when the current timezone is is UTC+00:00', () => {
|
|
12
|
-
getTimezoneOffset.mockReturnValueOnce(0);
|
|
13
|
-
expect(toLocalISOString(new Date('2025-01-01T00:00:00.000Z'))).toBe('2025-01-01T00:00:00.000');
|
|
14
|
-
});
|
|
15
|
-
it('should return the correct string when the current timezone is UTC-05:00', () => {
|
|
16
|
-
getTimezoneOffset.mockReturnValueOnce(300);
|
|
17
|
-
expect(toLocalISOString(new Date('2025-01-01T05:00:00.000Z'))).toBe('2025-01-01T00:00:00.000');
|
|
18
|
-
});
|
|
19
|
-
it('should return the correct string when the current timezone is UTC-08:00', () => {
|
|
20
|
-
getTimezoneOffset.mockReturnValueOnce(480);
|
|
21
|
-
expect(toLocalISOString(new Date('2025-01-01T05:00:00.000Z'))).toBe('2024-12-31T21:00:00.000');
|
|
22
|
-
});
|
|
23
|
-
it('should return the correct string when the current timezone is UTC+03:00', () => {
|
|
24
|
-
getTimezoneOffset.mockReturnValueOnce(-180);
|
|
25
|
-
expect(toLocalISOString(new Date('2025-01-01T05:00:00.000Z'))).toBe('2025-01-01T08:00:00.000');
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
describe('toBasicISOString', () => {
|
|
29
|
-
it('should return a string of the format yyyy-mm-dd', () => {
|
|
30
|
-
expect(toBasicISOString(new Date(2000, 0, 1))).toBe('2000-01-01');
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
describe('yearsPassed', () => {
|
|
34
|
-
it('should return zero for the date eleven months ago', () => {
|
|
35
|
-
const today = new Date();
|
|
36
|
-
const date = new Date(today.setMonth(today.getMonth() - 11));
|
|
37
|
-
expect(yearsPassed(date)).toBe(0);
|
|
38
|
-
});
|
|
39
|
-
it('should return one for the date eighteen months ago', () => {
|
|
40
|
-
const today = new Date();
|
|
41
|
-
const date = new Date(today.setMonth(today.getMonth() - 18));
|
|
42
|
-
expect(yearsPassed(date)).toBe(1);
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
describe('sleep', () => {
|
|
46
|
-
it('should wait for at least the specified time', async () => {
|
|
47
|
-
const start = Date.now();
|
|
48
|
-
await sleep(0.2);
|
|
49
|
-
const end = Date.now();
|
|
50
|
-
expect(end - start).toBeGreaterThanOrEqual(200);
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
describe('parseDuration', () => {
|
|
54
|
-
it('should fail to parse a negative duration', () => {
|
|
55
|
-
expect(() => parseDuration(-1)).toThrow('Cannot parse negative length of time: -1');
|
|
56
|
-
});
|
|
57
|
-
it('should parse a duration of zero', () => {
|
|
58
|
-
expect(parseDuration(0)).toEqual({
|
|
59
|
-
days: 0,
|
|
60
|
-
hours: 0,
|
|
61
|
-
milliseconds: 0,
|
|
62
|
-
minutes: 0,
|
|
63
|
-
seconds: 0
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
it('should parse a duration less than a second', () => {
|
|
67
|
-
expect(parseDuration(50)).toEqual({
|
|
68
|
-
days: 0,
|
|
69
|
-
hours: 0,
|
|
70
|
-
milliseconds: 50,
|
|
71
|
-
minutes: 0,
|
|
72
|
-
seconds: 0
|
|
73
|
-
});
|
|
74
|
-
expect(parseDuration(500)).toEqual({
|
|
75
|
-
days: 0,
|
|
76
|
-
hours: 0,
|
|
77
|
-
milliseconds: 500,
|
|
78
|
-
minutes: 0,
|
|
79
|
-
seconds: 0
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
it('should parse a duration less than one minute but more than one second', () => {
|
|
83
|
-
expect(parseDuration(11_100)).toEqual({
|
|
84
|
-
days: 0,
|
|
85
|
-
hours: 0,
|
|
86
|
-
milliseconds: 100,
|
|
87
|
-
minutes: 0,
|
|
88
|
-
seconds: 11
|
|
89
|
-
});
|
|
90
|
-
});
|
|
91
|
-
it('should parse a duration less than one hour but more than one minute', () => {
|
|
92
|
-
expect(parseDuration(60_000)).toEqual({
|
|
93
|
-
days: 0,
|
|
94
|
-
hours: 0,
|
|
95
|
-
milliseconds: 0,
|
|
96
|
-
minutes: 1,
|
|
97
|
-
seconds: 0
|
|
98
|
-
});
|
|
99
|
-
expect(parseDuration(62_500)).toEqual({
|
|
100
|
-
days: 0,
|
|
101
|
-
hours: 0,
|
|
102
|
-
milliseconds: 500,
|
|
103
|
-
minutes: 1,
|
|
104
|
-
seconds: 2
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
it('should parse a duration less than one day but more than one hour', () => {
|
|
108
|
-
expect(parseDuration(3_600_000)).toEqual({
|
|
109
|
-
days: 0,
|
|
110
|
-
hours: 1,
|
|
111
|
-
milliseconds: 0,
|
|
112
|
-
minutes: 0,
|
|
113
|
-
seconds: 0
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
it('should parse a duration greater than one day', () => {
|
|
117
|
-
expect(parseDuration(86_400_000)).toEqual({
|
|
118
|
-
days: 1,
|
|
119
|
-
hours: 0,
|
|
120
|
-
milliseconds: 0,
|
|
121
|
-
minutes: 0,
|
|
122
|
-
seconds: 0
|
|
123
|
-
});
|
|
124
|
-
expect(parseDuration(4_351_505_030)).toEqual({
|
|
125
|
-
days: 50,
|
|
126
|
-
hours: 8,
|
|
127
|
-
milliseconds: 30,
|
|
128
|
-
minutes: 45,
|
|
129
|
-
seconds: 5
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
});
|
|
Binary file
|
package/dist/json.test.d.ts
DELETED
package/dist/json.test.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"json.test.d.ts","sourceRoot":"","sources":["../src/json.test.ts"],"names":[],"mappings":""}
|
package/dist/json.test.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { replacer, reviver } from './json.js';
|
|
3
|
-
describe('replacer', () => {
|
|
4
|
-
it('should serialize a Set', () => {
|
|
5
|
-
const set = new Set([1, 2, 3]);
|
|
6
|
-
expect(JSON.parse(JSON.stringify({ set }, replacer))).toMatchObject({
|
|
7
|
-
set: {
|
|
8
|
-
__deserializedType: 'Set',
|
|
9
|
-
__isSerializedType: true,
|
|
10
|
-
value: [1, 2, 3]
|
|
11
|
-
}
|
|
12
|
-
});
|
|
13
|
-
});
|
|
14
|
-
it('should serialize a Date', () => {
|
|
15
|
-
const date = new Date();
|
|
16
|
-
expect(JSON.parse(JSON.stringify({ date }, replacer))).toMatchObject({
|
|
17
|
-
date: {
|
|
18
|
-
__isSerializedType: true
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
});
|
|
22
|
-
it('should return the value if it is not a Set or a Date', () => {
|
|
23
|
-
const obj = { a: 1 };
|
|
24
|
-
expect(replacer('', obj)).toBe(obj);
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
describe('reviver', () => {
|
|
28
|
-
it('should deserialize a serialized Set', () => {
|
|
29
|
-
const serializedSet = {
|
|
30
|
-
__deserializedType: 'Set',
|
|
31
|
-
__isSerializedType: true,
|
|
32
|
-
value: [1, 2, 3]
|
|
33
|
-
};
|
|
34
|
-
const deserializedSet = new Set([1, 2, 3]);
|
|
35
|
-
expect(reviver('', serializedSet)).toEqual(deserializedSet);
|
|
36
|
-
});
|
|
37
|
-
it('should deserialize a serialized Date', () => {
|
|
38
|
-
const date = new Date();
|
|
39
|
-
const serializedDate = {
|
|
40
|
-
__deserializedType: 'Date',
|
|
41
|
-
__isSerializedType: true,
|
|
42
|
-
value: date.toJSON()
|
|
43
|
-
};
|
|
44
|
-
expect(reviver('', serializedDate)).toEqual(date);
|
|
45
|
-
expect(JSON.parse(JSON.stringify({ date: serializedDate }), reviver)).toMatchObject({
|
|
46
|
-
date: expect.any(Date)
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
it('should return the value if it is not a plain object', () => {
|
|
50
|
-
const obj = new Date();
|
|
51
|
-
expect(reviver('', obj)).toBe(obj);
|
|
52
|
-
});
|
|
53
|
-
it('should return the value if it is a plain object but not a serialized Set or Date', () => {
|
|
54
|
-
const obj = { a: 1 };
|
|
55
|
-
expect(reviver('', obj)).toBe(obj);
|
|
56
|
-
});
|
|
57
|
-
});
|
package/dist/number.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"number.test.d.ts","sourceRoot":"","sources":["../src/number.test.ts"],"names":[],"mappings":""}
|
package/dist/number.test.js
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { isNumberLike, parseNumber } from './number.js';
|
|
3
|
-
const NUMBER_LIKE_VALUES = [
|
|
4
|
-
5e3,
|
|
5
|
-
0xff,
|
|
6
|
-
-0.1,
|
|
7
|
-
-0,
|
|
8
|
-
0.0,
|
|
9
|
-
+0,
|
|
10
|
-
3.14,
|
|
11
|
-
'5e3',
|
|
12
|
-
'5e+3',
|
|
13
|
-
'-0.1',
|
|
14
|
-
'-0',
|
|
15
|
-
'0.0',
|
|
16
|
-
'+0',
|
|
17
|
-
'3.14',
|
|
18
|
-
'1.',
|
|
19
|
-
'.1',
|
|
20
|
-
Number.MAX_VALUE,
|
|
21
|
-
Number.MIN_VALUE,
|
|
22
|
-
Infinity,
|
|
23
|
-
-Infinity,
|
|
24
|
-
'Infinity',
|
|
25
|
-
' +Infinity',
|
|
26
|
-
' -Infinity '
|
|
27
|
-
];
|
|
28
|
-
const NON_NUMBER_LIKE_VALUES = [
|
|
29
|
-
'1..',
|
|
30
|
-
'--5',
|
|
31
|
-
'A5',
|
|
32
|
-
'5A',
|
|
33
|
-
NaN,
|
|
34
|
-
null,
|
|
35
|
-
undefined,
|
|
36
|
-
'',
|
|
37
|
-
' ',
|
|
38
|
-
'foo',
|
|
39
|
-
[1],
|
|
40
|
-
{},
|
|
41
|
-
'NaN',
|
|
42
|
-
'infinity',
|
|
43
|
-
'--Infinity',
|
|
44
|
-
'++Infinity'
|
|
45
|
-
];
|
|
46
|
-
describe('isNumberLike', () => {
|
|
47
|
-
it('should return true for number-like values', () => {
|
|
48
|
-
NUMBER_LIKE_VALUES.forEach((value) => {
|
|
49
|
-
expect(isNumberLike(value)).toBe(true);
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
it('should return false for non-number-like values', () => {
|
|
53
|
-
NON_NUMBER_LIKE_VALUES.forEach((value) => {
|
|
54
|
-
expect(isNumberLike(value)).toBe(false);
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
describe('parseNumber', () => {
|
|
59
|
-
it('should parse numbers', () => {
|
|
60
|
-
NUMBER_LIKE_VALUES.forEach((value) => {
|
|
61
|
-
expect(Number.isNaN(parseNumber(value))).toBe(false);
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
it('should return NaN for non-numbers', () => {
|
|
65
|
-
NON_NUMBER_LIKE_VALUES.forEach((value) => {
|
|
66
|
-
expect(Number.isNaN(parseNumber(value))).toBe(true);
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
});
|
package/dist/object.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"object.test.d.ts","sourceRoot":"","sources":["../src/object.test.ts"],"names":[],"mappings":""}
|
package/dist/object.test.js
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { deepFreeze, isAllUndefined, isObject, isObjectLike, isPlainObject } from './object.js';
|
|
3
|
-
describe('deepFreeze', () => {
|
|
4
|
-
it('should not allow mutating a primitive value', () => {
|
|
5
|
-
const obj = deepFreeze({ foo: 1 });
|
|
6
|
-
expect(() => {
|
|
7
|
-
// @ts-expect-error - check runtime behavior
|
|
8
|
-
obj.foo = 2;
|
|
9
|
-
}).toThrow();
|
|
10
|
-
});
|
|
11
|
-
it('should not allow mutating a nested object', () => {
|
|
12
|
-
const obj = deepFreeze({ foo: { bar: 1 } });
|
|
13
|
-
expect(() => {
|
|
14
|
-
// @ts-expect-error - check runtime behavior
|
|
15
|
-
obj.foo.bar = 2;
|
|
16
|
-
}).toThrow();
|
|
17
|
-
});
|
|
18
|
-
it('should not allow mutating a deeply nested array', () => {
|
|
19
|
-
const obj = deepFreeze({ foo: { bar: [1, 2, 3] } });
|
|
20
|
-
expect(() => {
|
|
21
|
-
// @ts-expect-error - check runtime behavior
|
|
22
|
-
obj.foo.bar[0] = 2;
|
|
23
|
-
}).toThrow();
|
|
24
|
-
});
|
|
25
|
-
});
|
|
26
|
-
describe('isObject', () => {
|
|
27
|
-
it('should return true for an object literal', () => {
|
|
28
|
-
expect(isObject({})).toBe(true);
|
|
29
|
-
});
|
|
30
|
-
it('should return true for an array', () => {
|
|
31
|
-
expect(isObject([])).toBe(true);
|
|
32
|
-
});
|
|
33
|
-
it('should return true for a function', () => {
|
|
34
|
-
expect(isObject(() => null)).toBe(true);
|
|
35
|
-
});
|
|
36
|
-
it('should return false for null', () => {
|
|
37
|
-
expect(isObject(null)).toBe(false);
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
describe('isObjectLike', () => {
|
|
41
|
-
it('should return true for an object literal', () => {
|
|
42
|
-
expect(isObjectLike({})).toBe(true);
|
|
43
|
-
});
|
|
44
|
-
it('should return true for an array', () => {
|
|
45
|
-
expect(isObjectLike([])).toBe(true);
|
|
46
|
-
});
|
|
47
|
-
it('should return false for a function', () => {
|
|
48
|
-
expect(isObjectLike(() => null)).toBe(false);
|
|
49
|
-
});
|
|
50
|
-
it('should return false for null', () => {
|
|
51
|
-
expect(isObjectLike(() => null)).toBe(false);
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
describe('isPlainObject', () => {
|
|
55
|
-
it('should return true for an object literal', () => {
|
|
56
|
-
expect(isPlainObject({ foo: null })).toBe(true);
|
|
57
|
-
});
|
|
58
|
-
it('should return true for an object created with the Object constructor', () => {
|
|
59
|
-
expect(isPlainObject(new Object())).toBe(true);
|
|
60
|
-
});
|
|
61
|
-
it('should return true for an object with a null prototype', () => {
|
|
62
|
-
expect(isPlainObject(Object.create(null))).toBe(true);
|
|
63
|
-
});
|
|
64
|
-
it('should return false for an array', () => {
|
|
65
|
-
expect(isPlainObject([])).toBe(false);
|
|
66
|
-
});
|
|
67
|
-
it('should return false for a function', () => {
|
|
68
|
-
expect(isPlainObject(() => null)).toBe(false);
|
|
69
|
-
});
|
|
70
|
-
it('should return false for null', () => {
|
|
71
|
-
expect(isPlainObject(() => null)).toBe(false);
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
describe('isAllUndefined', () => {
|
|
75
|
-
it('should return true for an empty object', () => {
|
|
76
|
-
expect(isAllUndefined({})).toBe(true);
|
|
77
|
-
});
|
|
78
|
-
it('should return true for an object with an undefined property', () => {
|
|
79
|
-
expect(isAllUndefined({ foo: undefined })).toBe(true);
|
|
80
|
-
});
|
|
81
|
-
it('should return true for an object with a defined property', () => {
|
|
82
|
-
expect(isAllUndefined({ bar: null, foo: undefined })).toBe(false);
|
|
83
|
-
});
|
|
84
|
-
});
|
package/dist/random.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"random.test.d.ts","sourceRoot":"","sources":["../src/random.test.ts"],"names":[],"mappings":""}
|
package/dist/random.test.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { randomDate, randomInt, randomValue } from './random.js';
|
|
3
|
-
describe('randomInt', () => {
|
|
4
|
-
it('should return an integer value within the range', () => {
|
|
5
|
-
const min = 5;
|
|
6
|
-
const max = 8;
|
|
7
|
-
const result = randomInt(min, max);
|
|
8
|
-
expect(result).toBeGreaterThanOrEqual(min);
|
|
9
|
-
expect(result).toBeLessThan(8);
|
|
10
|
-
expect(Number.isInteger(result)).toBe(true);
|
|
11
|
-
});
|
|
12
|
-
it('should throw if the min value is larger than the max', () => {
|
|
13
|
-
expect(() => randomInt(10, 5)).toThrow();
|
|
14
|
-
});
|
|
15
|
-
it('should throw if the min value equals the max', () => {
|
|
16
|
-
expect(() => randomInt(10, 10)).toThrow();
|
|
17
|
-
});
|
|
18
|
-
it('should handle negative values', () => {
|
|
19
|
-
const min = -5;
|
|
20
|
-
const max = -3;
|
|
21
|
-
const result = randomInt(min, max);
|
|
22
|
-
expect(result).toBeGreaterThanOrEqual(min);
|
|
23
|
-
expect(result).toBeLessThan(8);
|
|
24
|
-
expect(Number.isInteger(result)).toBe(true);
|
|
25
|
-
expect(() => randomInt(max, min)).toThrow();
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
describe('randomDate', () => {
|
|
29
|
-
it('should return a date within the range', () => {
|
|
30
|
-
const start = new Date(2000, 0, 1);
|
|
31
|
-
const end = new Date();
|
|
32
|
-
const random = randomDate(start, end);
|
|
33
|
-
expect(random.getTime() >= start.getTime()).toBe(true);
|
|
34
|
-
expect(random.getTime() <= end.getTime()).toBe(true);
|
|
35
|
-
});
|
|
36
|
-
it('should throw if the end is before the start', () => {
|
|
37
|
-
expect(() => randomDate(new Date(), new Date(2000, 0, 1))).toThrow();
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
describe('randomValue', () => {
|
|
41
|
-
it('should throw if given an empty array', () => {
|
|
42
|
-
expect(() => randomValue([])).toThrow();
|
|
43
|
-
});
|
|
44
|
-
it('should return a value in the array', () => {
|
|
45
|
-
const arr = [-10, -20, -30];
|
|
46
|
-
expect(arr.includes(randomValue(arr)));
|
|
47
|
-
});
|
|
48
|
-
it('should not mutate the array', () => {
|
|
49
|
-
const arr = [-10, -20, -30];
|
|
50
|
-
randomValue(arr);
|
|
51
|
-
expect(arr).toMatchObject([-10, -20, -30]);
|
|
52
|
-
});
|
|
53
|
-
});
|
package/dist/range.test.d.ts
DELETED
package/dist/range.test.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"range.test.d.ts","sourceRoot":"","sources":["../src/range.test.ts"],"names":[],"mappings":""}
|
package/dist/range.test.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { range } from './range.js';
|
|
3
|
-
describe('range', () => {
|
|
4
|
-
it('should return an array equal in length to the range', () => {
|
|
5
|
-
const arr = range(10);
|
|
6
|
-
expect(arr.length).toBe(10);
|
|
7
|
-
});
|
|
8
|
-
it('should throw an error if the start is equal to the end', () => {
|
|
9
|
-
expect(() => range(1, 1)).toThrow();
|
|
10
|
-
});
|
|
11
|
-
});
|
package/dist/string.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"string.test.d.ts","sourceRoot":"","sources":["../src/string.test.ts"],"names":[],"mappings":""}
|
package/dist/string.test.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { camelToSnakeCase, capitalize, format, snakeToCamelCase, toLowerCase, toUpperCase, uncapitalize } from './string.js';
|
|
3
|
-
describe('camelToSnakeCase', () => {
|
|
4
|
-
it('should convert from camel to snake case ', () => {
|
|
5
|
-
expect(camelToSnakeCase('toSnakeCase')).toBe('to_snake_case');
|
|
6
|
-
expect(camelToSnakeCase('foo')).toBe('foo');
|
|
7
|
-
expect(camelToSnakeCase('')).toBe('');
|
|
8
|
-
});
|
|
9
|
-
});
|
|
10
|
-
describe('snakeToCamelCase', () => {
|
|
11
|
-
it('should convert from snake to camel case ', () => {
|
|
12
|
-
expect(snakeToCamelCase('to_camel_case')).toBe('toCamelCase');
|
|
13
|
-
expect(camelToSnakeCase('foo')).toBe('foo');
|
|
14
|
-
expect(camelToSnakeCase('')).toBe('');
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
|
-
describe('capitalize', () => {
|
|
18
|
-
it('should convert the first letter of the string to a capital letter', () => {
|
|
19
|
-
expect(capitalize('foo')).toBe('Foo');
|
|
20
|
-
expect(capitalize('FOO')).toBe('FOO');
|
|
21
|
-
expect(capitalize('foo bar')).toBe('Foo bar');
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
describe('uncapitalize', () => {
|
|
25
|
-
it('should convert the first letter of the string to a lowercase letter', () => {
|
|
26
|
-
expect(uncapitalize('Foo')).toBe('foo');
|
|
27
|
-
expect(uncapitalize('foo')).toBe('foo');
|
|
28
|
-
expect(uncapitalize('Foo bar')).toBe('foo bar');
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
describe('toLowerCase', () => {
|
|
32
|
-
it('should convert to lower case', () => {
|
|
33
|
-
expect(toLowerCase('FOO')).toBe('foo');
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
describe('toUpperCase', () => {
|
|
37
|
-
it('should convert to upper case', () => {
|
|
38
|
-
expect(toUpperCase('foo')).toBe('FOO');
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
describe('format', () => {
|
|
42
|
-
it('should return the same string if no positional args are given', () => {
|
|
43
|
-
expect(format('Hello')).toBe('Hello');
|
|
44
|
-
expect(format('Hello {}')).toBe('Hello {}');
|
|
45
|
-
});
|
|
46
|
-
it('should insert positional arguments', () => {
|
|
47
|
-
expect(format('Hello {}', 'World')).toBe('Hello World');
|
|
48
|
-
expect(format('Hello {}. {}.', 'World', 'This function works')).toBe('Hello World. This function works.');
|
|
49
|
-
});
|
|
50
|
-
it('should ignore additional indices', () => {
|
|
51
|
-
expect(format('Hello {}{}.', 'World')).toBe('Hello World{}.');
|
|
52
|
-
});
|
|
53
|
-
});
|