@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.
package/dist/json.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export declare function replacer(_: string, value: unknown): unknown;
1
+ export declare function replacer(this: unknown, key: string, value: unknown): unknown;
2
2
  export declare function reviver(_: string, value: unknown): unknown;
3
3
  //# sourceMappingURL=json.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"AA6BA,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,WAejD;AAED,wBAAgB,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,WAOhD"}
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(_, value) {
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: value.toJSON()
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(replacer('', set)).toEqual({
7
- __deserializedType: 'Set',
8
- __isSerializedType: true,
9
- value: [1, 2, 3]
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(replacer('', date)).toEqual({
15
- __deserializedType: 'Date',
16
- __isSerializedType: true,
17
- value: date.toJSON()
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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@douglasneuroinformatics/libjs",
3
3
  "type": "module",
4
- "version": "1.0.0",
4
+ "version": "1.0.1",
5
5
  "packageManager": "pnpm@9.11.0",
6
6
  "description": "A collection of utility functions and types for Node.js and the browser",
7
7
  "author": "Joshua Unrau",