@ls-stack/utils 2.6.0 → 2.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/cache.cjs ADDED
@@ -0,0 +1,125 @@
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/cache.ts
21
+ var cache_exports = {};
22
+ __export(cache_exports, {
23
+ cachedGetter: () => cachedGetter,
24
+ createCache: () => createCache
25
+ });
26
+ module.exports = __toCommonJS(cache_exports);
27
+
28
+ // src/assertions.ts
29
+ function isObject(value) {
30
+ return typeof value === "object" && value !== null && !Array.isArray(value);
31
+ }
32
+ function isPromise(value) {
33
+ return isObject(value) && "then" in value;
34
+ }
35
+
36
+ // src/cache.ts
37
+ function cachedGetter(getter) {
38
+ return {
39
+ get value() {
40
+ const value = getter();
41
+ Object.defineProperty(this, "value", { value });
42
+ return value;
43
+ }
44
+ };
45
+ }
46
+ function createCache({
47
+ maxCacheSize = 1e3,
48
+ maxItemAge,
49
+ expirationThrottle = 1e4
50
+ } = {}) {
51
+ const cache = /* @__PURE__ */ new Map();
52
+ let lastExpirationCheck = 0;
53
+ function checkExpiredItems() {
54
+ const now = Date.now();
55
+ if (!maxItemAge || now - lastExpirationCheck < expirationThrottle) return;
56
+ lastExpirationCheck = now;
57
+ const maxAgeMs = maxItemAge * 1e3;
58
+ for (const [key, item] of cache.entries()) {
59
+ if (now - item.timestamp > maxAgeMs) {
60
+ cache.delete(key);
61
+ }
62
+ }
63
+ }
64
+ function trimToSize() {
65
+ const currentSize = cache.size;
66
+ if (currentSize > maxCacheSize) {
67
+ const keysToRemove = currentSize - maxCacheSize;
68
+ const iterator = cache.keys();
69
+ for (let i = 0; i < keysToRemove; i++) {
70
+ const { value: key } = iterator.next();
71
+ if (key) {
72
+ cache.delete(key);
73
+ }
74
+ }
75
+ }
76
+ }
77
+ function isExpired(timestamp, now) {
78
+ return maxItemAge !== void 0 && now - timestamp > maxItemAge * 1e3;
79
+ }
80
+ function getOrInsert(cacheKey, val) {
81
+ const now = Date.now();
82
+ const entry = cache.get(cacheKey);
83
+ if (!entry || isExpired(entry.timestamp, now)) {
84
+ const value = val();
85
+ cache.set(cacheKey, { value, timestamp: now });
86
+ trimToSize();
87
+ checkExpiredItems();
88
+ return value;
89
+ }
90
+ return entry.value;
91
+ }
92
+ async function getOrInsertAsync(cacheKey, val) {
93
+ const entry = cache.get(cacheKey);
94
+ if (entry && isPromise(entry.value)) {
95
+ return entry.value;
96
+ }
97
+ const now = Date.now();
98
+ if (entry && !isExpired(entry.timestamp, now)) {
99
+ return entry.value;
100
+ }
101
+ const promise = val().then((result) => {
102
+ cache.set(cacheKey, { value: result, timestamp: Date.now() });
103
+ return result;
104
+ }).catch((error) => {
105
+ cache.delete(cacheKey);
106
+ throw error;
107
+ });
108
+ cache.set(cacheKey, { value: promise, timestamp: now });
109
+ trimToSize();
110
+ checkExpiredItems();
111
+ return promise;
112
+ }
113
+ return {
114
+ getOrInsert,
115
+ getOrInsertAsync,
116
+ clear: () => cache.clear(),
117
+ /** @internal */
118
+ "~cache": cache
119
+ };
120
+ }
121
+ // Annotate the CommonJS export names for ESM import in node:
122
+ 0 && (module.exports = {
123
+ cachedGetter,
124
+ createCache
125
+ });
@@ -0,0 +1,31 @@
1
+ declare function cachedGetter<T>(getter: () => T): {
2
+ value: T;
3
+ };
4
+ type Options = {
5
+ /**
6
+ * The maximum number of items in the cache.
7
+ * @default 1000
8
+ */
9
+ maxCacheSize?: number;
10
+ /**
11
+ * The maximum age of items in the cache in seconds.
12
+ */
13
+ maxItemAge?: number;
14
+ /**
15
+ * The throttle for checking expired items in milliseconds.
16
+ * @default 10_000
17
+ */
18
+ expirationThrottle?: number;
19
+ };
20
+ declare function createCache({ maxCacheSize, maxItemAge, expirationThrottle, }?: Options): {
21
+ getOrInsert: <T>(cacheKey: string, val: () => T) => T;
22
+ getOrInsertAsync: <T>(cacheKey: string, val: () => Promise<T>) => Promise<T>;
23
+ clear: () => void;
24
+ /** @internal */
25
+ '~cache': Map<string, {
26
+ value: unknown;
27
+ timestamp: number;
28
+ }>;
29
+ };
30
+
31
+ export { cachedGetter, createCache };
@@ -0,0 +1,31 @@
1
+ declare function cachedGetter<T>(getter: () => T): {
2
+ value: T;
3
+ };
4
+ type Options = {
5
+ /**
6
+ * The maximum number of items in the cache.
7
+ * @default 1000
8
+ */
9
+ maxCacheSize?: number;
10
+ /**
11
+ * The maximum age of items in the cache in seconds.
12
+ */
13
+ maxItemAge?: number;
14
+ /**
15
+ * The throttle for checking expired items in milliseconds.
16
+ * @default 10_000
17
+ */
18
+ expirationThrottle?: number;
19
+ };
20
+ declare function createCache({ maxCacheSize, maxItemAge, expirationThrottle, }?: Options): {
21
+ getOrInsert: <T>(cacheKey: string, val: () => T) => T;
22
+ getOrInsertAsync: <T>(cacheKey: string, val: () => Promise<T>) => Promise<T>;
23
+ clear: () => void;
24
+ /** @internal */
25
+ '~cache': Map<string, {
26
+ value: unknown;
27
+ timestamp: number;
28
+ }>;
29
+ };
30
+
31
+ export { cachedGetter, createCache };
package/dist/cache.js ADDED
@@ -0,0 +1,93 @@
1
+ import {
2
+ isPromise
3
+ } from "./chunk-4UGSP3L3.js";
4
+
5
+ // src/cache.ts
6
+ function cachedGetter(getter) {
7
+ return {
8
+ get value() {
9
+ const value = getter();
10
+ Object.defineProperty(this, "value", { value });
11
+ return value;
12
+ }
13
+ };
14
+ }
15
+ function createCache({
16
+ maxCacheSize = 1e3,
17
+ maxItemAge,
18
+ expirationThrottle = 1e4
19
+ } = {}) {
20
+ const cache = /* @__PURE__ */ new Map();
21
+ let lastExpirationCheck = 0;
22
+ function checkExpiredItems() {
23
+ const now = Date.now();
24
+ if (!maxItemAge || now - lastExpirationCheck < expirationThrottle) return;
25
+ lastExpirationCheck = now;
26
+ const maxAgeMs = maxItemAge * 1e3;
27
+ for (const [key, item] of cache.entries()) {
28
+ if (now - item.timestamp > maxAgeMs) {
29
+ cache.delete(key);
30
+ }
31
+ }
32
+ }
33
+ function trimToSize() {
34
+ const currentSize = cache.size;
35
+ if (currentSize > maxCacheSize) {
36
+ const keysToRemove = currentSize - maxCacheSize;
37
+ const iterator = cache.keys();
38
+ for (let i = 0; i < keysToRemove; i++) {
39
+ const { value: key } = iterator.next();
40
+ if (key) {
41
+ cache.delete(key);
42
+ }
43
+ }
44
+ }
45
+ }
46
+ function isExpired(timestamp, now) {
47
+ return maxItemAge !== void 0 && now - timestamp > maxItemAge * 1e3;
48
+ }
49
+ function getOrInsert(cacheKey, val) {
50
+ const now = Date.now();
51
+ const entry = cache.get(cacheKey);
52
+ if (!entry || isExpired(entry.timestamp, now)) {
53
+ const value = val();
54
+ cache.set(cacheKey, { value, timestamp: now });
55
+ trimToSize();
56
+ checkExpiredItems();
57
+ return value;
58
+ }
59
+ return entry.value;
60
+ }
61
+ async function getOrInsertAsync(cacheKey, val) {
62
+ const entry = cache.get(cacheKey);
63
+ if (entry && isPromise(entry.value)) {
64
+ return entry.value;
65
+ }
66
+ const now = Date.now();
67
+ if (entry && !isExpired(entry.timestamp, now)) {
68
+ return entry.value;
69
+ }
70
+ const promise = val().then((result) => {
71
+ cache.set(cacheKey, { value: result, timestamp: Date.now() });
72
+ return result;
73
+ }).catch((error) => {
74
+ cache.delete(cacheKey);
75
+ throw error;
76
+ });
77
+ cache.set(cacheKey, { value: promise, timestamp: now });
78
+ trimToSize();
79
+ checkExpiredItems();
80
+ return promise;
81
+ }
82
+ return {
83
+ getOrInsert,
84
+ getOrInsertAsync,
85
+ clear: () => cache.clear(),
86
+ /** @internal */
87
+ "~cache": cache
88
+ };
89
+ }
90
+ export {
91
+ cachedGetter,
92
+ createCache
93
+ };
@@ -1,15 +1,7 @@
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
- }
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
+ };
package/dist/main.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  ///<reference path="arrayUtils.d.ts" />
2
2
  ///<reference path="assertions.d.ts" />
