@ntnyq/utils 0.8.2 → 0.9.1

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.
Files changed (3) hide show
  1. package/dist/index.d.ts +69 -38
  2. package/dist/index.js +68 -49
  3. package/package.json +19 -17
package/dist/index.d.ts CHANGED
@@ -134,6 +134,14 @@ type Merge<T, U> = keyof T & keyof U extends never ? T & U : Omit<T, keyof T & k
134
134
  * Non empty object `{}`
135
135
  */
136
136
  type NonEmptyObject<T> = T extends Record<string, never> ? never : T;
137
+ /**
138
+ * A type that represents the values of an object type.
139
+ */
140
+ type ValueOf<T> = T[keyof T];
141
+ /**
142
+ * A type that represents the elements of an array type.
143
+ */
144
+ type ElementOf<T extends unknown[] | null | undefined> = T extends Array<infer U> ? U : never;
137
145
  //#endregion
138
146
  //#region src/types/module.d.ts
139
147
  /**
@@ -224,42 +232,6 @@ declare function rAF(fn: FrameRequestCallback): number;
224
232
  */
225
233
  declare function cAF(id: number): void;
226
234
  //#endregion
227
- //#region src/misc/time.d.ts
228
- /**
229
- * @file time utils
230
- * @module Time
231
- */
232
- /**
233
- * Converts seconds to milliseconds.
234
- * @param count - The number of seconds.
235
- * @returns The equivalent number of milliseconds.
236
- */
237
- declare function seconds(count: number): number;
238
- /**
239
- * Converts minutes to milliseconds.
240
- * @param count - The number of minutes.
241
- * @returns The equivalent number of milliseconds.
242
- */
243
- declare function minutes(count: number): number;
244
- /**
245
- * Converts hours to milliseconds.
246
- * @param count - The number of hours.
247
- * @returns The equivalent number of milliseconds.
248
- */
249
- declare function hours(count: number): number;
250
- /**
251
- * Converts days to milliseconds.
252
- * @param count - The number of days.
253
- * @returns The equivalent number of milliseconds.
254
- */
255
- declare function days(count: number): number;
256
- /**
257
- * Converts weeks to milliseconds.
258
- * @param count - The number of weeks.
259
- * @returns The equivalent number of milliseconds.
260
- */
261
- declare function weeks(count: number): number;
262
- //#endregion
263
235
  //#region src/misc/clamp.d.ts
264
236
  /**
265
237
  * Clamps a number between a minimum and maximum value
@@ -316,9 +288,68 @@ declare function debounce<T extends ((...args: any[]) => undefined | void) | und
316
288
  */
317
289
  declare function warnOnce(message: string): void;
318
290
  //#endregion
291
+ //#region src/misc/convertTime.d.ts
292
+ /**
293
+ * @file time utils
294
+ * @module Time
295
+ */
296
+ /**
297
+ * Time unit conversion constants (in milliseconds)
298
+ */
299
+ declare const TIME_UNITS: {
300
+ readonly MILLISECOND: 1;
301
+ readonly SECOND: 1000;
302
+ readonly MINUTE: 60000;
303
+ readonly HOUR: 3600000;
304
+ readonly DAY: 86400000;
305
+ readonly WEEK: 604800000;
306
+ };
307
+ type TimeUnit = keyof typeof TIME_UNITS;
308
+ /**
309
+ * Converts time units to milliseconds.
310
+ * @param value - The time value.
311
+ * @param fromUnit - The source unit (default: 'SECOND').
312
+ * @returns The time in milliseconds.
313
+ * @example
314
+ * ```ts
315
+ * convertToMilliseconds(5, 'SECOND') // 5000
316
+ * convertToMilliseconds(2, 'MINUTE') // 120000
317
+ * convertToMilliseconds(1, 'HOUR') // 3600000
318
+ * ```
319
+ */
320
+ declare function convertToMilliseconds(value: number, fromUnit?: TimeUnit): number;
321
+ /**
322
+ * Converts milliseconds to specified time unit.
323
+ * @param milliseconds - The time in milliseconds.
324
+ * @param toUnit - The target unit (default: 'SECOND').
325
+ * @returns The time in the specified unit.
326
+ * @example
327
+ * ```ts
328
+ * convertFromMilliseconds(5000, 'SECOND') // 5
329
+ * convertFromMilliseconds(120000, 'MINUTE') // 2
330
+ * convertFromMilliseconds(3600000, 'HOUR') // 1
331
+ * ```
332
+ */
333
+ declare function convertFromMilliseconds(milliseconds: number, toUnit?: TimeUnit): number;
334
+ /**
335
+ * Converts between time units.
336
+ * @param value - The time value.
337
+ * @param fromUnit - The source unit.
338
+ * @param toUnit - The target unit.
339
+ * @returns The converted time.
340
+ * @example
341
+ * ```ts
342
+ * convertTimeUnit(1, 'HOUR', 'MINUTE') // 60
343
+ * convertTimeUnit(120, 'SECOND', 'MINUTE') // 2
344
+ * convertTimeUnit(2, 'WEEK', 'DAY') // 14
345
+ * ```
346
+ */
347
+ declare function convertTimeUnit(value: number, fromUnit: TimeUnit, toUnit: TimeUnit): number;
348
+ //#endregion
319
349
  //#region src/misc/convertStorage.d.ts
