@ntnyq/utils 0.8.2 → 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.
package/dist/index.d.ts CHANGED
@@ -224,42 +224,6 @@ declare function rAF(fn: FrameRequestCallback): number;
224
224
  */
225
225
  declare function cAF(id: number): void;
226
226
  //#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
227
  //#region src/misc/clamp.d.ts
264
228
  /**
265
229
  * Clamps a number between a minimum and maximum value
@@ -316,9 +280,68 @@ declare function debounce<T extends ((...args: any[]) => undefined | void) | und
316
280
  */
317
281
  declare function warnOnce(message: string): void;
318
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
319
341
  //#region src/misc/convertStorage.d.ts
320
342
  /**
321
- * @file src/misc/transform.ts
343
+ * @file storage utils
344
+ * @module Storage
322
345
  */
323
346
  /**
324
347
  * Storage unit conversion constants
@@ -826,4 +849,4 @@ declare const RE_LINE_COMMENT: RegExp;
826
849
  */
827
850
  declare const RE_BLOCK_COMMENT: RegExp;
828
851
  //#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 };
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,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,7 @@
1
1
  {
2
2
  "name": "@ntnyq/utils",
3
3
  "type": "module",
4
- "version": "0.8.2",
4
+ "version": "0.9.0",
5
5
  "description": "Common used utils.",
6
6
  "keywords": [
7
7
  "utils"