@ntnyq/utils 0.8.1 → 0.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.
Files changed (3) hide show
  1. package/dist/index.d.ts +116 -38
  2. package/dist/index.js +131 -49
  3. package/package.json +6 -6
package/dist/index.d.ts CHANGED
@@ -50,7 +50,6 @@ declare function isInteger(value: unknown): value is number;
50
50
  declare function isBigInt(value: unknown): value is bigint;
51
51
  declare function isBoolean(value: unknown): value is boolean;
52
52
  declare function isTruthy<T>(value: T | undefined): value is T;
53
- // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
54
53
  declare function isFunction(value: unknown): value is Function;
55
54
  declare function isArray(value: unknown): value is unknown[];
56
55
  declare function isEmptyArray(value: unknown): value is [];
@@ -225,42 +224,6 @@ declare function rAF(fn: FrameRequestCallback): number;
225
224
  */
226
225
  declare function cAF(id: number): void;
227
226
  //#endregion
228
- //#region src/misc/time.d.ts
229
- /**
230
- * @file time utils
231
- * @module Time
232
- */
233
- /**
234
- * Converts seconds to milliseconds.
235
- * @param count - The number of seconds.
236
- * @returns The equivalent number of milliseconds.
237
- */
238
- declare function seconds(count: number): number;
239
- /**
240
- * Converts minutes to milliseconds.
241
- * @param count - The number of minutes.
242
- * @returns The equivalent number of milliseconds.
243
- */
244
- declare function minutes(count: number): number;
245
- /**
246
- * Converts hours to milliseconds.
247
- * @param count - The number of hours.
248
- * @returns The equivalent number of milliseconds.
249
- */
250
- declare function hours(count: number): number;
251
- /**
252
- * Converts days to milliseconds.
253
- * @param count - The number of days.
254
- * @returns The equivalent number of milliseconds.
255
- */
256
- declare function days(count: number): number;
257
- /**
258
- * Converts weeks to milliseconds.
259
- * @param count - The number of weeks.
260
- * @returns The equivalent number of milliseconds.
261
- */
262
- declare function weeks(count: number): number;
263
- //#endregion
264
227
  //#region src/misc/clamp.d.ts
265
228
  /**
266
229
  * Clamps a number between a minimum and maximum value
@@ -317,6 +280,121 @@ declare function debounce<T extends ((...args: any[]) => undefined | void) | und
317
280
  */
318
281
  declare function warnOnce(message: string): void;
319
282
  //#endregion