320
350
  /**
321
- * @file src/misc/transform.ts
351
+ * @file storage utils
352
+ * @module Storage
322
353
  */
323
354
  /**
324
355
  * Storage unit conversion constants
@@ -826,4 +857,4 @@ declare const RE_LINE_COMMENT: RegExp;
826
857
  */
827
858
  declare const RE_BLOCK_COMMENT: RegExp;
828
859
  //#endregion
829
- export { AnyFn, Arrayable, Awaitable, Callable, CleanObjectOptions, Color, CreatePadStringOptions, DeepRequired, GetStringSimilarityOptions, InteropModuleDefault, JsonArray, JsonObject, JsonPrimitive, JsonValue, LiteralUnion, MayBe, Merge, NOOP, NonEmptyObject, NonEmptyString, Nullable, OpenExternalURLOptions, Overwrite, Prettify, PrettifyV2, Primitive, RE_BLOCK_COMMENT, RE_COMMENTS, RE_LINE_COMMENT, RamdomNumberOptions, ResolvedOptions, SPECIAL_CHAR, STORAGE_UNITS, SortObjectOptions, StorageUnit, ThrottleDebounceOptions, ToIntegerOptions, Whitespace, at, cAF, chunk, clamp, cleanObject, convertFromBytes, convertStorageUnit, convertToBytes, createPadString, days, debounce, enhance, ensurePrefix, ensureSuffix, escapeHTML, flattenArrayable, getObjectType, getRoot, getStringLength, getStringSimilarity, hasOwn, hours, interopDefault, intersect, isArray, isArrayEqual, isBigInt, isBoolean, isBrowser, isDeepEqual, isElementVisibleInViewport, isEmptyArray, isEmptyMap, isEmptyObject, isEmptySet, isEmptyString, isEmptyStringOrWhitespace, isError, isFunction, isHTMLElement, isInteger, isIterable, isMap, isNaN, isNativePromise, isNil, isNonEmptyArray, isNonEmptyString, isNull, isNullOrUndefined, isNumber, isNumbericString, isObject, isPromise, isRegExp, isSet, isString, isTruthy, isUndefined, isWhitespaceString, isZero, join, last, mergeArrayable, minutes, noop, omit, once, openExternalURL, pick, rAF, randomHexColor, randomNumber, randomRGBAColor, randomRGBColor, randomString, removeFileExtension, resolveSubOptions, scrollElementIntoView, seconds, slash, slugify, sortObject, throttle, toArray, toInteger, unescapeHTML, unindent, unique, uniqueBy, waitFor, warnOnce, weeks };
860
+ export { AnyFn, Arrayable, Awaitable, Callable, CleanObjectOptions, Color, CreatePadStringOptions, DeepRequired, ElementOf, GetStringSimilarityOptions, InteropModuleDefault, JsonArray, JsonObject, JsonPrimitive, JsonValue, LiteralUnion, MayBe, Merge, NOOP, NonEmptyObject, NonEmptyString, Nullable, OpenExternalURLOptions, Overwrite, Prettify, PrettifyV2, Primitive, RE_BLOCK_COMMENT, RE_COMMENTS, RE_LINE_COMMENT, RamdomNumberOptions, ResolvedOptions, SPECIAL_CHAR, STORAGE_UNITS, SortObjectOptions, StorageUnit, TIME_UNITS, ThrottleDebounceOptions, TimeUnit, ToIntegerOptions, ValueOf, Whitespace, at, cAF, chunk, clamp, cleanObject, convertFromBytes, convertFromMilliseconds, convertStorageUnit, convertTimeUnit, convertToBytes, convertToMilliseconds, createPadString, debounce, enhance, ensurePrefix, ensureSuffix, escapeHTML, flattenArrayable, getObjectType, getRoot, getStringLength, getStringSimilarity, hasOwn, interopDefault, intersect, isArray, isArrayEqual, isBigInt, isBoolean, isBrowser, isDeepEqual, isElementVisibleInViewport, isEmptyArray, isEmptyMap, isEmptyObject, isEmptySet, isEmptyString, isEmptyStringOrWhitespace, isError, isFunction, isHTMLElement, isInteger, isIterable, isMap, isNaN, isNativePromise, isNil, isNonEmptyArray, isNonEmptyString, isNull, isNullOrUndefined, isNumber, isNumbericString, isObject, isPromise, isRegExp, isSet, isString, isTruthy, isUndefined, isWhitespaceString, isZero, join, last, mergeArrayable, noop, omit, once, openExternalURL, pick, rAF, randomHexColor, randomNumber, randomRGBAColor, randomRGBColor, randomString, removeFileExtension, resolveSubOptions, scrollElementIntoView, slash, slugify, sortObject, throttle, toArray, toInteger, unescapeHTML, unindent, unique, uniqueBy, waitFor, warnOnce };
package/dist/index.js CHANGED
@@ -305,53 +305,6 @@ function cAF(id) {
305
305
  return caf.call(root, id);
306
306
  }
