@douglasneuroinformatics/libjs 0.7.1 → 1.0.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.
@@ -1,3 +1,12 @@
1
+ export type Duration = {
2
+ days: number;
3
+ hours: number;
4
+ milliseconds: number;
5
+ minutes: number;
6
+ seconds: number;
7
+ };
8
+ /** A map that associates time units with their corresponding values in milliseconds */
9
+ export declare const TIME_MAP: Map<"day" | "hour" | "minute" | "second" | "millisecond", number>;
1
10
  /**
2
11
  * Returns the data in basic ISO format, e.g., yyyy-mm-dd
3
12
  * @example
@@ -25,4 +34,12 @@ export declare function yearsPassed(date: Date): number;
25
34
  * ```
26
35
  */
27
36
  export declare function sleep(seconds: number): Promise<unknown>;
37
+ /**
38
+ * Parses a given duration in milliseconds into an object representing the duration in days, hours, minutes, seconds, and milliseconds.
39
+ *
40
+ * @param milliseconds - The duration in milliseconds to be parsed.
41
+ * @returns An object of type `Duration` representing the parsed duration.
42
+ * @throws Will throw an error if the input duration is negative.
43
+ */
44
+ export declare function parseDuration(milliseconds: number): Duration;
28
45
  //# sourceMappingURL=datetime.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"datetime.d.ts","sourceRoot":"","sources":["../src/datetime.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAEnD;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAE9C;AAED;;;;;;GAMG;AACH,wBAAsB,KAAK,CAAC,OAAO,EAAE,MAAM,oBAE1C"}
