@awsless/json 0.0.7 → 0.0.9

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
@@ -24,6 +24,7 @@ JSON doesn't have support for types like:
24
24
  - `Date`
25
25
  - `BigInt`
26
26
  - `BigFloat` - npm package @awsless/bit-float
27
+ - `Duration` - npm package @awsless/duration
27
28
 
28
29
  Having to encode & decode these type of values can get quite annoying. We try to solve this problem by encoding these types using valid JSON syntax.
29
30
 
@@ -117,16 +118,3 @@ console.log(eq(value, result)) // true
117
118
  ### Don't use the `$` character inside your JSON.
118
119
 
119
120
  We use the `$` character to encode our special types inside JSON. In order to prevent parsing errors we recommend to avoid using the `$` character inside your object property names.
120
-
121
- ### Object properties with `undefined` as value type will be stripped away.
122
-
123
- ```ts
124
- // Will result in an empty object.
125
- const result = parse(stringify({ key: undefined }))
126
-
127
- // Will log false.
128
- console.log('key' in result)
129
-
130
- // Will log true.
131
- console.log(result.key === undefined)
132
- ```
package/dist/index.cjs CHANGED
@@ -18,8 +18,8 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
 
20
20
  // src/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
23
  createReplacer: () => createReplacer,
24
24
  createReviver: () => createReviver,
25
25
  createSafeNumberReplacer: () => createSafeNumberReplacer,
@@ -31,7 +31,7 @@ __export(src_exports, {
31
31
  stringify: () => stringify,
32
32
  unpatch: () => unpatch
33
33
  });
34
- module.exports = __toCommonJS(src_exports);
34
+ module.exports = __toCommonJS(index_exports);
35
35
 
36
36
  // src/type/bigfloat.ts
37
37
  var import_big_float = require("@awsless/big-float");
@@ -64,10 +64,24 @@ var $infinity = {
64
64
  stringify: (v) => v > 0 ? 1 : 0
65
65
  };
66
66
 
67
+ // src/type/undefined.ts
68
+ var $undefined = {
69
+ is: (v) => typeof v === "undefined",
70
+ replace: (_) => void 0,
71
+ stringify: (_) => 0
72
+ };
73
+ var isUndefined = (value) => {
74
+ return typeof value === "object" && value !== null && Object.keys(value).length === 1 && "$undefined" in value && value.$undefined === 0;
75
+ };
76
+
67
77
  // src/type/map.ts
68
78
  var $map = {
69
79
  is: (v) => v instanceof Map,
70
- parse: (v) => new Map(v),
80
+ parse: (v) => new Map(
81
+ v.map((pair) => {
82
+ return pair.map((i) => isUndefined(i) ? void 0 : i);
83
+ })
84
+ ),
71
85
  stringify: (v) => Array.from(v)
72
86
  };
73
87
 