307
307
 
308
- //#endregion
309
- //#region src/misc/time.ts
310
- /**
311
- * @file time utils
312
- * @module Time
313
- */
314
- /**
315
- * Converts seconds to milliseconds.
316
- * @param count - The number of seconds.
317
- * @returns The equivalent number of milliseconds.
318
- */
319
- function seconds(count) {
320
- return count * 1e3;
321
- }
322
- /**
323
- * Converts minutes to milliseconds.
324
- * @param count - The number of minutes.
325
- * @returns The equivalent number of milliseconds.
326
- */
327
- function minutes(count) {
328
- return count * 60 * 1e3;
329
- }
330
- /**
331
- * Converts hours to milliseconds.
332
- * @param count - The number of hours.
333
- * @returns The equivalent number of milliseconds.
334
- */
335
- function hours(count) {
336
- return count * 60 * 60 * 1e3;
337
- }
338
- /**
339
- * Converts days to milliseconds.
340
- * @param count - The number of days.
341
- * @returns The equivalent number of milliseconds.
342
- */
343
- function days(count) {
344
- return count * 24 * 60 * 60 * 1e3;
345
- }
346
- /**
347
- * Converts weeks to milliseconds.
348
- * @param count - The number of weeks.
349
- * @returns The equivalent number of milliseconds.
350
- */
351
- function weeks(count) {
352
- return count * 7 * 24 * 60 * 60 * 1e3;
353
- }
354
-
355
308
  //#endregion
356
309
  //#region src/misc/clamp.ts
