@douglasneuroinformatics/libjs 0.5.0 → 0.6.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/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './array.js';
2
2
  export * from './datetime.js';
3
+ export * from './json.js';
3
4
  export * from './number.js';
4
5
  export * from './object.js';
5
6
  export * from './random.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './array.js';
2
2
  export * from './datetime.js';
3
+ export * from './json.js';
3
4
  export * from './number.js';
4
5
  export * from './object.js';
5
6
  export * from './random.js';
package/dist/json.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export declare function replacer(_: string, value: unknown): unknown;
2
+ export declare function reviver(_: string, value: unknown): unknown;
3
+ //# sourceMappingURL=json.d.ts.map
@@ -0,0 +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"}
package/dist/json.js ADDED
@@ -0,0 +1,23 @@
1
+ import { isPlainObject } from './object.js';
2
+ function isSerializedSet(value) {
3
+ if (!isPlainObject(value)) {
4
+ return false;
5
+ }
6
+ return Boolean(value.__isSerializedType && value.__deserializedType === 'Set');
7
+ }
8
+ export function replacer(_, value) {
9
+ if (value instanceof Set) {
10
+ return {
11
+ __deserializedType: 'Set',
12
+ __isSerializedType: true,
13
+ value: Array.from(value)
14
+ };
15
+ }
16
+ return value;
17
+ }
18
+ export function reviver(_, value) {
19
+ if (isSerializedSet(value)) {
20
+ return new Set(value.value);
21
+ }
22
+ return value;
23
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=json.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json.test.d.ts","sourceRoot":"","sources":["../src/json.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,35 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { replacer, reviver } from './json.js';
3
+ describe('replacer', () => {
4
+ it('should serialize a Set', () => {
5
+ const set = new Set([1, 2, 3]);
6
+ expect(replacer('', set)).toEqual({
7
+ __deserializedType: 'Set',
8
+ __isSerializedType: true,
9
+ value: [1, 2, 3]
10
+ });
11
+ });
12
+ it('should return the value if it is not a Set', () => {
13
+ const obj = { a: 1 };
14
+ expect(replacer('', obj)).toBe(obj);
15
+ });
16
+ });
17
+ describe('reviver', () => {
18
+ it('should deserialize a serialized set', () => {
19
+ const serializedSet = {
20
+ __deserializedType: 'Set',
21
+ __isSerializedType: true,
22
+ value: [1, 2, 3]
23
+ };
24
+ const deserializedSet = new Set([1, 2, 3]);
25
+ expect(reviver('', serializedSet)).toEqual(deserializedSet);
26
+ });
27
+ it('should return the value if it is not a plain object', () => {
28
+ const obj = new Date();
29
+ expect(reviver('', obj)).toBe(obj);
30
+ });
31
+ it('should return the value if it is a plain object but not a serialized set', () => {
32
+ const obj = { a: 1 };
33
+ expect(reviver('', obj)).toBe(obj);
34
+ });
35
+ });
@@ -15,6 +15,8 @@ const NUMBER_LIKE_VALUES = [
15
15
  '0.0',
16
16
  '+0',
17
17
  '3.14',
18
+ '1.',
19
+ '.1',
18
20
  Number.MAX_VALUE,
19
21
  Number.MIN_VALUE,
20
22
  Infinity,
@@ -24,6 +26,7 @@ const NUMBER_LIKE_VALUES = [
24
26
  ' -Infinity '
25
27
  ];
26
28
  const NON_NUMBER_LIKE_VALUES = [
29
+ '1..',
27
30
  '--5',
28
31
  'A5',
29
32
  '5A',
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@douglasneuroinformatics/libjs",
3
3
  "type": "module",
4
- "version": "0.5.0",
4
+ "version": "0.6.0",
5
5
  "packageManager": "pnpm@9.3.0",
6
6
  "description": "A collection of utility functions and types for Node.js and the browser",
7
7
  "author": "Joshua Unrau",