3
+ ///<reference path="cache.d.ts" />
3
4
  ///<reference path="castValues.d.ts" />
4
5
  ///<reference path="consoleFmt.d.ts" />
5
6
  ///<reference path="conversions.d.ts" />
@@ -25,6 +26,7 @@
25
26
  ///<reference path="sleep.d.ts" />
26
27
  ///<reference path="stringUtils.d.ts" />
27
28
  ///<reference path="testUtils.d.ts" />
29
+ ///<reference path="time.d.ts" />
28
30
  ///<reference path="typingFnUtils.d.ts" />
29
31
  ///<reference path="typingTestUtils.d.ts" />
30
32
  ///<reference path="typingUtils.d.ts" />
@@ -1,11 +1,11 @@
1
+ import {
2
+ sleep
3
+ } from "./chunk-5DZT3Z5Z.js";
1
4
  import {
2
5
  Result,
3
6
  unknownToError
4
7
  } from "./chunk-KBFP7INB.js";
5
8
  import "./chunk-VAAMRG4K.js";
6
- import {
7
- sleep
8
- } from "./chunk-5DZT3Z5Z.js";
9
9
  import {
10
10
  invariant,
11
11
  isObject
package/dist/testUtils.js CHANGED
@@ -1,13 +1,13 @@
1
+ import {
2
+ omit,
3
+ pick
4
+ } from "./chunk-ZE3DLPMN.js";
1
5
  import {
2
6
  deepEqual
3
7
  } from "./chunk-JQFUKJU5.js";
4
8
  import {
5
9
  clampMin
6
10
  } from "./chunk-HTCYUMDR.js";
7
- import {
8
- omit,
9
- pick
10
- } from "./chunk-ZE3DLPMN.js";
11
11
  import {
12
12
  arrayWithPrevAndIndex,
13
13
  filterAndMap
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
+ });
@@ -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.6.0",
4
+ "version": "2.8.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "dist"
@@ -29,6 +29,11 @@
29
29
  "types": "./dist/assertions.d.ts",