1
+ {"version":3,"file":"datetime.d.ts","sourceRoot":"","sources":["../src/datetime.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,QAAQ,GAAG;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,uFAAuF;AACvF,eAAO,MAAM,QAAQ,mEAMV,CAAC;AAEZ;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAEnD;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAE9C;AAED;;;;;;GAMG;AACH,wBAAsB,KAAK,CAAC,OAAO,EAAE,MAAM,oBAE1C;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,YAAY,EAAE,MAAM,GAAG,QAAQ,CAY5D"}
package/dist/datetime.js CHANGED
@@ -1,3 +1,11 @@
1
+ /** A map that associates time units with their corresponding values in milliseconds */
2
+ export const TIME_MAP = new Map([
3
+ ['day', 24 * 60 * 60 * 1000],
4
+ ['hour', 60 * 60 * 1000],
5
+ ['minute', 60 * 1000],
6
+ ['second', 1000],
7
+ ['millisecond', 1]
8
+ ]);
1
9
  /**
2
10
  * Returns the data in basic ISO format, e.g., yyyy-mm-dd
3
11
  * @example
@@ -31,3 +39,23 @@ export function yearsPassed(date) {
31
39
  export async function sleep(seconds) {
32
40
  return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
33
41
  }
42
+ /**
43
+ * Parses a given duration in milliseconds into an object representing the duration in days, hours, minutes, seconds, and milliseconds.
44
+ *
45
+ * @param milliseconds - The duration in milliseconds to be parsed.
46
+ * @returns An object of type `Duration` representing the parsed duration.
47
+ * @throws Will throw an error if the input duration is negative.
48
+ */
49
+ export function parseDuration(milliseconds) {
50
+ if (0 > milliseconds) {
51
+ throw new Error(`Cannot parse negative length of time: ${milliseconds}`);
52
+ }
53
+ const duration = {};
54
+ let remaining = milliseconds;
55
+ TIME_MAP.forEach((value, unit) => {
56
+ const count = Math.floor(remaining / value);
57
+ duration[`${unit}s`] = count;
58
+ remaining %= value;
59
+ });
60
+ return duration;
61
+ }
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from 'vitest';
2
- import { sleep, toBasicISOString, yearsPassed } from './datetime.js';
2
+ import { parseDuration, sleep, toBasicISOString, yearsPassed } from './datetime.js';
3
3
  describe('toBasicISOString', () => {
4
4
  it('should return a string of the format yyyy-mm-dd', () => {
5
5
  expect(toBasicISOString(new Date(2000, 0, 1))).toBe('2000-01-01');
@@ -25,3 +25,83 @@ describe('sleep', () => {
25
25
  expect(end - start).toBeGreaterThanOrEqual(200);
26
26
  });
27
27
  });
28
+ describe('parseDuration', () => {
29
+ it('should fail to parse a negative duration', () => {
30
+ expect(() => parseDuration(-1)).toThrow('Cannot parse negative length of time: -1');
31
+ });
32
+ it('should parse a duration of zero', () => {
33
+ expect(parseDuration(0)).toEqual({
34
+ days: 0,
35
+ hours: 0,
36
+ milliseconds: 0,
37
+ minutes: 0,
38
+ seconds: 0
39
+ });
40
+ });
41
+ it('should parse a duration less than a second', () => {
42
+ expect(parseDuration(50)).toEqual({
43
+ days: 0,
44
+ hours: 0,
45
+ milliseconds: 50,
46
+ minutes: 0,
47
+ seconds: 0
48
+ });
49
+ expect(parseDuration(500)).toEqual({
50
+ days: 0,
51
+ hours: 0,
52
+ milliseconds: 500,
53
+ minutes: 0,
54
+ seconds: 0
55
+ });
56
+ });
57
+ it('should parse a duration less than one minute but more than one second', () => {
58
+ expect(parseDuration(11_100)).toEqual({
59
+ days: 0,
60
+ hours: 0,
61
+ milliseconds: 100,
62
+ minutes: 0,
63
+ seconds: 11
64
+ });
65
+ });
66
+ it('should parse a duration less than one hour but more than one minute', () => {
67
+ expect(parseDuration(60_000)).toEqual({
68
+ days: 0,
69
+ hours: 0,
70
+ milliseconds: 0,
71
+ minutes: 1,
72
+ seconds: 0
73
+ });
74
+ expect(parseDuration(62_500)).toEqual({
75
+ days: 0,
76
+ hours: 0,
77
+ milliseconds: 500,
78
+ minutes: 1,
79
+ seconds: 2
80
+ });
81
+ });
82
+ it('should parse a duration less than one day but more than one hour', () => {
83
+ expect(parseDuration(3_600_000)).toEqual({
84
+ days: 0,
85
+ hours: 1,
86
+ milliseconds: 0,
87
+ minutes: 0,
88
+ seconds: 0
89
+ });
90
+ });
91
+ it('should parse a duration greater than one day', () => {
92
+ expect(parseDuration(86_400_000)).toEqual({
93
+ days: 1,
94
+ hours: 0,
95
+ milliseconds: 0,
96
+ minutes: 0,
97
+ seconds: 0
98
+ });
99
+ expect(parseDuration(4_351_505_030)).toEqual({
100
+ days: 50,
101
+ hours: 8,
102
+ milliseconds: 30,
103
+ minutes: 45,
104
+ seconds: 5
105
+ });
106
+ });
107
+ });
@@ -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,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,WAejD;AAED,wBAAgB,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,WAOhD"}
package/dist/json.js CHANGED
@@ -1,9 +1,9 @@
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
8
  export function replacer(_, value) {
9
9
  if (value instanceof Set) {
@@ -13,11 +13,21 @@ export function replacer(_, value) {
13
13
  value: Array.from(value)
14
14
  };
15
15
  }
16
+ else if (value instanceof Date) {
17
+ return {
18
+ __deserializedType: 'Date',
19
+ __isSerializedType: true,
20
+ value: value.toJSON()
21
+ };
22
+ }
16
23
  return value;
17
24
  }
18
25
  export function reviver(_, value) {
19
- if (isSerializedSet(value)) {
26
+ if (isSerializedObject(value, 'Set')) {
20
27
  return new Set(value.value);
21
28
  }
29
+ else if (isSerializedObject(value, 'Date')) {
30
+ return new Date(value.value);
31
+ }
22
32
  return value;
23
33
  }
package/dist/json.test.js CHANGED
@@ -9,13 +9,21 @@ describe('replacer', () => {
9
9
  value: [1, 2, 3]
10
10
  });
11
11
  });
12
- it('should return the value if it is not a Set', () => {
12
+ it('should serialize a Date', () => {
13
+ const date = new Date();
14
+ expect(replacer('', date)).toEqual({
15
+ __deserializedType: 'Date',
16
+ __isSerializedType: true,
17
+ value: date.toJSON()
18
+ });
19
+ });
20
+ it('should return the value if it is not a Set or a Date', () => {
13
21
  const obj = { a: 1 };
14
22
  expect(replacer('', obj)).toBe(obj);
15
23
  });
16
24
  });
17
25
  describe('reviver', () => {
18
- it('should deserialize a serialized set', () => {
26
+ it('should deserialize a serialized Set', () => {
19
27
  const serializedSet = {
20
28
  __deserializedType: 'Set',
21
29
  __isSerializedType: true,
@@ -24,11 +32,20 @@ describe('reviver', () => {
24
32
  const deserializedSet = new Set([1, 2, 3]);
25
33
  expect(reviver('', serializedSet)).toEqual(deserializedSet);
26
34
  });
35
+ it('should deserialize a serialized Date', () => {
36
+ const date = new Date();
37
+ const serializedDate = {
38
+ __deserializedType: 'Date',
39
+ __isSerializedType: true,
40
+ value: date.toJSON()
41
+ };
42
+ expect(reviver('', serializedDate)).toEqual(date);
43
+ });
27
44
  it('should return the value if it is not a plain object', () => {
28
45
  const obj = new Date();
29
46
  expect(reviver('', obj)).toBe(obj);
30
47
  });
31
- it('should return the value if it is a plain object but not a serialized set', () => {
48
+ it('should return the value if it is a plain object but not a serialized Set or Date', () => {
32
49
  const obj = { a: 1 };
33
50
  expect(reviver('', obj)).toBe(obj);
34
51
  });
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@douglasneuroinformatics/libjs",
3
3
  "type": "module",
4
- "version": "0.7.1",
5
- "packageManager": "pnpm@9.3.0",
4
+ "version": "1.0.0",
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",
8
8
  "license": "Apache-2.0",