@ntnyq/utils 0.8.1 → 0.8.2

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/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 [];
@@ -317,6 +316,62 @@ declare function debounce<T extends ((...args: any[]) => undefined | void) | und
317
316
  */
318
317
  declare function warnOnce(message: string): void;
319
318
  //#endregion
319
+ //#region src/misc/convertStorage.d.ts
320
+ /**
321
+ * @file src/misc/transform.ts
322
+ */
323
+ /**
324
+ * Storage unit conversion constants
325
+ */
326
+ declare const STORAGE_UNITS: {
327
+ readonly BYTE: 1;
328
+ readonly KB: 1024;
329
+ readonly MB: 1048576;
330
+ readonly GB: 1073741824;
331
+ readonly TB: 1099511627776;
332
+ };
333
+ type StorageUnit = keyof typeof STORAGE_UNITS;
334
+ /**
335
+ * Converts storage units to bytes.
336
+ * @param value - The size value.
337
+ * @param fromUnit - The source unit (default: 'MB').
338
+ * @returns The size in bytes.
339
+ * @example
340
+ * ```ts
341
+ * convertToBytes(5, 'MB') // 5242880
342
+ * convertToBytes(1, 'GB') // 1073741824
343
+ * convertToBytes(512, 'KB') // 524288
344
+ * ```
345
+ */
346
+ declare function convertToBytes(value: number, fromUnit?: StorageUnit): number;
347
+ /**
348
+ * Converts bytes to specified storage unit.
349
+ * @param bytes - The size in bytes.
350
+ * @param toUnit - The target unit (default: 'MB').
351
+ * @returns The size in the specified unit.
352
+ * @example
353
+ * ```ts
354
+ * convertFromBytes(5242880, 'MB') // 5
355
+ * convertFromBytes(1073741824, 'GB') // 1
356
+ * convertFromBytes(524288, 'KB') // 512
357
+ * ```
358
+ */
359
+ declare function convertFromBytes(bytes: number, toUnit?: StorageUnit): number;
360
+ /**
361
+ * Converts between storage units.
362
+ * @param value - The size value.
363
+ * @param fromUnit - The source unit.
364
+ * @param toUnit - The target unit.
365
+ * @returns The converted size.
366
+ * @example
367
+ * ```ts
368
+ * convertStorageUnit(1, 'GB', 'MB') // 1024
369
+ * convertStorageUnit(2048, 'MB', 'GB') // 2
370
+ * convertStorageUnit(1024, 'KB', 'MB') // 1
371
+ * ```
372
+ */
373
+ declare function convertStorageUnit(value: number, fromUnit: StorageUnit, toUnit: StorageUnit): number;
374
+ //#endregion
320
375
  //#region src/array/at.d.ts
321
376
  /**
322
377
  * Get array item by index, negative for backward
@@ -771,4 +826,4 @@ declare const RE_LINE_COMMENT: RegExp;
771
826
  */
772
827
  declare const RE_BLOCK_COMMENT: RegExp;
773
828
  //#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 };
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 };
package/dist/index.js CHANGED
@@ -453,6 +453,69 @@ function warnOnce(message) {
453
453
  console.warn(message);
454
454
  }
455
455
 
456
+ //#endregion
457
+ //#region src/misc/convertStorage.ts
458
+ /**
459
+ * @file src/misc/transform.ts
460
+ */
461
+ /**
462
+ * Storage unit conversion constants
463
+ */
464
+ const STORAGE_UNITS = {
465
+ BYTE: 1,
466
+ KB: 1024,
467
+ MB: 1024 * 1024,
468
+ GB: 1024 * 1024 * 1024,
469
+ TB: 1024 * 1024 * 1024 * 1024
470
+ };
471
+ /**
472
+ * Converts storage units to bytes.
473
+ * @param value - The size value.
474
+ * @param fromUnit - The source unit (default: 'MB').
475
+ * @returns The size in bytes.
476
+ * @example
477
+ * ```ts
478
+ * convertToBytes(5, 'MB') // 5242880
479
+ * convertToBytes(1, 'GB') // 1073741824
480
+ * convertToBytes(512, 'KB') // 524288
481
+ * ```
482
+ */
483
+ function convertToBytes(value, fromUnit = "MB") {
484
+ return value * STORAGE_UNITS[fromUnit];
485
+ }
486
+ /**
487
+ * Converts bytes to specified storage unit.
488
+ * @param bytes - The size in bytes.
489
+ * @param toUnit - The target unit (default: 'MB').
490
+ * @returns The size in the specified unit.
491
+ * @example
492
+ * ```ts
493
+ * convertFromBytes(5242880, 'MB') // 5
494
+ * convertFromBytes(1073741824, 'GB') // 1
495
+ * convertFromBytes(524288, 'KB') // 512
496
+ * ```
497
+ */
498
+ function convertFromBytes(bytes, toUnit = "MB") {
499
+ return bytes / STORAGE_UNITS[toUnit];
500
+ }
501
+ /**
502
+ * Converts between storage units.
503
+ * @param value - The size value.
504
+ * @param fromUnit - The source unit.
505
+ * @param toUnit - The target unit.
506
+ * @returns The converted size.
507
+ * @example
508
+ * ```ts
509
+ * convertStorageUnit(1, 'GB', 'MB') // 1024
510
+ * convertStorageUnit(2048, 'MB', 'GB') // 2
511
+ * convertStorageUnit(1024, 'KB', 'MB') // 1
512
+ * ```
513
+ */
514
+ function convertStorageUnit(value, fromUnit, toUnit) {
515
+ const bytes = convertToBytes(value, fromUnit);
516
+ return convertFromBytes(bytes, toUnit);
517
+ }
518
+
456
519
  //#endregion
457
520
  //#region src/array/at.ts
458
521
  /**
@@ -646,7 +709,7 @@ function toInteger(value, options = {}) {
646
709
  if (onError === "throwError") throw new TypeError(`Cannot convert NaN to an integer`);
647
710
  return onError === "returnOriginal" ? value : defaultValue;
648
711
  }
649
- if (allowDecimal) result = Math.floor(numberValue);
712
+ if (allowDecimal) result = numberValue > 0 ? Math.floor(numberValue) : Math.ceil(numberValue);
650
713
  else {
651
714
  if (numberValue % 1 !== 0) {
652
715
  if (onError === "throwError") throw new Error("Decimal values are not allowed");
@@ -1083,4 +1146,4 @@ const RE_LINE_COMMENT = /\/\/.*/;
1083
1146
  const RE_BLOCK_COMMENT = /\/\*[\s\S]*?\*\//g;
1084
1147
 
1085
1148
  //#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 };
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 };
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.8.2",
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": {