30
30
  "require": "./dist/assertions.cjs"
31
31
  },
32
+ "./cache": {
33
+ "import": "./dist/cache.js",
34
+ "types": "./dist/cache.d.ts",
35
+ "require": "./dist/cache.cjs"
36
+ },
32
37
  "./castValues": {
33
38
  "import": "./dist/castValues.js",
34
39
  "types": "./dist/castValues.d.ts",
@@ -154,6 +159,11 @@
154
159
  "types": "./dist/testUtils.d.ts",
155
160
  "require": "./dist/testUtils.cjs"
156
161
  },
162
+ "./time": {
163
+ "import": "./dist/time.js",
164
+ "types": "./dist/time.d.ts",
165
+ "require": "./dist/time.cjs"
166
+ },
157
167
  "./typingFnUtils": {
158
168
  "import": "./dist/typingFnUtils.js",
159
169
  "types": "./dist/typingFnUtils.d.ts",
@@ -208,6 +218,9 @@
208
218
  "assertions": [
209
219
  "./dist/assertions.d.ts"
210
220
  ],
221
+ "cache": [
222
+ "./dist/cache.d.ts"
223
+ ],
211
224
  "castValues": [
212
225
  "./dist/castValues.d.ts"
213
226
  ],
@@ -283,6 +296,9 @@
283
296
  "testUtils": [
284
297
  "./dist/testUtils.d.ts"
285
298
  ],
299
+ "time": [
300
+ "./dist/time.d.ts"
301
+ ],
286
302
  "typingFnUtils": [
287
303
  "./dist/typingFnUtils.d.ts"
288
304
  ],