357
310
  /**
@@ -453,10 +406,76 @@ function warnOnce(message) {
453
406
  console.warn(message);
454
407
  }
455
408
 
409
+ //#endregion
410
+ //#region src/misc/convertTime.ts
411
+ /**
412
+ * @file time utils
413
+ * @module Time
414
+ */
415
+ /**
416
+ * Time unit conversion constants (in milliseconds)
417
+ */
418
+ const TIME_UNITS = {
419
+ MILLISECOND: 1,
420
+ SECOND: 1e3,
421
+ MINUTE: 60 * 1e3,
422
+ HOUR: 3600 * 1e3,
423
+ DAY: 1440 * 60 * 1e3,
424
+ WEEK: 10080 * 60 * 1e3
425
+ };
426
+ /**
427
+ * Converts time units to milliseconds.
428
+ * @param value - The time value.
429
+ * @param fromUnit - The source unit (default: 'SECOND').
430
+ * @returns The time in milliseconds.
431
+ * @example
432
+ * ```ts
433
+ * convertToMilliseconds(5, 'SECOND') // 5000
434
+ * convertToMilliseconds(2, 'MINUTE') // 120000
435
+ * convertToMilliseconds(1, 'HOUR') // 3600000
436
+ * ```
437
+ */
438
+ function convertToMilliseconds(value, fromUnit = "SECOND") {
439
+ return value * TIME_UNITS[fromUnit];
440
+ }
441
+ /**
442
+ * Converts milliseconds to specified time unit.
443
+ * @param milliseconds - The time in milliseconds.
444
+ * @param toUnit - The target unit (default: 'SECOND').
445
+ * @returns The time in the specified unit.
446
+ * @example
447
+ * ```ts
448
+ * convertFromMilliseconds(5000, 'SECOND') // 5
449
+ * convertFromMilliseconds(120000, 'MINUTE') // 2
450
+ * convertFromMilliseconds(3600000, 'HOUR') // 1
451
+ * ```
452
+ */
453
+ function convertFromMilliseconds(milliseconds, toUnit = "SECOND") {
454
+ return milliseconds / TIME_UNITS[toUnit];
455
+ }
456
+ /**
457
+ * Converts between time units.
458
+ * @param value - The time value.
459
+ * @param fromUnit - The source unit.
460
+ * @param toUnit - The target unit.
461
+ * @returns The converted time.
462
+ * @example
463
+ * ```ts
464
+ * convertTimeUnit(1, 'HOUR', 'MINUTE') // 60
465
+ * convertTimeUnit(120, 'SECOND', 'MINUTE') // 2
466
+ * convertTimeUnit(2, 'WEEK', 'DAY') // 14
467
+ * ```
468
+ */
469
+ function convertTimeUnit(value, fromUnit, toUnit) {
470
+ const milliseconds = convertToMilliseconds(value, fromUnit);
471
+ return convertFromMilliseconds(milliseconds, toUnit);
472
+ }
473
+
456
474
  //#endregion
457
475
  //#region src/misc/convertStorage.ts
458
476
  /**
459
- * @file src/misc/transform.ts
477
+ * @file storage utils
478
+ * @module Storage
460
479
  */
461
480
  /**
462
481
  * Storage unit conversion constants
@@ -1146,4 +1165,4 @@ const RE_LINE_COMMENT = /\/\/.*/;
1146
1165
  const RE_BLOCK_COMMENT = /\/\*[\s\S]*?\*\//g;
1147
1166
 
1148
1167
  //#endregion