283
+ //#region src/misc/convertTime.d.ts
284
+ /**
285
+ * @file time utils
286
+ * @module Time
287
+ */
288
+ /**
289
+ * Time unit conversion constants (in milliseconds)
290
+ */
291
+ declare const TIME_UNITS: {
292
+ readonly MILLISECOND: 1;
293
+ readonly SECOND: 1000;
294
+ readonly MINUTE: 60000;
295
+ readonly HOUR: 3600000;
296
+ readonly DAY: 86400000;
297
+ readonly WEEK: 604800000;
298
+ };
299
+ type TimeUnit = keyof typeof TIME_UNITS;
300
+ /**
301
+ * Converts time units to milliseconds.
302
+ * @param value - The time value.
303
+ * @param fromUnit - The source unit (default: 'SECOND').
304
+ * @returns The time in milliseconds.
305
+ * @example
306
+ * ```ts
307
+ * convertToMilliseconds(5, 'SECOND') // 5000
308
+ * convertToMilliseconds(2, 'MINUTE') // 120000
309
+ * convertToMilliseconds(1, 'HOUR') // 3600000
310
+ * ```
311
+ */
312
+ declare function convertToMilliseconds(value: number, fromUnit?: TimeUnit): number;
313
+ /**
314
+ * Converts milliseconds to specified time unit.
315
+ * @param milliseconds - The time in milliseconds.
316
+ * @param toUnit - The target unit (default: 'SECOND').
317
+ * @returns The time in the specified unit.
318
+ * @example
319
+ * ```ts
320
+ * convertFromMilliseconds(5000, 'SECOND') // 5
321
+ * convertFromMilliseconds(120000, 'MINUTE') // 2
322
+ * convertFromMilliseconds(3600000, 'HOUR') // 1
323
+ * ```
324
+ */
325
+ declare function convertFromMilliseconds(milliseconds: number, toUnit?: TimeUnit): number;
326
+ /**
327
+ * Converts between time units.
328
+ * @param value - The time value.
329
+ * @param fromUnit - The source unit.
330
+ * @param toUnit - The target unit.
331
+ * @returns The converted time.
332
+ * @example
333
+ * ```ts
334
+ * convertTimeUnit(1, 'HOUR', 'MINUTE') // 60
335
+ * convertTimeUnit(120, 'SECOND', 'MINUTE') // 2
336
+ * convertTimeUnit(2, 'WEEK', 'DAY') // 14
337
+ * ```
338
+ */
339
+ declare function convertTimeUnit(value: number, fromUnit: TimeUnit, toUnit: TimeUnit): number;
340
+ //#endregion
341
+ //#region src/misc/convertStorage.d.ts
342
+ /**
343
+ * @file storage utils
344
+ * @module Storage
345
+ */
346
+ /**
347
+ * Storage unit conversion constants
348
+ */
349
+ declare const STORAGE_UNITS: {
350
+ readonly BYTE: 1;
351
+ readonly KB: 1024;
352
+ readonly MB: 1048576;
353
+ readonly GB: 1073741824;
354
+ readonly TB: 1099511627776;
355
+ };
356
+ type StorageUnit = keyof typeof STORAGE_UNITS;
357
+ /**
358
+ * Converts storage units to bytes.
359
+ * @param value - The size value.
360
+ * @param fromUnit - The source unit (default: 'MB').
361
+ * @returns The size in bytes.
362
+ * @example
363
+ * ```ts
364
+ * convertToBytes(5, 'MB') // 5242880
365
+ * convertToBytes(1, 'GB') // 1073741824
366
+ * convertToBytes(512, 'KB') // 524288
367
+ * ```
368
+ */
369
+ declare function convertToBytes(value: number, fromUnit?: StorageUnit): number;
370
+ /**
371
+ * Converts bytes to specified storage unit.
372
+ * @param bytes - The size in bytes.
373
+ * @param toUnit - The target unit (default: 'MB').
374
+ * @returns The size in the specified unit.
375
+ * @example
376
+ * ```ts
377
+ * convertFromBytes(5242880, 'MB') // 5
378
+ * convertFromBytes(1073741824, 'GB') // 1
379
+ * convertFromBytes(524288, 'KB') // 512
380
+ * ```
381
+ */
382
+ declare function convertFromBytes(bytes: number, toUnit?: StorageUnit): number;
383
+ /**
384
+ * Converts between storage units.
385
+ * @param value - The size value.
386
+ * @param fromUnit - The source unit.
387
+ * @param toUnit - The target unit.
388
+ * @returns The converted size.
389
+ * @example
390
+ * ```ts
391
+ * convertStorageUnit(1, 'GB', 'MB') // 1024
392
+ * convertStorageUnit(2048, 'MB', 'GB') // 2
393
+ * convertStorageUnit(1024, 'KB', 'MB') // 1
394
+ * ```
395
+ */
396
+ declare function convertStorageUnit(value: number, fromUnit: StorageUnit, toUnit: StorageUnit): number;
397
+ //#endregion
320
398
  //#region src/array/at.d.ts
321
399
  /**
322
400
  * Get array item by index, negative for backward
@@ -771,4 +849,4 @@ declare const RE_LINE_COMMENT: RegExp;
771
849
  */
772
850
  declare const RE_BLOCK_COMMENT: RegExp;
773
851
  //#endregion
