@douglasneuroinformatics/libjs 0.8.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":"AAeA,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,WASjD;AAED,wBAAgB,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,WAKhD"}
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
@@ -1,11 +1,12 @@
1
1
  import { isPlainObject } from './object.js';
2
- function isSerializedSet(value) {
2
+ function isSerializedObject(value, name) {
3
3
  if (!isPlainObject(value)) {
4
4
  return false;
5
5
  }
6
- return Boolean(value.__isSerializedType && value.__deserializedType === 'Set');
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,21 @@ export function replacer(_, value) {
13
14
  value: Array.from(value)
14
15
  };
15
16
  }
17
+ else if (typeof value === 'string' && isPlainObject(this) && this[key] instanceof Date) {
18
+ return {
19
+ __deserializedType: 'Date',
20
+ __isSerializedType: true,
21
+ value
22
+ };
23
+ }
16
24
  return value;
17
25
  }
18
26
  export function reviver(_, value) {
19
- if (isSerializedSet(value)) {
27
+ if (isSerializedObject(value, 'Set')) {
20
28
  return new Set(value.value);
21
29
  }
30
+ else if (isSerializedObject(value, 'Date')) {
31
+ return new Date(value.value);
32
+ }
22
33
  return value;
23
34
  }
package/dist/json.test.js CHANGED
@@ -3,19 +3,29 @@ 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
+ }
12
+ });
13
+ });
14
+ it('should serialize a Date', () => {
15
+ const date = new Date();
16
+ expect(JSON.parse(JSON.stringify({ date }, replacer))).toMatchObject({
17
+ date: {
18
+ __isSerializedType: true
19
+ }
10
20
  });
11
21
  });
12
- it('should return the value if it is not a Set', () => {
22
+ it('should return the value if it is not a Set or a Date', () => {
13
23
  const obj = { a: 1 };
14
24
  expect(replacer('', obj)).toBe(obj);
15
25
  });
16
26
  });
17
27
  describe('reviver', () => {
18
- it('should deserialize a serialized set', () => {
28
+ it('should deserialize a serialized Set', () => {
19
29
  const serializedSet = {
20
30
  __deserializedType: 'Set',
21
31
  __isSerializedType: true,
@@ -24,11 +34,23 @@ describe('reviver', () => {
24
34
  const deserializedSet = new Set([1, 2, 3]);
25
35
  expect(reviver('', serializedSet)).toEqual(deserializedSet);
26
36
  });
37
+ it('should deserialize a serialized Date', () => {
38
+ const date = new Date();
39
+ const serializedDate = {
40
+ __deserializedType: 'Date',
41
+ __isSerializedType: true,
42
+ value: date.toJSON()
43
+ };
44
+ expect(reviver('', serializedDate)).toEqual(date);
45
+ expect(JSON.parse(JSON.stringify({ date: serializedDate }), reviver)).toMatchObject({
46
+ date: expect.any(Date)
47
+ });
48
+ });
27
49
  it('should return the value if it is not a plain object', () => {
28
50
  const obj = new Date();
29
51
  expect(reviver('', obj)).toBe(obj);
30
52
  });
31
- it('should return the value if it is a plain object but not a serialized set', () => {
53
+ it('should return the value if it is a plain object but not a serialized Set or Date', () => {
32
54
  const obj = { a: 1 };
33
55
  expect(reviver('', obj)).toBe(obj);
34
56
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@douglasneuroinformatics/libjs",
3
3
  "type": "module",
4
- "version": "0.8.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",