1149
- export { Color, NOOP, RE_BLOCK_COMMENT, RE_COMMENTS, RE_LINE_COMMENT, SPECIAL_CHAR, STORAGE_UNITS, at, cAF, chunk, clamp, cleanObject, convertFromBytes, convertStorageUnit, convertToBytes, createPadString, days, debounce, enhance, ensurePrefix, ensureSuffix, escapeHTML, flattenArrayable, getObjectType, getRoot, getStringLength, getStringSimilarity, hasOwn, hours, interopDefault, intersect, isArray, isArrayEqual, isBigInt, isBoolean, isBrowser, isDeepEqual, isElementVisibleInViewport, isEmptyArray, isEmptyMap, isEmptyObject, isEmptySet, isEmptyString, isEmptyStringOrWhitespace, isError, isFunction, isHTMLElement, isInteger, isIterable, isMap, isNaN, isNativePromise, isNil, isNonEmptyArray, isNonEmptyString, isNull, isNullOrUndefined, isNumber, isNumbericString, isObject, isPromise, isRegExp, isSet, isString, isTruthy, isUndefined, isWhitespaceString, isZero, join, last, mergeArrayable, minutes, noop, omit, once, openExternalURL, pick, rAF, randomHexColor, randomNumber, randomRGBAColor, randomRGBColor, randomString, removeFileExtension, resolveSubOptions, scrollElementIntoView, seconds, slash, slugify, sortObject, throttle, toArray, toInteger, unescapeHTML, unindent, unique, uniqueBy, waitFor, warnOnce, weeks };
1168
+ export { Color, NOOP, RE_BLOCK_COMMENT, RE_COMMENTS, RE_LINE_COMMENT, SPECIAL_CHAR, STORAGE_UNITS, TIME_UNITS, at, cAF, chunk, clamp, cleanObject, convertFromBytes, convertFromMilliseconds, convertStorageUnit, convertTimeUnit, convertToBytes, convertToMilliseconds, createPadString, debounce, enhance, ensurePrefix, ensureSuffix, escapeHTML, flattenArrayable, getObjectType, getRoot, getStringLength, getStringSimilarity, hasOwn, interopDefault, intersect, isArray, isArrayEqual, isBigInt, isBoolean, isBrowser, isDeepEqual, isElementVisibleInViewport, isEmptyArray, isEmptyMap, isEmptyObject, isEmptySet, isEmptyString, isEmptyStringOrWhitespace, isError, isFunction, isHTMLElement, isInteger, isIterable, isMap, isNaN, isNativePromise, isNil, isNonEmptyArray, isNonEmptyString, isNull, isNullOrUndefined, isNumber, isNumbericString, isObject, isPromise, isRegExp, isSet, isString, isTruthy, isUndefined, isWhitespaceString, isZero, join, last, mergeArrayable, noop, omit, once, openExternalURL, pick, rAF, randomHexColor, randomNumber, randomRGBAColor, randomRGBColor, randomString, removeFileExtension, resolveSubOptions, scrollElementIntoView, slash, slugify, sortObject, throttle, toArray, toInteger, unescapeHTML, unindent, unique, uniqueBy, waitFor, warnOnce };
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@ntnyq/utils",
3
3
  "type": "module",
4
- "version": "0.8.2",
4
+ "version": "0.9.1",
5
+ "packageManager": "pnpm@10.14.0",
5
6
  "description": "Common used utils.",
6
7
  "keywords": [
7
8
  "utils"
@@ -29,16 +30,28 @@
29
30
  "dist"
30
31
  ],
31
32
  "sideEffects": false,
33
+ "scripts": {
34
+ "build": "tsdown",
35
+ "dev": "tsdown --watch src",
36
+ "lint": "eslint",
37
+ "prepare": "husky",
38
+ "prepublishOnly": "pnpm run build",
39
+ "release": "run-s release:check release:version",
40
+ "release:check": "run-s lint typecheck test",
41
+ "release:version": "bumpp",
42
+ "test": "vitest",
43
+ "typecheck": "tsc --noEmit"
44
+ },
32
45
  "devDependencies": {
33
- "@ntnyq/eslint-config": "^5.2.0",
46
+ "@ntnyq/eslint-config": "^5.3.1",
34
47
  "@ntnyq/prettier-config": "^3.0.1",
35
- "bumpp": "^10.2.2",
36
- "eslint": "^9.32.0",
48
+ "bumpp": "^10.2.3",
49
+ "eslint": "^9.33.0",
37
50
  "husky": "^9.1.7",
38
51
  "nano-staged": "^0.8.0",
39
52
  "npm-run-all2": "^8.0.4",
40
53
  "prettier": "^3.6.2",
41
- "tsdown": "^0.13.1",
54
+ "tsdown": "^0.14.1",
42
55
  "typescript": "^5.9.2",
43
56
  "vitest": "^3.2.4"
44
57
  },
@@ -47,16 +60,5 @@
47
60
  },
48
61
  "nano-staged": {
49
62
  "*.{js,ts,mjs,cjs,md,yml,yaml,toml,json}": "eslint --fix"
50
- },
51
- "scripts": {
52
- "build": "tsdown",
53
- "dev": "tsdown --watch src",
54
- "lint": "eslint",
55
- "release": "run-s release:check release:version release:publish",
56
- "release:check": "run-s lint typecheck test",
57
- "release:publish": "pnpm publish",
58
- "release:version": "bumpp",
59
- "test": "vitest",
60
- "typecheck": "tsc --noEmit"
61
63
  }
62
- }
64
+ }