774
- 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, SortObjectOptions, ThrottleDebounceOptions, ToIntegerOptions, Whitespace, at, cAF, chunk, clamp, cleanObject, 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 };
852
+ 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, TIME_UNITS, ThrottleDebounceOptions, TimeUnit, ToIntegerOptions, 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,6 +406,135 @@ 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
+
474
+ //#endregion
475
+ //#region src/misc/convertStorage.ts
476
+ /**
477
+ * @file storage utils
478
+ * @module Storage
479
+ */
480
+ /**
481
+ * Storage unit conversion constants
482
+ */
483
+ const STORAGE_UNITS = {
484
+ BYTE: 1,
485
+ KB: 1024,
486
+ MB: 1024 * 1024,
487
+ GB: 1024 * 1024 * 1024,
488
+ TB: 1024 * 1024 * 1024 * 1024
489
+ };
490
+ /**
491
+ * Converts storage units to bytes.
492
+ * @param value - The size value.
493
+ * @param fromUnit - The source unit (default: 'MB').
494
+ * @returns The size in bytes.
495
+ * @example
496
+ * ```ts
497
+ * convertToBytes(5, 'MB') // 5242880
498
+ * convertToBytes(1, 'GB') // 1073741824
499
+ * convertToBytes(512, 'KB') // 524288
500
+ * ```
501
+ */
502
+ function convertToBytes(value, fromUnit = "MB") {
503
+ return value * STORAGE_UNITS[fromUnit];
504
+ }
505
+ /**
506
+ * Converts bytes to specified storage unit.
507
+ * @param bytes - The size in bytes.
508
+ * @param toUnit - The target unit (default: 'MB').
509
+ * @returns The size in the specified unit.
510
+ * @example
511
+ * ```ts
512
+ * convertFromBytes(5242880, 'MB') // 5
513
+ * convertFromBytes(1073741824, 'GB') // 1
514
+ * convertFromBytes(524288, 'KB') // 512
515
+ * ```
516
+ */
517
+ function convertFromBytes(bytes, toUnit = "MB") {
518
+ return bytes / STORAGE_UNITS[toUnit];
519
+ }
520
+ /**
521
+ * Converts between storage units.
522
+ * @param value - The size value.
523
+ * @param fromUnit - The source unit.
524
+ * @param toUnit - The target unit.
525
+ * @returns The converted size.
526
+ * @example
527
+ * ```ts
528
+ * convertStorageUnit(1, 'GB', 'MB') // 1024
529
+ * convertStorageUnit(2048, 'MB', 'GB') // 2
530
+ * convertStorageUnit(1024, 'KB', 'MB') // 1
531
+ * ```
532
+ */
533
+ function convertStorageUnit(value, fromUnit, toUnit) {
534
+ const bytes = convertToBytes(value, fromUnit);
535
+ return convertFromBytes(bytes, toUnit);
536
+ }
537
+
456
538
  //#endregion
457
539
  //#region src/array/at.ts
458
540
  /**
@@ -646,7 +728,7 @@ function toInteger(value, options = {}) {
646
728
  if (onError === "throwError") throw new TypeError(`Cannot convert NaN to an integer`);
647
729
  return onError === "returnOriginal" ? value : defaultValue;
648
730
  }
649
- if (allowDecimal) result = Math.floor(numberValue);
731
+ if (allowDecimal) result = numberValue > 0 ? Math.floor(numberValue) : Math.ceil(numberValue);
650
732
  else {
651
733
  if (numberValue % 1 !== 0) {
652
734
  if (onError === "throwError") throw new Error("Decimal values are not allowed");
@@ -1083,4 +1165,4 @@ const RE_LINE_COMMENT = /\/\/.*/;
1083
1165
  const RE_BLOCK_COMMENT = /\/\*[\s\S]*?\*\//g;
1084
1166
 
1085
1167
  //#endregion
1086
- export { Color, NOOP, RE_BLOCK_COMMENT, RE_COMMENTS, RE_LINE_COMMENT, SPECIAL_CHAR, at, cAF, chunk, clamp, cleanObject, 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,7 @@
1
1
  {
2
2
  "name": "@ntnyq/utils",
3
3
  "type": "module",
4
- "version": "0.8.1",
4
+ "version": "0.9.0",
5
5
  "description": "Common used utils.",
6
6
  "keywords": [
7
7
  "utils"
@@ -30,16 +30,16 @@
30
30
  ],
31
31
  "sideEffects": false,
32
32
  "devDependencies": {
33
- "@ntnyq/eslint-config": "^5.0.0",
33
+ "@ntnyq/eslint-config": "^5.2.0",
34
34
  "@ntnyq/prettier-config": "^3.0.1",
35
- "bumpp": "^10.2.0",
36
- "eslint": "^9.30.1",
35
+ "bumpp": "^10.2.2",
36
+ "eslint": "^9.32.0",
37
37
  "husky": "^9.1.7",
38
38
  "nano-staged": "^0.8.0",
39
39
  "npm-run-all2": "^8.0.4",
40
40
  "prettier": "^3.6.2",
41
- "tsdown": "^0.12.9",
42
- "typescript": "^5.8.3",
41
+ "tsdown": "^0.13.1",
42
+ "typescript": "^5.9.2",
43
43
  "vitest": "^3.2.4"
44
44
  },
45
45
  "engines": {