@douglasneuroinformatics/libjs 1.0.0 → 1.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.
|
Binary file
|
package/dist/json.d.ts
CHANGED
package/dist/json.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"AA6BA,wBAAgB,QAAQ,CAAC,
|
|
1
|
+
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"AA6BA,wBAAgB,QAAQ,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,WAgBlE;AAED,wBAAgB,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,WAOhD"}
|
package/dist/json.js
CHANGED
|
@@ -5,7 +5,8 @@ function isSerializedObject(value, name) {
|
|
|
5
5
|
}
|
|
6
6
|
return Boolean(value.__isSerializedType && value.__deserializedType === name);
|
|
7
7
|
}
|
|
8
|
-
export function replacer(
|
|
8
|
+
export function replacer(key, value) {
|
|
9
|
+
console.log({ this: this });
|
|
9
10
|
if (value instanceof Set) {
|
|
10
11
|
return {
|
|
11
12
|
__deserializedType: 'Set',
|
|
@@ -13,11 +14,11 @@ export function replacer(_, value) {
|
|
|
13
14
|
value: Array.from(value)
|
|
14
15
|
};
|
|
15
16
|
}
|
|
16
|
-
else if (value instanceof Date) {
|
|
17
|
+
else if (typeof value === 'string' && isPlainObject(this) && this[key] instanceof Date) {
|
|
17
18
|
return {
|
|
18
19
|
__deserializedType: 'Date',
|
|
19
20
|
__isSerializedType: true,
|
|
20
|
-
value
|
|
21
|
+
value
|
|
21
22
|
};
|
|
22
23
|
}
|
|
23
24
|
return value;
|
package/dist/json.test.js
CHANGED
|
@@ -3,18 +3,20 @@ import { replacer, reviver } from './json.js';
|
|
|
3
3
|
describe('replacer', () => {
|
|
4
4
|
it('should serialize a Set', () => {
|
|
5
5
|
const set = new Set([1, 2, 3]);
|
|
6
|
-
expect(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
expect(JSON.parse(JSON.stringify({ set }, replacer))).toMatchObject({
|
|
7
|
+
set: {
|
|
8
|
+
__deserializedType: 'Set',
|
|
9
|
+
__isSerializedType: true,
|
|
10
|
+
value: [1, 2, 3]
|
|
11
|
+
}
|
|
10
12
|
});
|
|
11
13
|
});
|
|
12
14
|
it('should serialize a Date', () => {
|
|
13
15
|
const date = new Date();
|
|
14
|
-
expect(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
expect(JSON.parse(JSON.stringify({ date }, replacer))).toMatchObject({
|
|
17
|
+
date: {
|
|
18
|
+
__isSerializedType: true
|
|
19
|
+
}
|
|
18
20
|
});
|
|
19
21
|
});
|
|
20
22
|
it('should return the value if it is not a Set or a Date', () => {
|
|
@@ -40,6 +42,9 @@ describe('reviver', () => {
|
|
|
40
42
|
value: date.toJSON()
|
|
41
43
|
};
|
|
42
44
|
expect(reviver('', serializedDate)).toEqual(date);
|
|
45
|
+
expect(JSON.parse(JSON.stringify({ date: serializedDate }), reviver)).toMatchObject({
|
|
46
|
+
date: expect.any(Date)
|
|
47
|
+
});
|
|
43
48
|
});
|
|
44
49
|
it('should return the value if it is not a plain object', () => {
|
|
45
50
|
const obj = new Date();
|
package/package.json
CHANGED
|
Binary file
|