@serum-enterprises/json 2.0.1 → 3.0.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # JSON
2
2
 
3
- Type-Safe drop-in replacement for the native JSON Object
3
+ Type-Safe drop-in replacement for the native JSON
4
4
 
5
5
  ## Installation
6
6
 
@@ -11,22 +11,23 @@ npm install --save @serum-enterprises/json
11
11
  ## Usage
12
12
 
13
13
  ```typescript
14
- import * as JSON from '@serum-enterprises/json';
14
+ import {JSON} from '@serum-enterprises/json';
15
15
 
16
- const data: JSON.JSON = { name: 'John Doe', age: 25 };
16
+ const data: JSON = { name: 'John Doe', age: 25 };
17
+ const name: JSON.String = data.name;
17
18
 
18
- function verify(data: JSON.JSON): boolean {
19
+ function verify(data: JSON): boolean {
19
20
  if(!JSON.isObject(data))
20
21
  return false;
21
22
 
22
- if(!JSON.isString(data.name) || !JSON.isInteger(data.age))
23
+ if(!JSON.isString(data.name) || !JSON.isInteger(data.age))
23
24
  return false;
24
25
 
25
26
  return true;
26
27
  }
27
28
 
28
29
  if(verify(data)) {
29
- console.log(`Data is correct: `);
30
+ console.log(`Data for ${name} is correct: `);
30
31
  console.log(JSON.stringify(data, null, 2));
31
32
  }
32
33
  ```
package/build/JSON.js CHANGED
@@ -1,81 +1,85 @@
1
1
  "use strict";
2
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
- }
3
+ exports.JSON = void 0;
4
+ var JSON;
5
+ (function (JSON) {
6
+ function isNull(value) {
7
+ return value === null;
8
+ }
9
+ JSON.isNull = isNull;
10
+ function isBoolean(value) {
11
+ return typeof value === 'boolean';
12
+ }
13
+ JSON.isBoolean = isBoolean;
14
+ function isNumber(value) {
15
+ // Using Number.isFinite here as NaN, Infinity and -Infinity are not valid JSON Numbers
16
+ return Number.isFinite(value);
17
+ }
18
+ JSON.isNumber = isNumber;
19
+ function isInteger(value) {
20
+ // Using Number.isSafeInteger here as JS Numbers are 64-bit floating point numbers and not all integers can be represented accurately
21
+ return Number.isSafeInteger(value);
22
+ }
23
+ JSON.isInteger = isInteger;
24
+ function isString(value) {
25
+ return typeof value === 'string';
26
+ }
27
+ JSON.isString = isString;
28
+ function isShallowArray(value) {
29
+ return Array.isArray(value);
30
+ }
31
+ JSON.isShallowArray = isShallowArray;
32
+ function isArray(value) {
33
+ return isShallowArray(value) && value.every((v) => isJSON(v));
34
+ }
35
+ JSON.isArray = isArray;
36
+ function isShallowObject(value) {
37
+ // Using Object.prototype.toString.call here as it is the most reliable way to check if something is an Object
38
+ return (Object.prototype.toString.call(value) === '[object Object]');
39
+ }
40
+ JSON.isShallowObject = isShallowObject;
41
+ function isObject(value) {
42
+ return isShallowObject(value) && Object.values(value).every((v) => isJSON(v));
43
+ }
44
+ JSON.isObject = isObject;
45
+ function isPrimitive(value) {
46
+ return isBoolean(value) || isNumber(value) || isString(value);
47
+ }
48
+ JSON.isPrimitive = isPrimitive;
49
+ function isShallowContainer(value) {
50
+ return isShallowArray(value) || isShallowObject(value);
51
+ }
52
+ JSON.isShallowContainer = isShallowContainer;
53
+ function isContainer(value) {
54
+ return isArray(value) || isObject(value);
55
+ }
56
+ JSON.isContainer = isContainer;
57
+ function isShallowJSON(value) {
58
+ return isNull(value) || isPrimitive(value) || isShallowArray(value) || isShallowObject(value);
59
+ }
60
+ JSON.isShallowJSON = isShallowJSON;
61
+ function isJSON(value) {
62
+ return isNull(value) || isPrimitive(value) || isContainer(value);
63
+ }
64
+ JSON.isJSON = isJSON;
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
+ JSON.clone = clone;
75
+ // Compatibility Method for the native JSON.parse
76
+ function parse(value) {
77
+ return globalThis.JSON.parse(value);
78
+ }
79
+ JSON.parse = parse;
80
+ // Compatibility Method for the native JSON.stringify
81
+ function stringify(value) {
82
+ return globalThis.JSON.stringify(value);
83
+ }
84
+ JSON.stringify = stringify;
85
+ })(JSON || (exports.JSON = JSON = {}));
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@serum-enterprises/json",
3
- "version": "2.0.1",
3
+ "version": "3.0.0",
4
4
  "engines": {
5
- "node": ">=14.0.0"
5
+ "node": ">=24.0.0"
6
6
  },
7
- "description": "Type-Safe drop-in replacement for the native JSON Object",
7
+ "description": "Type-Safe drop-in replacement for native JSON",
8
8
  "main": "./build/JSON.js",
9
9
  "types": "./types/JSON.d.ts",
10
10
  "scripts": {
package/src/JSON.ts CHANGED
@@ -1,91 +1,96 @@
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);
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
+ }
91
96
  }
@@ -1,4 +1,4 @@
1
- const JSON = require('../build/JSON');
1
+ const {JSON} = require('../build/JSON');
2
2
 
3
3
  describe('Testing JSON', () => {
4
4
  test('Testing JSON.isNull', () => {
package/types/JSON.d.ts CHANGED
@@ -1,37 +1,40 @@
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;
1
+ export type JSON = JSON.JSON;
2
+ export declare namespace JSON {
3
+ type Null = null;
4
+ type Boolean = boolean;
5
+ type Number = number;
6
+ type Integer = number;
7
+ type String = string;
8
+ type Array = JSON[];
9
+ type ShallowArray = unknown[];
10
+ type Object = {
11
+ [key: string]: JSON;
12
+ };
13
+ type ShallowObject = {
14
+ [key: string]: unknown;
15
+ };
16
+ type Primitive = Boolean | Number | String;
17
+ type Container = Array | Object;
18
+ type JSON = Null | Primitive | Container;
19
+ function isNull(value: unknown): value is Null;
20
+ function isBoolean(value: unknown): value is Boolean;
21
+ function isNumber(value: unknown): value is Number;
22
+ function isInteger(value: unknown): value is Integer;
23
+ function isString(value: unknown): value is String;
24
+ function isShallowArray(value: unknown): value is ShallowArray;
25
+ function isArray(value: unknown): value is Array;
26
+ function isShallowObject(value: unknown): value is ShallowObject;
27
+ function isObject(value: unknown): value is Object;
28
+ function isPrimitive(value: unknown): value is Primitive;
29
+ function isShallowContainer(value: unknown): value is unknown[] | {
30
+ [key: string]: unknown;
31
+ };
32
+ function isContainer(value: unknown): value is Container;
33
+ function isShallowJSON(value: unknown): value is Null | Boolean | Number | String | unknown[] | {
34
+ [key: string]: unknown;
35
+ };
36
+ function isJSON(value: unknown): value is JSON;
37
+ function clone<T extends JSON = JSON>(value: T): T;
38
+ function parse(value: string): JSON;
39
+ function stringify(value: JSON): string;
40
+ }