@douglasneuroinformatics/libjs 0.7.0 → 0.8.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/datetime.d.ts +17 -0
- package/dist/datetime.d.ts.map +1 -1
- package/dist/datetime.js +28 -0
- package/dist/datetime.test.js +81 -1
- package/dist/douglasneuroinformatics-libjs-0.8.0.tgz +0 -0
- package/dist/string.js +0 -1
- package/package.json +2 -2
- package/dist/douglasneuroinformatics-libjs-0.7.0.tgz +0 -0
package/dist/datetime.d.ts
CHANGED
|
@@ -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
|
package/dist/datetime.d.ts.map
CHANGED
|
@@ -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
|
+
}
|
package/dist/datetime.test.js
CHANGED
|
@@ -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
|
+
});
|
|
Binary file
|
package/dist/string.js
CHANGED
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@douglasneuroinformatics/libjs",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
5
|
-
"packageManager": "pnpm@9.
|
|
4
|
+
"version": "0.8.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",
|
|
Binary file
|