@@ -88,7 +102,7 @@ var $regexp = {
88
102
  // src/type/set.ts
89
103
  var $set = {
90
104
  is: (v) => v instanceof Set,
91
- parse: (v) => new Set(v),
105
+ parse: (v) => new Set(v.map((i) => isUndefined(i) ? void 0 : i)),
92
106
  stringify: (v) => Array.from(v)
93
107
  };
94
108
 
@@ -99,13 +113,6 @@ var $binary = {
99
113
  stringify: (v) => btoa(String.fromCharCode(...v))
100
114
  };
101
115
 
102
- // src/type/undefined.ts
103
- var $undefined = {
104
- is: (v) => typeof v === "undefined",
105
- parse: (_) => void 0,
106
- stringify: (_) => 0
107
- };
108
-
109
116
  // src/type/url.ts
110
117
  var $url = {
111
118
  is: (v) => v instanceof URL,
@@ -113,9 +120,18 @@ var $url = {
113
120
  stringify: (v) => v.toString()
114
121
  };
115
122
 
123
+ // src/type/duration.ts
124
+ var import_duration = require("@awsless/duration");
125
+ var $duration = {
126
+ is: (v) => v instanceof import_duration.Duration,
127
+ parse: (v) => new import_duration.Duration(BigInt(v)),
128
+ stringify: (v) => v.value.toString()
129
+ };
130
+
116
131
  // src/type/index.ts
117
132
  var baseTypes = {
118
133
  $undefined,
134
+ $duration,
119
135
  $infinity,
120
136
  $bigfloat,
121
137
  $bigint,
@@ -130,9 +146,19 @@ var baseTypes = {
130
146
 
131
147
  // src/parse.ts
132
148
  var parse = (json, types = {}) => {
133
- return JSON.parse(json, createReviver(types));
149
+ const replacements = [];
150
+ const result = JSON.parse(
151
+ json,
152
+ createReviver(types, (target, key, value) => {
153
+ replacements.push([target, key, value]);
154
+ })
155
+ );
156
+ for (const [target, key, value] of replacements) {
157
+ target[key] = value;
158
+ }
159
+ return result;
134
160
  };
135
- var createReviver = (types = {}) => {
161
+ var createReviver = (types = {}, registerReplacement) => {
136
162
  types = {
137
163
  ...baseTypes,
138
164
  ...types
@@ -145,7 +171,16 @@ var createReviver = (types = {}) => {
145
171
  const typeName = keys[0];
146
172
  if (typeName in types && types[typeName]) {
147
173
  const type = types[typeName];
148
- return type.parse(original[typeName]);
174
+ const stringified = original[typeName];
175
+ if ("parse" in type) {
176
+ return type.parse(stringified);
177
+ } else if (registerReplacement) {
178
+ const result = type.replace(stringified);
179
+ registerReplacement(this, key, result);
180
+ return result;
181
+ } else {
182
+ return type.replace(stringified);
183
+ }
149
184
  }
150
185
  }
151
186
  }
package/dist/index.d.cts CHANGED
@@ -1,8 +1,11 @@
1
1
  type Serializable<I, O> = {
2
2
  is: (value: unknown) => boolean;
3
- parse: (value: O) => I;
4
3
  stringify: (value: I) => O;
5
- };
4
+ } & ({
5
+ parse: (value: O) => I;
6
+ } | {
7
+ replace: (value: O) => I;
8
+ });
6
9
  type SerializableTypes = Record<string, Serializable<any, any>>;
7
10
 
8
11
  declare const patch: (value: unknown, types?: SerializableTypes) => any;
@@ -10,7 +13,7 @@ declare const unpatch: (value: unknown, types?: SerializableTypes) => any;
10
13
 
11
14
  declare const parse: (json: string, types?: SerializableTypes) => any;
12
15
  type Reviver$1 = (this: any, key: string, value: any) => any;
13
- declare const createReviver: (types?: SerializableTypes) => Reviver$1;
16
+ declare const createReviver: (types?: SerializableTypes, registerReplacement?: (target: any, key: string, value: unknown) => void) => Reviver$1;
14
17
 
15
18
  declare const stringify: (value: unknown, types?: SerializableTypes) => string;
16
19
  type Replacer$1 = (this: any, key: string, value: any) => any;
package/dist/index.d.ts CHANGED
@@ -1,8 +1,11 @@
1
1
  type Serializable<I, O> = {
2
2
  is: (value: unknown) => boolean;
3
- parse: (value: O) => I;
4
3
  stringify: (value: I) => O;
5
- };
4
+ } & ({
5
+ parse: (value: O) => I;
6
+ } | {
7
+ replace: (value: O) => I;
8
+ });
6
9
  type SerializableTypes = Record<string, Serializable<any, any>>;
7
10
 
8
11
  declare const patch: (value: unknown, types?: SerializableTypes) => any;
@@ -10,7 +13,7 @@ declare const unpatch: (value: unknown, types?: SerializableTypes) => any;
10
13
 
11
14
  declare const parse: (json: string, types?: SerializableTypes) => any;
12
15
  type Reviver$1 = (this: any, key: string, value: any) => any;
13
- declare const createReviver: (types?: SerializableTypes) => Reviver$1;
16
+ declare const createReviver: (types?: SerializableTypes, registerReplacement?: (target: any, key: string, value: unknown) => void) => Reviver$1;
14
17
 
15
18
  declare const stringify: (value: unknown, types?: SerializableTypes) => string;
16
19
  type Replacer$1 = (this: any, key: string, value: any) => any;
package/dist/index.js CHANGED
@@ -29,10 +29,24 @@ var $infinity = {
29
29
  stringify: (v) => v > 0 ? 1 : 0
30
30
  };
31
31
 
32
+ // src/type/undefined.ts
33
+ var $undefined = {
34
+ is: (v) => typeof v === "undefined",
35
+ replace: (_) => void 0,
36
+ stringify: (_) => 0
37
+ };
38
+ var isUndefined = (value) => {
39
+ return typeof value === "object" && value !== null && Object.keys(value).length === 1 && "$undefined" in value && value.$undefined === 0;
40
+ };
41
+
32
42
  // src/type/map.ts
33
43
  var $map = {
34
44
  is: (v) => v instanceof Map,
35
- parse: (v) => new Map(v),
45
+ parse: (v) => new Map(
46
+ v.map((pair) => {
47
+ return pair.map((i) => isUndefined(i) ? void 0 : i);
48
+ })
49
+ ),
36
50
  stringify: (v) => Array.from(v)
37
51
  };
38
52
 
@@ -53,7 +67,7 @@ var $regexp = {
53
67
  // src/type/set.ts
54
68
  var $set = {
55
69
  is: (v) => v instanceof Set,
56
- parse: (v) => new Set(v),
70
+ parse: (v) => new Set(v.map((i) => isUndefined(i) ? void 0 : i)),
57
71
  stringify: (v) => Array.from(v)
58
72
  };
59
73
 
@@ -64,13 +78,6 @@ var $binary = {
64
78
  stringify: (v) => btoa(String.fromCharCode(...v))
65
79
  };
66
80
 
67
- // src/type/undefined.ts
68
- var $undefined = {
69
- is: (v) => typeof v === "undefined",
70
- parse: (_) => void 0,
71
- stringify: (_) => 0
72
- };
73
-
74
81
  // src/type/url.ts
75
82
  var $url = {
76
83
  is: (v) => v instanceof URL,
@@ -78,9 +85,18 @@ var $url = {
78
85
  stringify: (v) => v.toString()
79
86
  };
80
87
 
88
+ // src/type/duration.ts
89
+ import { Duration } from "@awsless/duration";
90
+ var $duration = {
91
+ is: (v) => v instanceof Duration,
92
+ parse: (v) => new Duration(BigInt(v)),
93
+ stringify: (v) => v.value.toString()
94
+ };
95
+
81
96
  // src/type/index.ts
82
97
  var baseTypes = {
83
98
  $undefined,
99
+ $duration,
84
100
  $infinity,
85
101
  $bigfloat,
86
102
  $bigint,
@@ -95,9 +111,19 @@ var baseTypes = {
95
111
 
96
112
  // src/parse.ts
97
113
  var parse = (json, types = {}) => {
98
- return JSON.parse(json, createReviver(types));
114
+ const replacements = [];
115
+ const result = JSON.parse(
116
+ json,
117
+ createReviver(types, (target, key, value) => {
118
+ replacements.push([target, key, value]);
119
+ })
120
+ );
121
+ for (const [target, key, value] of replacements) {
122
+ target[key] = value;
123
+ }
124
+ return result;
99
125
  };
100
- var createReviver = (types = {}) => {
126
+ var createReviver = (types = {}, registerReplacement) => {
101
127
  types = {
102
128
  ...baseTypes,
103
129
  ...types
@@ -110,7 +136,16 @@ var createReviver = (types = {}) => {
110
136
  const typeName = keys[0];
111
137
  if (typeName in types && types[typeName]) {
112
138
  const type = types[typeName];
113
- return type.parse(original[typeName]);
139
+ const stringified = original[typeName];
140
+ if ("parse" in type) {
141
+ return type.parse(stringified);
142
+ } else if (registerReplacement) {
143
+ const result = type.replace(stringified);
144
+ registerReplacement(this, key, result);
145
+ return result;
146
+ } else {
147
+ return type.replace(stringified);
148
+ }
114
149
  }
115
150
  }
116
151
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@awsless/json",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -17,7 +17,8 @@
17
17
  "RegExp",
18
18
  "Uint8Array",
19
19
  "Binary",
20
- "undefined"
20
+ "undefined",
21
+ "extendable"
21
22
  ],
22
23
  "repository": {
23
24
  "type": "git",
@@ -40,10 +41,12 @@
40
41
  }
41
42
  },
42
43
  "peerDependencies": {
43
- "@awsless/big-float": "^0.0.4"
44
+ "@awsless/big-float": "^0.0.6",
45
+ "@awsless/duration": "^0.0.3"
44
46
  },
45
47
  "devDependencies": {
46
- "@awsless/big-float": "^0.0.4"
48
+ "@awsless/big-float": "^0.0.6",
49
+ "@awsless/duration": "^0.0.3"
47
50
  },
48
51
  "scripts": {
49
52
  "test": "pnpm code test",