@ntnyq/utils 0.8.0 → 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 [];
@@ -183,7 +182,7 @@ declare function isElementVisibleInViewport(element: HTMLElement, targetWindow?:
183
182
  *
184
183
  * @returns boolean - true if the code is running in a browser
185
184
  */
186
- declare const isBrowser: () => boolean;
185
+ declare function isBrowser(): boolean;
187
186
  //#endregion
188
187
  //#region src/file/removeExtension.d.ts
189
188
  /**
@@ -205,6 +204,10 @@ declare function escapeHTML(str: string): string;
205
204
  declare function unescapeHTML(str: string): string;
206
205
  //#endregion
207
206
  //#region src/misc/raf.d.ts
207
+ /**
208
+ * Gets the global root object.
209
+ * @returns the global root object
210
+ */
208
211
  declare function getRoot(): Window | typeof globalThis;
209
212
  /**
210
213
  * Request animation frame
@@ -226,15 +229,35 @@ declare function cAF(id: number): void;
226
229
  * @file time utils
227
230
  * @module Time
228
231
  */
229
- declare const ONE_SECOND_MILLSECONDS: number;
230
- declare const ONE_MINUTE_MILLSECONDS: number;
231
- declare const ONE_HOUR_MILLSECONDS: number;
232
- declare const ONE_DAY_MILLSECONDS: number;
233
- declare const ONE_WEEK_MILLSECONDS: number;
232
+ /**
233
+ * Converts seconds to milliseconds.
234
+ * @param count - The number of seconds.
235
+ * @returns The equivalent number of milliseconds.
236
+ */
234
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
+ */
235
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
+ */
236
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
+ */
237
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
+ */
238
261
  declare function weeks(count: number): number;
239
262
  //#endregion
240
263
  //#region src/misc/clamp.d.ts
@@ -293,6 +316,62 @@ declare function debounce<T extends ((...args: any[]) => undefined | void) | und
293
316
  */
294
317
  declare function warnOnce(message: string): void;
295
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
296
375
  //#region src/array/at.d.ts
297
376
  /**
298
377
  * Get array item by index, negative for backward
@@ -747,4 +826,4 @@ declare const RE_LINE_COMMENT: RegExp;
747
826
  */
748
827
  declare const RE_BLOCK_COMMENT: RegExp;
749
828
  //#endregion
750
- export { AnyFn, Arrayable, Awaitable, Callable, CleanObjectOptions, Color, CreatePadStringOptions, DeepRequired, GetStringSimilarityOptions, InteropModuleDefault, JsonArray, JsonObject, JsonPrimitive, JsonValue, LiteralUnion, MayBe, Merge, NOOP, NonEmptyObject, NonEmptyString, Nullable, ONE_DAY_MILLSECONDS, ONE_HOUR_MILLSECONDS, ONE_MINUTE_MILLSECONDS, ONE_SECOND_MILLSECONDS, ONE_WEEK_MILLSECONDS, 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
@@ -221,7 +221,9 @@ function isElementVisibleInViewport(element, targetWindow = window) {
221
221
  *
222
222
  * @returns boolean - true if the code is running in a browser
223
223
  */
224
- const isBrowser = () => typeof document !== "undefined";
224
+ function isBrowser() {
225
+ return typeof document !== "undefined";
226
+ }
225
227
 
226
228
  //#endregion
227
229
  //#region src/file/removeExtension.ts
@@ -273,17 +275,13 @@ function unescapeHTML(str) {
273
275
 
274
276
  //#endregion
275
277
  //#region src/misc/raf.ts
278
+ /**
279
+ * Gets the global root object.
280
+ * @returns the global root object
281
+ */
276
282
  function getRoot() {
277
283
  return isBrowser() ? window : globalThis;
278
284
  }
279
- let prev = Date.now();
280
- function mockRAF(fn) {
281
- const curr = Date.now();
282
- const ms = Math.max(0, 16 - (curr - prev));
283
- const id = setTimeout(fn, ms);
284
- prev = curr + ms;
285
- return id;
286
- }
287
285
  /**
288
286
  * Request animation frame
289
287
  *
@@ -292,7 +290,7 @@ function mockRAF(fn) {
292
290
  */
293
291
  function rAF(fn) {
294
292
  const root = getRoot();
295
- const raf = root.requestAnimationFrame || mockRAF;
293
+ const raf = root.requestAnimationFrame;
296
294
  return raf.call(root, fn);
297
295
  }
298
296
  /**
@@ -303,7 +301,7 @@ function rAF(fn) {
303
301
  */
304
302
  function cAF(id) {
305
303
  const root = getRoot();
306
- const caf = root.cancelAnimationFrame || root.clearTimeout;
304
+ const caf = root.cancelAnimationFrame;
307
305
  return caf.call(root, id);
308
306
  }
309
307
 
@@ -313,25 +311,45 @@ function cAF(id) {
313
311
  * @file time utils
314
312
  * @module Time
315
313
  */
316
- const ONE_SECOND_MILLSECONDS = 1e3;
317
- const ONE_MINUTE_MILLSECONDS = 60 * 1e3;
318
- const ONE_HOUR_MILLSECONDS = 60 * 60 * 1e3;
319
- const ONE_DAY_MILLSECONDS = 24 * 60 * 60 * 1e3;
320
- const ONE_WEEK_MILLSECONDS = 7 * 24 * 60 * 60 * 1e3;
314
+ /**
315
+ * Converts seconds to milliseconds.
316
+ * @param count - The number of seconds.
317
+ * @returns The equivalent number of milliseconds.
318
+ */
321
319
  function seconds(count) {
322
- return count * ONE_SECOND_MILLSECONDS;
320
+ return count * 1e3;
323
321
  }
322
+ /**
323
+ * Converts minutes to milliseconds.
324
+ * @param count - The number of minutes.
325
+ * @returns The equivalent number of milliseconds.
326
+ */
324
327
  function minutes(count) {
325
- return count * ONE_MINUTE_MILLSECONDS;
328
+ return count * 60 * 1e3;
326
329
  }
330
+ /**
331
+ * Converts hours to milliseconds.
332
+ * @param count - The number of hours.
333
+ * @returns The equivalent number of milliseconds.
334
+ */
327
335
  function hours(count) {
328
- return count * ONE_HOUR_MILLSECONDS;
336
+ return count * 60 * 60 * 1e3;
329
337
  }
338
+ /**
339
+ * Converts days to milliseconds.
340
+ * @param count - The number of days.
341
+ * @returns The equivalent number of milliseconds.
342
+ */
330
343
  function days(count) {
331
- return count * ONE_DAY_MILLSECONDS;
344
+ return count * 24 * 60 * 60 * 1e3;
332
345
  }
346
+ /**
347
+ * Converts weeks to milliseconds.
348
+ * @param count - The number of weeks.
349
+ * @returns The equivalent number of milliseconds.
350
+ */
333
351
  function weeks(count) {
334
- return count * ONE_WEEK_MILLSECONDS;
352
+ return count * 7 * 24 * 60 * 60 * 1e3;
335
353
  }
336
354
 
337
355
  //#endregion
@@ -435,6 +453,69 @@ function warnOnce(message) {
435
453
  console.warn(message);
436
454
  }
437
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
+
438
519
  //#endregion
439
520
  //#region src/array/at.ts
440
521
  /**
@@ -628,7 +709,7 @@ function toInteger(value, options = {}) {
628
709
  if (onError === "throwError") throw new TypeError(`Cannot convert NaN to an integer`);
629
710
  return onError === "returnOriginal" ? value : defaultValue;
630
711
  }
631
- if (allowDecimal) result = Math.floor(numberValue);
712
+ if (allowDecimal) result = numberValue > 0 ? Math.floor(numberValue) : Math.ceil(numberValue);
632
713
  else {
633
714
  if (numberValue % 1 !== 0) {
634
715
  if (onError === "throwError") throw new Error("Decimal values are not allowed");
@@ -1065,4 +1146,4 @@ const RE_LINE_COMMENT = /\/\/.*/;
1065
1146
  const RE_BLOCK_COMMENT = /\/\*[\s\S]*?\*\//g;
1066
1147
 
1067
1148
  //#endregion
1068
- export { Color, NOOP, ONE_DAY_MILLSECONDS, ONE_HOUR_MILLSECONDS, ONE_MINUTE_MILLSECONDS, ONE_SECOND_MILLSECONDS, ONE_WEEK_MILLSECONDS, 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.0",
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": {