@serum-enterprises/json 2.0.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/LICENSE +21 -0
- package/README.md +60 -0
- package/build/JSON.js +81 -0
- package/package.json +29 -0
- package/src/JSON.ts +91 -0
- package/tests/JSON.test.js +131 -0
- package/tsconfig.json +29 -0
- package/types/JSON.d.ts +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Serum Enterprises L.L.C-FZ
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# JSON
|
|
2
|
+
|
|
3
|
+
Type-Safe drop-in replacement for the native JSON Object
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install --save @serum-enterprises/json
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import * as JSON from '@serum-enterprises/json';
|
|
15
|
+
|
|
16
|
+
const data: JSON.JSON = { name: 'John Doe', age: 25 };
|
|
17
|
+
|
|
18
|
+
function verify(data: JSON.JSON): boolean {
|
|
19
|
+
if(!JSON.isObject(data))
|
|
20
|
+
return false;
|
|
21
|
+
|
|
22
|
+
if(!JSON.isString(data.name) || !JSON.isInteger(data.age))
|
|
23
|
+
return false;
|
|
24
|
+
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if(verify(data)) {
|
|
29
|
+
console.log(`Data is correct: `);
|
|
30
|
+
console.log(JSON.stringify(data, null, 2));
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## API
|
|
35
|
+
|
|
36
|
+
Please check [JSON.d.ts](./types/JSON.d.ts) for the full API.
|
|
37
|
+
|
|
38
|
+
## LICENSE
|
|
39
|
+
|
|
40
|
+
MIT License
|
|
41
|
+
|
|
42
|
+
Copyright (c) 2025 Serum Enterprises L.L.C-FZ
|
|
43
|
+
|
|
44
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
45
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
46
|
+
in the Software without restriction, including without limitation the rights
|
|
47
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
48
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
49
|
+
furnished to do so, subject to the following conditions:
|
|
50
|
+
|
|
51
|
+
The above copyright notice and this permission notice shall be included in all
|
|
52
|
+
copies or substantial portions of the Software.
|
|
53
|
+
|
|
54
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
55
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
56
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
57
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
58
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
59
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
60
|
+
SOFTWARE.
|
package/build/JSON.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isNull = isNull;
|
|
4
|
+
exports.isBoolean = isBoolean;
|
|
5
|
+
exports.isNumber = isNumber;
|
|
6
|
+
exports.isInteger = isInteger;
|
|
7
|
+
exports.isString = isString;
|
|
8
|
+
exports.isShallowArray = isShallowArray;
|
|
9
|
+
exports.isArray = isArray;
|
|
10
|
+
exports.isShallowObject = isShallowObject;
|
|
11
|
+
exports.isObject = isObject;
|
|
12
|
+
exports.isPrimitive = isPrimitive;
|
|
13
|
+
exports.isShallowContainer = isShallowContainer;
|
|
14
|
+
exports.isContainer = isContainer;
|
|
15
|
+
exports.isShallowJSON = isShallowJSON;
|
|
16
|
+
exports.isJSON = isJSON;
|
|
17
|
+
exports.clone = clone;
|
|
18
|
+
exports.parse = parse;
|
|
19
|
+
exports.stringify = stringify;
|
|
20
|
+
function isNull(value) {
|
|
21
|
+
return value === null;
|
|
22
|
+
}
|
|
23
|
+
function isBoolean(value) {
|
|
24
|
+
return typeof value === 'boolean';
|
|
25
|
+
}
|
|
26
|
+
function isNumber(value) {
|
|
27
|
+
// Using Number.isFinite here as NaN, Infinity and -Infinity are not valid JSON Numbers
|
|
28
|
+
return Number.isFinite(value);
|
|
29
|
+
}
|
|
30
|
+
function isInteger(value) {
|
|
31
|
+
// Using Number.isSafeInteger here as JS Numbers are 64-bit floating point numbers and not all integers can be represented accurately
|
|
32
|
+
return Number.isSafeInteger(value);
|
|
33
|
+
}
|
|
34
|
+
function isString(value) {
|
|
35
|
+
return typeof value === 'string';
|
|
36
|
+
}
|
|
37
|
+
function isShallowArray(value) {
|
|
38
|
+
return Array.isArray(value);
|
|
39
|
+
}
|
|
40
|
+
function isArray(value) {
|
|
41
|
+
return isShallowArray(value) && value.every((v) => isJSON(v));
|
|
42
|
+
}
|
|
43
|
+
function isShallowObject(value) {
|
|
44
|
+
// Using Object.prototype.toString.call here as it is the most reliable way to check if something is an Object
|
|
45
|
+
return (Object.prototype.toString.call(value) === '[object Object]');
|
|
46
|
+
}
|
|
47
|
+
function isObject(value) {
|
|
48
|
+
return isShallowObject(value) && Object.values(value).every((v) => isJSON(v));
|
|
49
|
+
}
|
|
50
|
+
function isPrimitive(value) {
|
|
51
|
+
return isBoolean(value) || isNumber(value) || isString(value);
|
|
52
|
+
}
|
|
53
|
+
function isShallowContainer(value) {
|
|
54
|
+
return isShallowArray(value) || isShallowObject(value);
|
|
55
|
+
}
|
|
56
|
+
function isContainer(value) {
|
|
57
|
+
return isArray(value) || isObject(value);
|
|
58
|
+
}
|
|
59
|
+
function isShallowJSON(value) {
|
|
60
|
+
return isNull(value) || isPrimitive(value) || isShallowArray(value) || isShallowObject(value);
|
|
61
|
+
}
|
|
62
|
+
function isJSON(value) {
|
|
63
|
+
return isNull(value) || isPrimitive(value) || isContainer(value);
|
|
64
|
+
}
|
|
65
|
+
function clone(value) {
|
|
66
|
+
// Using Shallow Checks here as it is expected that value is JSON anyway, and this function is recursive anyway
|
|
67
|
+
if (isShallowObject(value))
|
|
68
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, clone(v)]));
|
|
69
|
+
else if (isShallowArray(value))
|
|
70
|
+
return value.map((v) => clone(v));
|
|
71
|
+
else
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
74
|
+
// Compatibility Method for the native JSON.parse
|
|
75
|
+
function parse(value) {
|
|
76
|
+
return globalThis.JSON.parse(value);
|
|
77
|
+
}
|
|
78
|
+
// Compatibility Method for the native JSON.stringify
|
|
79
|
+
function stringify(value) {
|
|
80
|
+
return globalThis.JSON.stringify(value);
|
|
81
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@serum-enterprises/json",
|
|
3
|
+
"version": "2.0.1",
|
|
4
|
+
"engines": {
|
|
5
|
+
"node": ">=14.0.0"
|
|
6
|
+
},
|
|
7
|
+
"description": "Type-Safe drop-in replacement for the native JSON Object",
|
|
8
|
+
"main": "./build/JSON.js",
|
|
9
|
+
"types": "./types/JSON.d.ts",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"test": "jest"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/Serum-Enterprises/SerumJS.git"
|
|
17
|
+
},
|
|
18
|
+
"author": "Philipp Steckenbauer <philipp@serum.enterprises>",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/Serum-Enterprises/SerumJS/issues"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/jest": "^29.5.14",
|
|
25
|
+
"@types/node": "^22.10.5",
|
|
26
|
+
"jest": "^29.7.0",
|
|
27
|
+
"typescript": "^5.7.3"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/JSON.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
export type Null = null;
|
|
2
|
+
export type Boolean = boolean;
|
|
3
|
+
export type Number = number;
|
|
4
|
+
export type Integer = number;
|
|
5
|
+
export type String = string;
|
|
6
|
+
export type Array = JSON[];
|
|
7
|
+
export type ShallowArray = unknown[];
|
|
8
|
+
export type Object = { [key: string]: JSON };
|
|
9
|
+
export type ShallowObject = { [key: string]: unknown };
|
|
10
|
+
export type Primitive = Boolean | Number | String;
|
|
11
|
+
export type Container = Array | Object;
|
|
12
|
+
export type JSON = Null | Primitive | Container;
|
|
13
|
+
|
|
14
|
+
export function isNull(value: unknown): value is Null {
|
|
15
|
+
return value === null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function isBoolean(value: unknown): value is Boolean {
|
|
19
|
+
return typeof value === 'boolean';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function isNumber(value: unknown): value is Number {
|
|
23
|
+
// Using Number.isFinite here as NaN, Infinity and -Infinity are not valid JSON Numbers
|
|
24
|
+
return Number.isFinite(value);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function isInteger(value: unknown): value is Integer {
|
|
28
|
+
// Using Number.isSafeInteger here as JS Numbers are 64-bit floating point numbers and not all integers can be represented accurately
|
|
29
|
+
return Number.isSafeInteger(value);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function isString(value: unknown): value is String {
|
|
33
|
+
return typeof value === 'string';
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function isShallowArray(value: unknown): value is ShallowArray {
|
|
37
|
+
return Array.isArray(value);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function isArray(value: unknown): value is Array {
|
|
41
|
+
return isShallowArray(value) && value.every((v) => isJSON(v));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function isShallowObject(value: unknown): value is ShallowObject {
|
|
45
|
+
// Using Object.prototype.toString.call here as it is the most reliable way to check if something is an Object
|
|
46
|
+
return (Object.prototype.toString.call(value) === '[object Object]');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function isObject(value: unknown): value is Object {
|
|
50
|
+
return isShallowObject(value) && Object.values(value).every((v) => isJSON(v));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function isPrimitive(value: unknown): value is Primitive {
|
|
54
|
+
return isBoolean(value) || isNumber(value) || isString(value);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function isShallowContainer(value: unknown): value is unknown[] | { [key: string]: unknown } {
|
|
58
|
+
return isShallowArray(value) || isShallowObject(value);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function isContainer(value: unknown): value is Container {
|
|
62
|
+
return isArray(value) || isObject(value);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function isShallowJSON(value: unknown): value is Null | Boolean | Number | String | unknown[] | { [key: string]: unknown } {
|
|
66
|
+
return isNull(value) || isPrimitive(value) || isShallowArray(value) || isShallowObject(value);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function isJSON(value: unknown): value is JSON {
|
|
70
|
+
return isNull(value) || isPrimitive(value) || isContainer(value);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function clone<T extends JSON = JSON>(value: T): T {
|
|
74
|
+
// Using Shallow Checks here as it is expected that value is JSON anyway, and this function is recursive anyway
|
|
75
|
+
if (isShallowObject(value))
|
|
76
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, clone(v as JSON)])) as T;
|
|
77
|
+
else if (isShallowArray(value))
|
|
78
|
+
return value.map((v) => clone(v)) as T;
|
|
79
|
+
else
|
|
80
|
+
return value;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Compatibility Method for the native JSON.parse
|
|
84
|
+
export function parse(value: string): JSON {
|
|
85
|
+
return globalThis.JSON.parse(value);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Compatibility Method for the native JSON.stringify
|
|
89
|
+
export function stringify(value: JSON): string {
|
|
90
|
+
return globalThis.JSON.stringify(value);
|
|
91
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
}
|
package/types/JSON.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export type Null = null;
|
|
2
|
+
export type Boolean = boolean;
|
|
3
|
+
export type Number = number;
|
|
4
|
+
export type Integer = number;
|
|
5
|
+
export type String = string;
|
|
6
|
+
export type Array = JSON[];
|
|
7
|
+
export type ShallowArray = unknown[];
|
|
8
|
+
export type Object = {
|
|
9
|
+
[key: string]: JSON;
|
|
10
|
+
};
|
|
11
|
+
export type ShallowObject = {
|
|
12
|
+
[key: string]: unknown;
|
|
13
|
+
};
|
|
14
|
+
export type Primitive = Boolean | Number | String;
|
|
15
|
+
export type Container = Array | Object;
|
|
16
|
+
export type JSON = Null | Primitive | Container;
|
|
17
|
+
export declare function isNull(value: unknown): value is Null;
|
|
18
|
+
export declare function isBoolean(value: unknown): value is Boolean;
|
|
19
|
+
export declare function isNumber(value: unknown): value is Number;
|
|
20
|
+
export declare function isInteger(value: unknown): value is Integer;
|
|
21
|
+
export declare function isString(value: unknown): value is String;
|
|
22
|
+
export declare function isShallowArray(value: unknown): value is ShallowArray;
|
|
23
|
+
export declare function isArray(value: unknown): value is Array;
|
|
24
|
+
export declare function isShallowObject(value: unknown): value is ShallowObject;
|
|
25
|
+
export declare function isObject(value: unknown): value is Object;
|
|
26
|
+
export declare function isPrimitive(value: unknown): value is Primitive;
|
|
27
|
+
export declare function isShallowContainer(value: unknown): value is unknown[] | {
|
|
28
|
+
[key: string]: unknown;
|
|
29
|
+
};
|
|
30
|
+
export declare function isContainer(value: unknown): value is Container;
|
|
31
|
+
export declare function isShallowJSON(value: unknown): value is Null | Boolean | Number | String | unknown[] | {
|
|
32
|
+
[key: string]: unknown;
|
|
33
|
+
};
|
|
34
|
+
export declare function isJSON(value: unknown): value is JSON;
|
|
35
|
+
export declare function clone<T extends JSON = JSON>(value: T): T;
|
|
36
|
+
export declare function parse(value: string): JSON;
|
|
37
|
+
export declare function stringify(value: JSON): string;
|