@ls-stack/utils 2.7.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.d.cts CHANGED
@@ -12,7 +12,7 @@ type Options = {
12
12
  */
13
13
  maxItemAge?: number;
14
14
  /**
15
- * The maximum number of items to check for expiration in a single call.
15
+ * The throttle for checking expired items in milliseconds.
16
16
  * @default 10_000
17
17
  */
18
18
  expirationThrottle?: number;
package/dist/cache.d.ts CHANGED
@@ -12,7 +12,7 @@ type Options = {
12
12
  */
13
13
  maxItemAge?: number;
14
14
  /**
15
- * The maximum number of items to check for expiration in a single call.
15
+ * The throttle for checking expired items in milliseconds.
16
16
  * @default 10_000
17
17
  */
18
18
  expirationThrottle?: number;
@@ -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
@@ -26,6 +26,7 @@
26
26
  ///<reference path="sleep.d.ts" />
27
27
  ///<reference path="stringUtils.d.ts" />
28
28
  ///<reference path="testUtils.d.ts" />
29
+ ///<reference path="time.d.ts" />
29
30
  ///<reference path="typingFnUtils.d.ts" />
30
31
  ///<reference path="typingTestUtils.d.ts" />
31
32
  ///<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
+ });
@@ -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.7.0",
4
+ "version": "2.8.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "dist"
@@ -159,6 +159,11 @@
159
159
  "types": "./dist/testUtils.d.ts",
160
160
  "require": "./dist/testUtils.cjs"
161
161
  },
162
+ "./time": {
163
+ "import": "./dist/time.js",
164
+ "types": "./dist/time.d.ts",
165
+ "require": "./dist/time.cjs"
166
+ },
162
167
  "./typingFnUtils": {
163
168
  "import": "./dist/typingFnUtils.js",
164
169
  "types": "./dist/typingFnUtils.d.ts",
@@ -291,6 +296,9 @@
291
296
  "testUtils": [
292
297
  "./dist/testUtils.d.ts"
293
298
  ],
299
+ "time": [
300
+ "./dist/time.d.ts"
301
+ ],
294
302
  "typingFnUtils": [
295
303
  "./dist/typingFnUtils.d.ts"
296
304
  ],