@ls-stack/utils 2.7.0 → 2.9.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/cache.d.cts +1 -1
- package/dist/cache.d.ts +1 -1
- package/dist/castValues.js +4 -12
- package/dist/chunk-GBFS2I67.js +17 -0
- package/dist/getObjStableKey.cjs +83 -0
- package/dist/getObjStableKey.d.cts +3 -0
- package/dist/getObjStableKey.d.ts +3 -0
- package/dist/getObjStableKey.js +44 -0
- package/dist/main.d.ts +2 -0
- package/dist/time.cjs +139 -0
- package/dist/time.d.cts +17 -0
- package/dist/time.d.ts +17 -0
- package/dist/time.js +91 -0
- package/package.json +17 -1
package/dist/cache.d.cts
CHANGED
package/dist/cache.d.ts
CHANGED
package/dist/castValues.js
CHANGED
|
@@ -1,15 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
function castToNumber(value) {
|
|
7
|
-
return isNumeric(value) ? Number(value) : null;
|
|
8
|
-
}
|
|
9
|
-
function isNumeric(num) {
|
|
10
|
-
const str = String(num);
|
|
11
|
-
return !isNaN(str) && !isNaN(parseFloat(str));
|
|
12
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
castToNumber,
|
|
3
|
+
castToString
|
|
4
|
+
} from "./chunk-GBFS2I67.js";
|
|
13
5
|
export {
|
|
14
6
|
castToNumber,
|
|
15
7
|
castToString
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// src/castValues.ts
|
|
2
|
+
function castToString(value) {
|
|
3
|
+
const valueType = typeof value;
|
|
4
|
+
return valueType === "string" || valueType === "number" || valueType === "boolean" || valueType === "bigint" ? String(value) : null;
|
|
5
|
+
}
|
|
6
|
+
function castToNumber(value) {
|
|
7
|
+
return isNumeric(value) ? Number(value) : null;
|
|
8
|
+
}
|
|
9
|
+
function isNumeric(num) {
|
|
10
|
+
const str = String(num);
|
|
11
|
+
return !isNaN(str) && !isNaN(parseFloat(str));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export {
|
|
15
|
+
castToString,
|
|
16
|
+
castToNumber
|
|
17
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/getObjStableKey.ts
|
|
21
|
+
var getObjStableKey_exports = {};
|
|
22
|
+
__export(getObjStableKey_exports, {
|
|
23
|
+
getObjStableKey: () => getObjStableKey
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(getObjStableKey_exports);
|
|
26
|
+
|
|
27
|
+
// src/arrayUtils.ts
|
|
28
|
+
function filterAndMap(array, mapFilter) {
|
|
29
|
+
const result = [];
|
|
30
|
+
let i = -1;
|
|
31
|
+
for (const item of array) {
|
|
32
|
+
i += 1;
|
|
33
|
+
const filterResult = mapFilter(item, i);
|
|
34
|
+
if (filterResult !== false) {
|
|
35
|
+
result.push(filterResult);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/assertions.ts
|
|
42
|
+
function isObject(value) {
|
|
43
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/getObjStableKey.ts
|
|
47
|
+
function getObjStableKey(input, maxDepth = 3) {
|
|
48
|
+
if (typeof input === "string") return String(input);
|
|
49
|
+
if (!input || typeof input !== "object") return `#$${input}$#`;
|
|
50
|
+
return JSON.stringify(sortValues(input, maxDepth, 0));
|
|
51
|
+
}
|
|
52
|
+
function sortValues(input, maxDepth, depth) {
|
|
53
|
+
if (depth >= maxDepth) return input;
|
|
54
|
+
if (Array.isArray(input)) {
|
|
55
|
+
return input.map((v) => sortValues(v, maxDepth, depth + 1));
|
|
56
|
+
}
|
|
57
|
+
if (isObject(input)) {
|
|
58
|
+
return orderedProps(input, (v) => sortValues(v, maxDepth, depth + 1));
|
|
59
|
+
}
|
|
60
|
+
return input;
|
|
61
|
+
}
|
|
62
|
+
var emptyObject = {};
|
|
63
|
+
function orderedProps(obj, mapValue) {
|
|
64
|
+
const keys = Object.keys(obj);
|
|
65
|
+
if (keys.length === 0) return emptyObject;
|
|
66
|
+
if (keys.length === 1) {
|
|
67
|
+
const value = obj[keys[0]];
|
|
68
|
+
if (value === void 0) return emptyObject;
|
|
69
|
+
return { [keys[0]]: mapValue(value) };
|
|
70
|
+
}
|
|
71
|
+
const mappedValues = filterAndMap(keys.sort(), (k) => {
|
|
72
|
+
const value = obj[k];
|
|
73
|
+
if (value === void 0) return false;
|
|
74
|
+
return { [k]: mapValue(value) };
|
|
75
|
+
});
|
|
76
|
+
if (mappedValues.length === 0) return emptyObject;
|
|
77
|
+
if (mappedValues.length === 1) return mappedValues[0];
|
|
78
|
+
return mappedValues;
|
|
79
|
+
}
|
|
80
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
81
|
+
0 && (module.exports = {
|
|
82
|
+
getObjStableKey
|
|
83
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {
|
|
2
|
+
filterAndMap
|
|
3
|
+
} from "./chunk-QMFZE2VO.js";
|
|
4
|
+
import {
|
|
5
|
+
isObject
|
|
6
|
+
} from "./chunk-4UGSP3L3.js";
|
|
7
|
+
|
|
8
|
+
// src/getObjStableKey.ts
|
|
9
|
+
function getObjStableKey(input, maxDepth = 3) {
|
|
10
|
+
if (typeof input === "string") return String(input);
|
|
11
|
+
if (!input || typeof input !== "object") return `#$${input}$#`;
|
|
12
|
+
return JSON.stringify(sortValues(input, maxDepth, 0));
|
|
13
|
+
}
|
|
14
|
+
function sortValues(input, maxDepth, depth) {
|
|
15
|
+
if (depth >= maxDepth) return input;
|
|
16
|
+
if (Array.isArray(input)) {
|
|
17
|
+
return input.map((v) => sortValues(v, maxDepth, depth + 1));
|
|
18
|
+
}
|
|
19
|
+
if (isObject(input)) {
|
|
20
|
+
return orderedProps(input, (v) => sortValues(v, maxDepth, depth + 1));
|
|
21
|
+
}
|
|
22
|
+
return input;
|
|
23
|
+
}
|
|
24
|
+
var emptyObject = {};
|
|
25
|
+
function orderedProps(obj, mapValue) {
|
|
26
|
+
const keys = Object.keys(obj);
|
|
27
|
+
if (keys.length === 0) return emptyObject;
|
|
28
|
+
if (keys.length === 1) {
|
|
29
|
+
const value = obj[keys[0]];
|
|
30
|
+
if (value === void 0) return emptyObject;
|
|
31
|
+
return { [keys[0]]: mapValue(value) };
|
|
32
|
+
}
|
|
33
|
+
const mappedValues = filterAndMap(keys.sort(), (k) => {
|
|
34
|
+
const value = obj[k];
|
|
35
|
+
if (value === void 0) return false;
|
|
36
|
+
return { [k]: mapValue(value) };
|
|
37
|
+
});
|
|
38
|
+
if (mappedValues.length === 0) return emptyObject;
|
|
39
|
+
if (mappedValues.length === 1) return mappedValues[0];
|
|
40
|
+
return mappedValues;
|
|
41
|
+
}
|
|
42
|
+
export {
|
|
43
|
+
getObjStableKey
|
|
44
|
+
};
|
package/dist/main.d.ts
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
///<reference path="deepEqual.d.ts" />
|
|
11
11
|
///<reference path="enhancedMap.d.ts" />
|
|
12
12
|
///<reference path="exhaustiveMatch.d.ts" />
|
|
13
|
+
///<reference path="getObjStableKey.d.ts" />
|
|
13
14
|
///<reference path="internalUtils.d.ts" />
|
|
14
15
|
///<reference path="interpolate.d.ts" />
|
|
15
16
|
///<reference path="levenshtein.d.ts" />
|
|
@@ -26,6 +27,7 @@
|
|
|
26
27
|
///<reference path="sleep.d.ts" />
|
|
27
28
|
///<reference path="stringUtils.d.ts" />
|
|
28
29
|
///<reference path="testUtils.d.ts" />
|
|
30
|
+
///<reference path="time.d.ts" />
|
|
29
31
|
///<reference path="typingFnUtils.d.ts" />
|
|
30
32
|
///<reference path="typingTestUtils.d.ts" />
|
|
31
33
|
///<reference path="typingUtils.d.ts" />
|
package/dist/time.cjs
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/time.ts
|
|
21
|
+
var time_exports = {};
|
|
22
|
+
__export(time_exports, {
|
|
23
|
+
DAY_AS_MS: () => DAY_AS_MS,
|
|
24
|
+
DAY_AS_SECS: () => DAY_AS_SECS,
|
|
25
|
+
HOUR_AS_MS: () => HOUR_AS_MS,
|
|
26
|
+
HOUR_AS_SECS: () => HOUR_AS_SECS,
|
|
27
|
+
MINUTE_AS_MS: () => MINUTE_AS_MS,
|
|
28
|
+
MONTH_AS_MS: () => MONTH_AS_MS,
|
|
29
|
+
MONTH_AS_SECS: () => MONTH_AS_SECS,
|
|
30
|
+
WEEK_AS_MS: () => WEEK_AS_MS,
|
|
31
|
+
WEEK_AS_SECS: () => WEEK_AS_SECS,
|
|
32
|
+
YEAR_AS_MS: () => YEAR_AS_MS,
|
|
33
|
+
YEAR_AS_SECS: () => YEAR_AS_SECS,
|
|
34
|
+
dateStringOrNullToUnixMs: () => dateStringOrNullToUnixMs,
|
|
35
|
+
getUnixSeconds: () => getUnixSeconds,
|
|
36
|
+
msToTimeString: () => msToTimeString,
|
|
37
|
+
parseTimeStringToMs: () => parseTimeStringToMs
|
|
38
|
+
});
|
|
39
|
+
module.exports = __toCommonJS(time_exports);
|
|
40
|
+
|
|
41
|
+
// src/castValues.ts
|
|
42
|
+
function castToNumber(value) {
|
|
43
|
+
return isNumeric(value) ? Number(value) : null;
|
|
44
|
+
}
|
|
45
|
+
function isNumeric(num) {
|
|
46
|
+
const str = String(num);
|
|
47
|
+
return !isNaN(str) && !isNaN(parseFloat(str));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// src/mathUtils.ts
|
|
51
|
+
function clampMax(value, max) {
|
|
52
|
+
return value > max ? max : value;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// src/time.ts
|
|
56
|
+
var MINUTE_AS_MS = 60 * 1e3;
|
|
57
|
+
var HOUR_AS_MS = 60 * MINUTE_AS_MS;
|
|
58
|
+
var DAY_AS_MS = 24 * HOUR_AS_MS;
|
|
59
|
+
var WEEK_AS_MS = 7 * DAY_AS_MS;
|
|
60
|
+
var MONTH_AS_MS = 30 * DAY_AS_MS;
|
|
61
|
+
var YEAR_AS_MS = 365 * DAY_AS_MS;
|
|
62
|
+
var HOUR_AS_SECS = 60 * 60;
|
|
63
|
+
var DAY_AS_SECS = 24 * HOUR_AS_SECS;
|
|
64
|
+
var WEEK_AS_SECS = 7 * DAY_AS_SECS;
|
|
65
|
+
var MONTH_AS_SECS = 30 * DAY_AS_SECS;
|
|
66
|
+
var YEAR_AS_SECS = 365 * DAY_AS_SECS;
|
|
67
|
+
function dateStringOrNullToUnixMs(isoString) {
|
|
68
|
+
if (!isoString) return null;
|
|
69
|
+
const unixMs = new Date(isoString).getTime();
|
|
70
|
+
if (isNaN(unixMs)) return null;
|
|
71
|
+
return unixMs;
|
|
72
|
+
}
|
|
73
|
+
function msToTimeString(ms, format, hoursMinLength = 2) {
|
|
74
|
+
const { hours, minutes, seconds, milliseconds } = msToDurationObj(ms);
|
|
75
|
+
const hoursString = padTimeVal(hours, hoursMinLength);
|
|
76
|
+
const minutesString = padTimeVal(minutes);
|
|
77
|
+
if (format === "minutes") {
|
|
78
|
+
return `${hoursString}:${minutesString}`;
|
|
79
|
+
}
|
|
80
|
+
const secondsString = padTimeVal(seconds);
|
|
81
|
+
if (format === "seconds") {
|
|
82
|
+
return `${hoursString}:${minutesString}:${secondsString}`;
|
|
83
|
+
}
|
|
84
|
+
return `${hoursString}:${minutesString}:${secondsString}:${padTimeVal(
|
|
85
|
+
milliseconds,
|
|
86
|
+
3
|
|
87
|
+
)}`;
|
|
88
|
+
}
|
|
89
|
+
function padTimeVal(val, maxLength = 2) {
|
|
90
|
+
return val.toString().padStart(maxLength, "0");
|
|
91
|
+
}
|
|
92
|
+
function parseTimeStringToMs(timeString) {
|
|
93
|
+
if (!timeString.trim()) return 0;
|
|
94
|
+
const [hours, minutes, seconds, ms] = timeString.split(":");
|
|
95
|
+
return getTimeStringPartToInt(hours) * HOUR_AS_MS + clampMax(getTimeStringPartToInt(minutes), 59) * MINUTE_AS_MS + clampMax(getTimeStringPartToInt(seconds), 59) * 1e3 + getTimeStringPartToInt(ms, 3);
|
|
96
|
+
}
|
|
97
|
+
function getTimeStringPartToInt(timeStringPart, length) {
|
|
98
|
+
if (!timeStringPart?.trim()) return 0;
|
|
99
|
+
let string = timeStringPart.replaceAll("_", "0");
|
|
100
|
+
string = string.replaceAll("-", "");
|
|
101
|
+
if (length) {
|
|
102
|
+
string = string.padEnd(length, "0");
|
|
103
|
+
if (string.length > length) {
|
|
104
|
+
string = string.slice(0, length);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const num = castToNumber(string);
|
|
108
|
+
if (!num) return 0;
|
|
109
|
+
return Math.floor(num);
|
|
110
|
+
}
|
|
111
|
+
function msToDurationObj(ms) {
|
|
112
|
+
return {
|
|
113
|
+
milliseconds: ms % 1e3,
|
|
114
|
+
seconds: Math.floor(ms / 1e3) % 60,
|
|
115
|
+
minutes: Math.floor(ms / 1e3 / 60) % 60,
|
|
116
|
+
hours: Math.floor(ms / 1e3 / 60 / 60)
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
function getUnixSeconds() {
|
|
120
|
+
return Math.floor(Date.now() / 1e3);
|
|
121
|
+
}
|
|
122
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
123
|
+
0 && (module.exports = {
|
|
124
|
+
DAY_AS_MS,
|
|
125
|
+
DAY_AS_SECS,
|
|
126
|
+
HOUR_AS_MS,
|
|
127
|
+
HOUR_AS_SECS,
|
|
128
|
+
MINUTE_AS_MS,
|
|
129
|
+
MONTH_AS_MS,
|
|
130
|
+
MONTH_AS_SECS,
|
|
131
|
+
WEEK_AS_MS,
|
|
132
|
+
WEEK_AS_SECS,
|
|
133
|
+
YEAR_AS_MS,
|
|
134
|
+
YEAR_AS_SECS,
|
|
135
|
+
dateStringOrNullToUnixMs,
|
|
136
|
+
getUnixSeconds,
|
|
137
|
+
msToTimeString,
|
|
138
|
+
parseTimeStringToMs
|
|
139
|
+
});
|
package/dist/time.d.cts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
declare const MINUTE_AS_MS: number;
|
|
2
|
+
declare const HOUR_AS_MS: number;
|
|
3
|
+
declare const DAY_AS_MS: number;
|
|
4
|
+
declare const WEEK_AS_MS: number;
|
|
5
|
+
declare const MONTH_AS_MS: number;
|
|
6
|
+
declare const YEAR_AS_MS: number;
|
|
7
|
+
declare const HOUR_AS_SECS: number;
|
|
8
|
+
declare const DAY_AS_SECS: number;
|
|
9
|
+
declare const WEEK_AS_SECS: number;
|
|
10
|
+
declare const MONTH_AS_SECS: number;
|
|
11
|
+
declare const YEAR_AS_SECS: number;
|
|
12
|
+
declare function dateStringOrNullToUnixMs(isoString: string | null | undefined): number | null;
|
|
13
|
+
declare function msToTimeString(ms: number, format: 'minutes' | 'seconds' | 'milliseconds', hoursMinLength?: number): string;
|
|
14
|
+
declare function parseTimeStringToMs(timeString: string): number;
|
|
15
|
+
declare function getUnixSeconds(): number;
|
|
16
|
+
|
|
17
|
+
export { DAY_AS_MS, DAY_AS_SECS, HOUR_AS_MS, HOUR_AS_SECS, MINUTE_AS_MS, MONTH_AS_MS, MONTH_AS_SECS, WEEK_AS_MS, WEEK_AS_SECS, YEAR_AS_MS, YEAR_AS_SECS, dateStringOrNullToUnixMs, getUnixSeconds, msToTimeString, parseTimeStringToMs };
|
package/dist/time.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
declare const MINUTE_AS_MS: number;
|
|
2
|
+
declare const HOUR_AS_MS: number;
|
|
3
|
+
declare const DAY_AS_MS: number;
|
|
4
|
+
declare const WEEK_AS_MS: number;
|
|
5
|
+
declare const MONTH_AS_MS: number;
|
|
6
|
+
declare const YEAR_AS_MS: number;
|
|
7
|
+
declare const HOUR_AS_SECS: number;
|
|
8
|
+
declare const DAY_AS_SECS: number;
|
|
9
|
+
declare const WEEK_AS_SECS: number;
|
|
10
|
+
declare const MONTH_AS_SECS: number;
|
|
11
|
+
declare const YEAR_AS_SECS: number;
|
|
12
|
+
declare function dateStringOrNullToUnixMs(isoString: string | null | undefined): number | null;
|
|
13
|
+
declare function msToTimeString(ms: number, format: 'minutes' | 'seconds' | 'milliseconds', hoursMinLength?: number): string;
|
|
14
|
+
declare function parseTimeStringToMs(timeString: string): number;
|
|
15
|
+
declare function getUnixSeconds(): number;
|
|
16
|
+
|
|
17
|
+
export { DAY_AS_MS, DAY_AS_SECS, HOUR_AS_MS, HOUR_AS_SECS, MINUTE_AS_MS, MONTH_AS_MS, MONTH_AS_SECS, WEEK_AS_MS, WEEK_AS_SECS, YEAR_AS_MS, YEAR_AS_SECS, dateStringOrNullToUnixMs, getUnixSeconds, msToTimeString, parseTimeStringToMs };
|
package/dist/time.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import {
|
|
2
|
+
clampMax
|
|
3
|
+
} from "./chunk-HTCYUMDR.js";
|
|
4
|
+
import {
|
|
5
|
+
castToNumber
|
|
6
|
+
} from "./chunk-GBFS2I67.js";
|
|
7
|
+
|
|
8
|
+
// src/time.ts
|
|
9
|
+
var MINUTE_AS_MS = 60 * 1e3;
|
|
10
|
+
var HOUR_AS_MS = 60 * MINUTE_AS_MS;
|
|
11
|
+
var DAY_AS_MS = 24 * HOUR_AS_MS;
|
|
12
|
+
var WEEK_AS_MS = 7 * DAY_AS_MS;
|
|
13
|
+
var MONTH_AS_MS = 30 * DAY_AS_MS;
|
|
14
|
+
var YEAR_AS_MS = 365 * DAY_AS_MS;
|
|
15
|
+
var HOUR_AS_SECS = 60 * 60;
|
|
16
|
+
var DAY_AS_SECS = 24 * HOUR_AS_SECS;
|
|
17
|
+
var WEEK_AS_SECS = 7 * DAY_AS_SECS;
|
|
18
|
+
var MONTH_AS_SECS = 30 * DAY_AS_SECS;
|
|
19
|
+
var YEAR_AS_SECS = 365 * DAY_AS_SECS;
|
|
20
|
+
function dateStringOrNullToUnixMs(isoString) {
|
|
21
|
+
if (!isoString) return null;
|
|
22
|
+
const unixMs = new Date(isoString).getTime();
|
|
23
|
+
if (isNaN(unixMs)) return null;
|
|
24
|
+
return unixMs;
|
|
25
|
+
}
|
|
26
|
+
function msToTimeString(ms, format, hoursMinLength = 2) {
|
|
27
|
+
const { hours, minutes, seconds, milliseconds } = msToDurationObj(ms);
|
|
28
|
+
const hoursString = padTimeVal(hours, hoursMinLength);
|
|
29
|
+
const minutesString = padTimeVal(minutes);
|
|
30
|
+
if (format === "minutes") {
|
|
31
|
+
return `${hoursString}:${minutesString}`;
|
|
32
|
+
}
|
|
33
|
+
const secondsString = padTimeVal(seconds);
|
|
34
|
+
if (format === "seconds") {
|
|
35
|
+
return `${hoursString}:${minutesString}:${secondsString}`;
|
|
36
|
+
}
|
|
37
|
+
return `${hoursString}:${minutesString}:${secondsString}:${padTimeVal(
|
|
38
|
+
milliseconds,
|
|
39
|
+
3
|
|
40
|
+
)}`;
|
|
41
|
+
}
|
|
42
|
+
function padTimeVal(val, maxLength = 2) {
|
|
43
|
+
return val.toString().padStart(maxLength, "0");
|
|
44
|
+
}
|
|
45
|
+
function parseTimeStringToMs(timeString) {
|
|
46
|
+
if (!timeString.trim()) return 0;
|
|
47
|
+
const [hours, minutes, seconds, ms] = timeString.split(":");
|
|
48
|
+
return getTimeStringPartToInt(hours) * HOUR_AS_MS + clampMax(getTimeStringPartToInt(minutes), 59) * MINUTE_AS_MS + clampMax(getTimeStringPartToInt(seconds), 59) * 1e3 + getTimeStringPartToInt(ms, 3);
|
|
49
|
+
}
|
|
50
|
+
function getTimeStringPartToInt(timeStringPart, length) {
|
|
51
|
+
if (!timeStringPart?.trim()) return 0;
|
|
52
|
+
let string = timeStringPart.replaceAll("_", "0");
|
|
53
|
+
string = string.replaceAll("-", "");
|
|
54
|
+
if (length) {
|
|
55
|
+
string = string.padEnd(length, "0");
|
|
56
|
+
if (string.length > length) {
|
|
57
|
+
string = string.slice(0, length);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const num = castToNumber(string);
|
|
61
|
+
if (!num) return 0;
|
|
62
|
+
return Math.floor(num);
|
|
63
|
+
}
|
|
64
|
+
function msToDurationObj(ms) {
|
|
65
|
+
return {
|
|
66
|
+
milliseconds: ms % 1e3,
|
|
67
|
+
seconds: Math.floor(ms / 1e3) % 60,
|
|
68
|
+
minutes: Math.floor(ms / 1e3 / 60) % 60,
|
|
69
|
+
hours: Math.floor(ms / 1e3 / 60 / 60)
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function getUnixSeconds() {
|
|
73
|
+
return Math.floor(Date.now() / 1e3);
|
|
74
|
+
}
|
|
75
|
+
export {
|
|
76
|
+
DAY_AS_MS,
|
|
77
|
+
DAY_AS_SECS,
|
|
78
|
+
HOUR_AS_MS,
|
|
79
|
+
HOUR_AS_SECS,
|
|
80
|
+
MINUTE_AS_MS,
|
|
81
|
+
MONTH_AS_MS,
|
|
82
|
+
MONTH_AS_SECS,
|
|
83
|
+
WEEK_AS_MS,
|
|
84
|
+
WEEK_AS_SECS,
|
|
85
|
+
YEAR_AS_MS,
|
|
86
|
+
YEAR_AS_SECS,
|
|
87
|
+
dateStringOrNullToUnixMs,
|
|
88
|
+
getUnixSeconds,
|
|
89
|
+
msToTimeString,
|
|
90
|
+
parseTimeStringToMs
|
|
91
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ls-stack/utils",
|
|
3
3
|
"description": "Typescript utils",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.9.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist"
|
|
@@ -79,6 +79,11 @@
|
|
|
79
79
|
"types": "./dist/exhaustiveMatch.d.ts",
|
|
80
80
|
"require": "./dist/exhaustiveMatch.cjs"
|
|
81
81
|
},
|
|
82
|
+
"./getObjStableKey": {
|
|
83
|
+
"import": "./dist/getObjStableKey.js",
|
|
84
|
+
"types": "./dist/getObjStableKey.d.ts",
|
|
85
|
+
"require": "./dist/getObjStableKey.cjs"
|
|
86
|
+
},
|
|
82
87
|
"./internalUtils": {
|
|
83
88
|
"import": "./dist/internalUtils.js",
|
|
84
89
|
"types": "./dist/internalUtils.d.ts",
|
|
@@ -159,6 +164,11 @@
|
|
|
159
164
|
"types": "./dist/testUtils.d.ts",
|
|
160
165
|
"require": "./dist/testUtils.cjs"
|
|
161
166
|
},
|
|
167
|
+
"./time": {
|
|
168
|
+
"import": "./dist/time.js",
|
|
169
|
+
"types": "./dist/time.d.ts",
|
|
170
|
+
"require": "./dist/time.cjs"
|
|
171
|
+
},
|
|
162
172
|
"./typingFnUtils": {
|
|
163
173
|
"import": "./dist/typingFnUtils.js",
|
|
164
174
|
"types": "./dist/typingFnUtils.d.ts",
|
|
@@ -243,6 +253,9 @@
|
|
|
243
253
|
"exhaustiveMatch": [
|
|
244
254
|
"./dist/exhaustiveMatch.d.ts"
|
|
245
255
|
],
|
|
256
|
+
"getObjStableKey": [
|
|
257
|
+
"./dist/getObjStableKey.d.ts"
|
|
258
|
+
],
|
|
246
259
|
"internalUtils": [
|
|
247
260
|
"./dist/internalUtils.d.ts"
|
|
248
261
|
],
|
|
@@ -291,6 +304,9 @@
|
|
|
291
304
|
"testUtils": [
|
|
292
305
|
"./dist/testUtils.d.ts"
|
|
293
306
|
],
|
|
307
|
+
"time": [
|
|
308
|
+
"./dist/time.d.ts"
|
|
309
|
+
],
|
|
294
310
|
"typingFnUtils": [
|
|
295
311
|
"./dist/typingFnUtils.d.ts"
|
|
296
312
|
],
|