@serum-enterprises/json 3.0.0 → 3.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 +2 -0
- package/package.json +3 -3
- package/types/{JSON.d.ts → index.d.ts} +0 -1
- package/src/JSON.ts +0 -96
- package/tests/JSON.test.js +0 -131
- package/tsconfig.json +0 -29
- /package/build/{JSON.js → index.js} +0 -0
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@serum-enterprises/json",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"engines": {
|
|
5
5
|
"node": ">=24.0.0"
|
|
6
6
|
},
|
|
7
7
|
"description": "Type-Safe drop-in replacement for native JSON",
|
|
8
|
-
"main": "./build/
|
|
9
|
-
"types": "./types/
|
|
8
|
+
"main": "./build/index.js",
|
|
9
|
+
"types": "./types/index.d.ts",
|
|
10
10
|
"scripts": {
|
|
11
11
|
"build": "tsc",
|
|
12
12
|
"test": "jest"
|
package/src/JSON.ts
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
export type JSON = JSON.JSON;
|
|
2
|
-
export namespace JSON {
|
|
3
|
-
export type Null = null;
|
|
4
|
-
export type Boolean = boolean;
|
|
5
|
-
export type Number = number;
|
|
6
|
-
export type Integer = number;
|
|
7
|
-
export type String = string;
|
|
8
|
-
export type Array = JSON[];
|
|
9
|
-
export type ShallowArray = unknown[];
|
|
10
|
-
export type Object = { [key: string]: JSON };
|
|
11
|
-
export type ShallowObject = { [key: string]: unknown };
|
|
12
|
-
export type Primitive = Boolean | Number | String;
|
|
13
|
-
export type Container = Array | Object;
|
|
14
|
-
export type JSON = Null | Primitive | Container;
|
|
15
|
-
|
|
16
|
-
export function isNull(value: unknown): value is Null {
|
|
17
|
-
return value === null;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function isBoolean(value: unknown): value is Boolean {
|
|
21
|
-
return typeof value === 'boolean';
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function isNumber(value: unknown): value is Number {
|
|
25
|
-
// Using Number.isFinite here as NaN, Infinity and -Infinity are not valid JSON Numbers
|
|
26
|
-
return Number.isFinite(value);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function isInteger(value: unknown): value is Integer {
|
|
30
|
-
// Using Number.isSafeInteger here as JS Numbers are 64-bit floating point numbers and not all integers can be represented accurately
|
|
31
|
-
return Number.isSafeInteger(value);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function isString(value: unknown): value is String {
|
|
35
|
-
return typeof value === 'string';
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function isShallowArray(value: unknown): value is ShallowArray {
|
|
39
|
-
return Array.isArray(value);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function isArray(value: unknown): value is Array {
|
|
43
|
-
return isShallowArray(value) && value.every((v) => isJSON(v));
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function isShallowObject(value: unknown): value is ShallowObject {
|
|
47
|
-
// Using Object.prototype.toString.call here as it is the most reliable way to check if something is an Object
|
|
48
|
-
return (Object.prototype.toString.call(value) === '[object Object]');
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function isObject(value: unknown): value is Object {
|
|
52
|
-
return isShallowObject(value) && Object.values(value).every((v) => isJSON(v));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function isPrimitive(value: unknown): value is Primitive {
|
|
56
|
-
return isBoolean(value) || isNumber(value) || isString(value);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function isShallowContainer(value: unknown): value is unknown[] | { [key: string]: unknown } {
|
|
60
|
-
return isShallowArray(value) || isShallowObject(value);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function isContainer(value: unknown): value is Container {
|
|
64
|
-
return isArray(value) || isObject(value);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export function isShallowJSON(value: unknown): value is Null | Boolean | Number | String | unknown[] | {
|
|
68
|
-
[key: string]: unknown
|
|
69
|
-
} {
|
|
70
|
-
return isNull(value) || isPrimitive(value) || isShallowArray(value) || isShallowObject(value);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export function isJSON(value: unknown): value is JSON {
|
|
74
|
-
return isNull(value) || isPrimitive(value) || isContainer(value);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export function clone<T extends JSON = JSON>(value: T): T {
|
|
78
|
-
// Using Shallow Checks here as it is expected that value is JSON anyway, and this function is recursive anyway
|
|
79
|
-
if (isShallowObject(value))
|
|
80
|
-
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, clone(v as JSON)])) as T;
|
|
81
|
-
else if (isShallowArray(value))
|
|
82
|
-
return value.map((v) => clone(v)) as T;
|
|
83
|
-
else
|
|
84
|
-
return value;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// Compatibility Method for the native JSON.parse
|
|
88
|
-
export function parse(value: string): JSON {
|
|
89
|
-
return globalThis.JSON.parse(value);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// Compatibility Method for the native JSON.stringify
|
|
93
|
-
export function stringify(value: JSON): string {
|
|
94
|
-
return globalThis.JSON.stringify(value);
|
|
95
|
-
}
|
|
96
|
-
}
|
package/tests/JSON.test.js
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
const {JSON} = require('../build/JSON');
|
|
2
|
-
|
|
3
|
-
describe('Testing JSON', () => {
|
|
4
|
-
test('Testing JSON.isNull', () => {
|
|
5
|
-
expect(JSON.isNull(null)).toBe(true);
|
|
6
|
-
expect(JSON.isNull(undefined)).toBe(false);
|
|
7
|
-
expect(JSON.isNull("Hello World")).toBe(false);
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
test('Testing JSON.isBoolean', () => {
|
|
11
|
-
expect(JSON.isBoolean(true)).toBe(true);
|
|
12
|
-
expect(JSON.isBoolean(false)).toBe(true);
|
|
13
|
-
expect(JSON.isBoolean("Hello World")).toBe(false);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
test('Testing JSON.isNumber', () => {
|
|
17
|
-
expect(JSON.isNumber(42)).toBe(true);
|
|
18
|
-
expect(JSON.isNumber(Infinity)).toBe(false);
|
|
19
|
-
expect(JSON.isNumber(NaN)).toBe(false);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test('Testing JSON.isInteger', () => {
|
|
23
|
-
expect(JSON.isInteger(42)).toBe(true);
|
|
24
|
-
expect(JSON.isInteger(42.1)).toBe(false);
|
|
25
|
-
expect(JSON.isInteger(Number.MAX_SAFE_INTEGER + 1)).toBe(false);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
test('Testing JSON.isString', () => {
|
|
29
|
-
expect(JSON.isString("Hello World")).toBe(true);
|
|
30
|
-
expect(JSON.isString(42)).toBe(false);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test('Testing JSON.isShallowArray', () => {
|
|
34
|
-
expect(JSON.isShallowArray([null, 1, "Hello", { key: "World" }])).toBe(true);
|
|
35
|
-
expect(JSON.isShallowArray([null, 1, "Hello World", undefined])).toBe(true);
|
|
36
|
-
expect(JSON.isShallowArray({})).toBe(false);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test('Testing JSON.isArray', () => {
|
|
40
|
-
expect(JSON.isArray([null, 1, "Hello", { key: "World" }])).toBe(true);
|
|
41
|
-
expect(JSON.isArray([null, 1, "Hello World", undefined])).toBe(false);
|
|
42
|
-
expect(JSON.isArray({})).toBe(false);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
test('Testing JSON.isShallowObject', () => {
|
|
46
|
-
expect(JSON.isShallowObject({ key: "Hello World", nested: { anotherKey: null } })).toBe(true);
|
|
47
|
-
expect(JSON.isShallowObject({ key: undefined })).toBe(true);
|
|
48
|
-
expect(JSON.isShallowObject([])).toBe(false);
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
test('Testing JSON.isObject', () => {
|
|
52
|
-
expect(JSON.isObject({ key: "Hello World", nested: { anotherKey: null } })).toBe(true);
|
|
53
|
-
expect(JSON.isObject({ key: undefined })).toBe(false);
|
|
54
|
-
expect(JSON.isShallowObject([])).toBe(false);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
test('Testing JSON.isPrimitive', () => {
|
|
58
|
-
expect(JSON.isPrimitive(null)).toBe(false);
|
|
59
|
-
expect(JSON.isPrimitive(true)).toBe(true);
|
|
60
|
-
expect(JSON.isPrimitive(42)).toBe(true);
|
|
61
|
-
expect(JSON.isPrimitive("Hello World")).toBe(true);
|
|
62
|
-
expect(JSON.isPrimitive([])).toBe(false);
|
|
63
|
-
expect(JSON.isPrimitive({})).toBe(false);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
test('Testing JSON.isShallowContainer', () => {
|
|
67
|
-
expect(JSON.isShallowContainer(null)).toBe(false);
|
|
68
|
-
expect(JSON.isShallowContainer(true)).toBe(false);
|
|
69
|
-
expect(JSON.isShallowContainer(42)).toBe(false);
|
|
70
|
-
expect(JSON.isShallowContainer("Hello World")).toBe(false);
|
|
71
|
-
expect(JSON.isShallowContainer([null, 1, "Hello", { key: "World" }])).toBe(true);
|
|
72
|
-
expect(JSON.isShallowContainer([null, 1, "Hello World", undefined])).toBe(true);
|
|
73
|
-
expect(JSON.isShallowContainer({ key: "Hello World", nested: { anotherKey: null } })).toBe(true);
|
|
74
|
-
expect(JSON.isShallowContainer({ key: undefined })).toBe(true);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
test('Testing JSON.isContainer', () => {
|
|
78
|
-
expect(JSON.isContainer(null)).toBe(false);
|
|
79
|
-
expect(JSON.isContainer(true)).toBe(false);
|
|
80
|
-
expect(JSON.isContainer(42)).toBe(false);
|
|
81
|
-
expect(JSON.isContainer("Hello World")).toBe(false);
|
|
82
|
-
expect(JSON.isContainer([null, 1, "Hello", { key: "World" }])).toBe(true);
|
|
83
|
-
expect(JSON.isContainer([null, 1, "Hello World", undefined])).toBe(false);
|
|
84
|
-
expect(JSON.isContainer({ key: "Hello World", nested: { anotherKey: null } })).toBe(true);
|
|
85
|
-
expect(JSON.isContainer({ key: undefined })).toBe(false);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
test('Testing JSON.isShallowJSON', () => {
|
|
89
|
-
expect(JSON.isShallowJSON(null)).toBe(true);
|
|
90
|
-
expect(JSON.isShallowJSON(true)).toBe(true);
|
|
91
|
-
expect(JSON.isShallowJSON(42)).toBe(true);
|
|
92
|
-
expect(JSON.isShallowJSON("Hello World")).toBe(true);
|
|
93
|
-
expect(JSON.isShallowJSON([null, 1, "Hello", { key: "World" }])).toBe(true);
|
|
94
|
-
expect(JSON.isShallowJSON([null, 1, "Hello World", undefined])).toBe(true);
|
|
95
|
-
expect(JSON.isShallowJSON({ key: "Hello World", nested: { anotherKey: null } })).toBe(true);
|
|
96
|
-
expect(JSON.isShallowJSON({ key: undefined })).toBe(true);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
test('Testing JSON.isJSON', () => {
|
|
100
|
-
expect(JSON.isJSON(null)).toBe(true);
|
|
101
|
-
expect(JSON.isJSON(true)).toBe(true);
|
|
102
|
-
expect(JSON.isJSON(42)).toBe(true);
|
|
103
|
-
expect(JSON.isJSON("Hello World")).toBe(true);
|
|
104
|
-
expect(JSON.isJSON([null, 1, "Hello", { key: "World" }])).toBe(true);
|
|
105
|
-
expect(JSON.isJSON([null, 1, "Hello World", undefined])).toBe(false);
|
|
106
|
-
expect(JSON.isJSON({ key: "Hello World", nested: { anotherKey: null } })).toBe(true);
|
|
107
|
-
expect(JSON.isJSON({ key: undefined })).toBe(false);
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
test('Testing JSON.clone', () => {
|
|
111
|
-
const obj = { key: "Hello World", nested: [1, 2, { deep: true }] };
|
|
112
|
-
const clonedObj = JSON.clone(obj);
|
|
113
|
-
|
|
114
|
-
expect(clonedObj).toEqual(obj);
|
|
115
|
-
expect(clonedObj).not.toBe(obj);
|
|
116
|
-
expect(clonedObj.nested).not.toBe(obj.nested);
|
|
117
|
-
expect(clonedObj.nested[2]).not.toBe(obj.nested[2]);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
test('Testing JSON.stringify', () => {
|
|
121
|
-
const data = { key: "Hello World", nested: [1, 2, { deep: true }] };
|
|
122
|
-
|
|
123
|
-
expect(JSON.stringify(data)).toBe('{"key":"Hello World","nested":[1,2,{"deep":true}]}');
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
test('Testing JSON.parse', () => {
|
|
127
|
-
const data = '{"key":"Hello World","nested":[1,2,{"deep":true}]}'
|
|
128
|
-
|
|
129
|
-
expect(JSON.parse(data)).toStrictEqual({ key: "Hello World", nested: [1, 2, { deep: true }] });
|
|
130
|
-
});
|
|
131
|
-
});
|
package/tsconfig.json
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2024", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
|
4
|
-
"module": "Node16", /* Specify what module code is generated. */
|
|
5
|
-
"rootDir": "./src", /* Specify the root folder within your source files. */
|
|
6
|
-
"outDir": "./build", /* Specify an output folder for all emitted files. */
|
|
7
|
-
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
8
|
-
"declarationDir": "./types", /* Specify the output directory for generated declaration files. */
|
|
9
|
-
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
|
10
|
-
/* Type Checking */
|
|
11
|
-
"strict": true, /* Enable all strict type-checking options. */
|
|
12
|
-
"noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
13
|
-
"strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
14
|
-
"strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
15
|
-
"strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
16
|
-
"strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
17
|
-
"noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
18
|
-
"useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
19
|
-
"alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
20
|
-
"noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
21
|
-
"noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
22
|
-
"exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
23
|
-
"noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
24
|
-
"noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
25
|
-
"noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
|
26
|
-
"noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
27
|
-
"noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
28
|
-
}
|
|
29
|
-
}
|
|
File without changes
|