@ntnyq/utils 0.4.5 → 0.5.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.
package/dist/index.cjs CHANGED
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ Color: () => Color,
23
24
  NOOP: () => NOOP,
24
25
  at: () => at,
25
26
  cAF: () => cAF,
@@ -28,8 +29,10 @@ __export(index_exports, {
28
29
  chunk: () => chunk,
29
30
  clamp: () => clamp,
30
31
  cleanObject: () => cleanObject,
32
+ createPadString: () => createPadString,
31
33
  days: () => days,
32
34
  debounce: () => debounce,
35
+ enhance: () => enhance,
33
36
  ensurePrefix: () => ensurePrefix,
34
37
  ensureSuffix: () => ensureSuffix,
35
38
  escapeHtml: () => escapeHtml,
@@ -39,6 +42,7 @@ __export(index_exports, {
39
42
  hasOwn: () => hasOwn,
40
43
  hours: () => hours,
41
44
  interopDefault: () => interopDefault,
45
+ intersect: () => intersect,
42
46
  isArray: () => isArray,
43
47
  isArrayEqual: () => isArrayEqual,
44
48
  isBigInt: () => isBigInt,
@@ -86,6 +90,11 @@ __export(index_exports, {
86
90
  pascalCase: () => pascalCase,
87
91
  pick: () => pick,
88
92
  rAF: () => rAF,
93
+ randomHexColor: () => randomHexColor,
94
+ randomNumber: () => randomNumber,
95
+ randomRGBAColor: () => randomRGBAColor,
96
+ randomRGBColor: () => randomRGBColor,
97
+ randomString: () => randomString,
89
98
  resolveSubOptions: () => resolveSubOptions,
90
99
  seconds: () => seconds,
91
100
  slash: () => slash,
@@ -124,7 +133,9 @@ function isDeepEqual(value1, value2) {
124
133
  if (keys.length !== Object.keys(value2).length) {
125
134
  return false;
126
135
  }
127
- return keys.every((key) => isDeepEqual(value1[key], value2[key]));
136
+ return keys.every(
137
+ (key) => isDeepEqual(value1[key], value2[key])
138
+ );
128
139
  }
129
140
  return Object.is(value1, value2);
130
141
  }
@@ -334,7 +345,10 @@ function throttle(delay, callback, options = {}) {
334
345
  if (!isDebounce && elapsed > delay) {
335
346
  exec(now);
336
347
  } else {
337
- timeoutId = setTimeout(isDebounce ? clear : exec, isDebounce ? delay : delay - elapsed);
348
+ timeoutId = setTimeout(
349
+ isDebounce ? clear : exec,
350
+ isDebounce ? delay : delay - elapsed
351
+ );
338
352
  }
339
353
  }
340
354
  wrapper.cancel = cancel;
@@ -412,6 +426,11 @@ function mergeArrayable(...args) {
412
426
  return args.flatMap((i) => toArray(i));
413
427
  }
414
428
 
429
+ // src/array/intersect.ts
430
+ function intersect(a, b) {
431
+ return a.filter((item) => b.includes(item));
432
+ }
433
+
415
434
  // src/array/isArrayEqual.ts
416
435
  function isArrayEqual(array1, array2) {
417
436
  if (array1.length !== array2.length) {
@@ -420,6 +439,12 @@ function isArrayEqual(array1, array2) {
420
439
  return array1.every((item, idx) => item === array2[idx]);
421
440
  }
422
441
 
442
+ // src/string/pad.ts
443
+ function createPadString(options) {
444
+ const { length, char } = options;
445
+ return (value) => (char.repeat(length) + value).slice(-length);
446
+ }
447
+
423
448
  // src/string/join.ts
424
449
  function join(array, options = {}) {
425
450
  const { separator = "" } = options;
@@ -432,6 +457,30 @@ function slash(input) {
432
457
  return input.replace(/\\/g, "/");
433
458
  }
434
459
 
460
+ // src/number/random.ts
461
+ function randomNumber(min, max = 0, options = {}) {
462
+ if (max === 0) {
463
+ max = min;
464
+ min = 0;
465
+ }
466
+ if (min > max) {
467
+ ;
468
+ [min, max] = [max, min];
469
+ }
470
+ return Math.trunc(
471
+ Math.random() * (max - min + (options.includeMax ? 1 : 0)) + min
472
+ );
473
+ }
474
+
475
+ // src/string/random.ts
476
+ function randomString(length = 16, chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") {
477
+ const result = [];
478
+ for (let i = length; i > 0; --i) {
479
+ result.push(chars[randomNumber(chars.length)]);
480
+ }
481
+ return result.join("");
482
+ }
483
+
435
484
  // src/string/unindent.ts
436
485
  var _RE_FULL_WS = /^\s*$/;
437
486
  function unindent(input) {
@@ -465,6 +514,119 @@ function ensureSuffix(input, suffix) {
465
514
  return input.endsWith(suffix) ? input : `${input}${suffix}`;
466
515
  }
467
516
 
517
+ // src/color/color.ts
518
+ var pad2 = createPadString({ length: 2, char: "0" });
519
+ var RE_VALID_HEX_COLOR = /^#(?:[0-9a-f]{6}|[0-9a-f]{3})$/i;
520
+ function validateHexColor(hex) {
521
+ if (hex.length !== 4 && hex.length !== 7) return false;
522
+ if (!hex.startsWith("#")) return false;
523
+ return RE_VALID_HEX_COLOR.test(hex);
524
+ }
525
+ function normalizeHexString(hex) {
526
+ return hex.length === 6 ? hex : hex.replace(/./g, "$&$&");
527
+ }
528
+ var Color = class _Color {
529
+ constructor(red = 0, green = 0, blue = 0, alpha = 1) {
530
+ this.red = red;
531
+ this.green = green;
532
+ this.blue = blue;
533
+ this.alpha = alpha;
534
+ }
535
+ static fromRGB(red, green, blue) {
536
+ return new _Color(red, green, blue);
537
+ }
538
+ static fromRGBA(red, green, blue, alpha) {
539
+ return new _Color(red, green, blue, alpha);
540
+ }
541
+ static fromHex(hex) {
542
+ if (!validateHexColor(hex)) {
543
+ throw new Error("Invalid hex color");
544
+ }
545
+ const [red, green, blue] = normalizeHexString(hex.slice(1)).match(/.{2}/g)?.map((value) => Number.parseInt(value, 16)) ?? [0, 0, 0];
546
+ return new _Color(red, green, blue);
547
+ }
548
+ get brightness() {
549
+ return (this.red * 299 + this.green * 587 + this.blue * 114) / 1e3;
550
+ }
551
+ get isDark() {
552
+ return this.brightness < 128;
553
+ }
554
+ get isLight() {
555
+ return !this.isDark;
556
+ }
557
+ toHexString(isUpperCase = true) {
558
+ const hexString = `#${pad2(this.red.toString(16))}${pad2(this.green.toString(16))}${pad2(this.blue.toString(16))}`;
559
+ return isUpperCase ? hexString.toUpperCase() : hexString;
560
+ }
561
+ toRGBAString() {
562
+ return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`;
563
+ }
564
+ /**
565
+ * add alpha value to {@link Color}
566
+ *
567
+ * @param alpha - alpha value
568
+ * @returns instance of {@link Color}
569
+ */
570
+ withAlpha(alpha = 1) {
571
+ return new _Color(this.red, this.green, this.blue, alpha);
572
+ }
573
+ /**
574
+ * lighten the color by percentage
575
+ *
576
+ * @param percentage - percentage to lighten
577
+ */
578
+ lighten(percentage = 0) {
579
+ const amount = Math.round(percentage / 100 * 255);
580
+ return new _Color(
581
+ Math.min(this.red + amount, 255),
582
+ Math.min(this.green + amount, 255),
583
+ Math.min(this.blue + amount, 255),
584
+ this.alpha
585
+ );
586
+ }
587
+ /**
588
+ * darken the color by percentage
589
+ *
590
+ * @param percentage - percentage to darken
591
+ */
592
+ darken(percentage = 0) {
593
+ const amount = Math.round(percentage / 100 * 255);
594
+ return new _Color(
595
+ Math.max(this.red - amount, 0),
596
+ Math.max(this.green - amount, 0),
597
+ Math.max(this.blue - amount, 0),
598
+ this.alpha
599
+ );
600
+ }
601
+ };
602
+
603
+ // src/color/random.ts
604
+ var MAX_RGB = 255;
605
+ function randomRGBColor() {
606
+ return `rgb(${randomNumber(MAX_RGB)}, ${randomNumber(MAX_RGB)}, ${randomNumber(MAX_RGB)})`;
607
+ }
608
+ function randomRGBAColor() {
609
+ return `rgba(${randomNumber(MAX_RGB)}, ${randomNumber(MAX_RGB)}, ${randomNumber(MAX_RGB)}, ${Math.random().toFixed(1)})`;
610
+ }
611
+ function randomHexColor() {
612
+ return `#${Math.random().toString(16).slice(2, 8)}`;
613
+ }
614
+
615
+ // src/proxy/enhance.ts
616
+ function enhance(module2, extra) {
617
+ return new Proxy(module2, {
618
+ get(target, key, receiver) {
619
+ if (Reflect.has(extra, key)) {
620
+ return Reflect.get(extra, key, receiver);
621
+ }
622
+ return Reflect.get(target, key, receiver);
623
+ },
624
+ has(target, key) {
625
+ return Reflect.has(extra, key) || Reflect.has(target, key);
626
+ }
627
+ });
628
+ }
629
+
468
630
  // src/object/omit.ts
469
631
  function omit(object, ...keys) {
470
632
  keys.forEach((key) => delete object[key]);
@@ -496,11 +658,11 @@ function cleanObject(obj, options = {}) {
496
658
  const {
497
659
  cleanUndefined = true,
498
660
  cleanNull = true,
499
- cleanZero = false,
500
661
  cleanNaN = true,
501
- cleanEmptyString = true,
502
- cleanEmptyArray = true,
503
- cleanEmptyObject = true,
662
+ cleanZero = false,
663
+ cleanEmptyString = false,
664
+ cleanEmptyArray = false,
665
+ cleanEmptyObject = false,
504
666
  recursive = true
505
667
  } = options;
506
668
  Object.keys(obj).forEach((key) => {
@@ -659,6 +821,7 @@ function resolveSubOptions(options, key) {
659
821
  }
660
822
  // Annotate the CommonJS export names for ESM import in node:
661
823
  0 && (module.exports = {
824
+ Color,
662
825
  NOOP,
663
826
  at,
664
827
  cAF,
@@ -667,8 +830,10 @@ function resolveSubOptions(options, key) {
667
830
  chunk,
668
831
  clamp,
669
832
  cleanObject,
833
+ createPadString,
670
834
  days,
671
835
  debounce,
836
+ enhance,
672
837
  ensurePrefix,
673
838
  ensureSuffix,
674
839
  escapeHtml,
@@ -678,6 +843,7 @@ function resolveSubOptions(options, key) {
678
843
  hasOwn,
679
844
  hours,
680
845
  interopDefault,
846
+ intersect,
681
847
  isArray,
682
848
  isArrayEqual,
683
849
  isBigInt,
@@ -725,6 +891,11 @@ function resolveSubOptions(options, key) {
725
891
  pascalCase,
726
892
  pick,
727
893
  rAF,
894
+ randomHexColor,
895
+ randomNumber,
896
+ randomRGBAColor,
897
+ randomRGBColor,
898
+ randomString,
728
899
  resolveSubOptions,
729
900
  seconds,
730
901
  slash,
package/dist/index.d.cts CHANGED
@@ -207,7 +207,10 @@ type Awaitable<T> = Promise<T> | T;
207
207
  type Callable<T> = AnyFn<any, T> | T;
208
208
  type MayBe<T> = T | undefined;
209
209
  type Nullable<T> = T | null;
210
- type PrimitiveType = bigint | boolean | number | string | symbol | null | undefined;
210
+ /**
211
+ * Overwrite some keys type
212
+ */
213
+ type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
211
214
  /**
212
215
  * Prettify object type
213
216
  */
@@ -215,10 +218,7 @@ type Prettify<T> = {
215
218
  [K in keyof T]: T[K];
216
219
  } & {};
217
220
  type PrettifyV2<T> = Omit<T, never>;
218
- /**
219
- * Overwrite some keys type
220
- */
221
- type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
221
+ type PrimitiveType = bigint | boolean | number | string | symbol | null | undefined;
222
222
  /**
223
223
  * Resolve `boolean | Record<string, any>` to `Record<string, any>`
224
224
  */
@@ -244,8 +244,78 @@ declare function flattenArrayable<T>(array?: Nullable<Arrayable<T | Array<T>>>):
244
244
  */
245
245
  declare function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>;
246
246
 
247
+ /**
248
+ * Get intersect items
249
+ *
250
+ * @returns intersect items
251
+ */
252
+ declare function intersect<T>(a: T[], b: T[]): T[];
253
+
247
254
  declare function isArrayEqual(array1: unknown[], array2: unknown[]): boolean;
248
255
 
256
+ declare class Color {
257
+ red: number;
258
+ green: number;
259
+ blue: number;
260
+ alpha: number;
261
+ constructor(red?: number, green?: number, blue?: number, alpha?: number);
262
+ static fromRGB(red: number, green: number, blue: number): Color;
263
+ static fromRGBA(red: number, green: number, blue: number, alpha: number): Color;
264
+ static fromHex(hex: string): Color;
265
+ get brightness(): number;
266
+ get isDark(): boolean;
267
+ get isLight(): boolean;
268
+ toHexString(isUpperCase?: boolean): string;
269
+ toRGBAString(): string;
270
+ /**
271
+ * add alpha value to {@link Color}
272
+ *
273
+ * @param alpha - alpha value
274
+ * @returns instance of {@link Color}
275
+ */
276
+ withAlpha(alpha?: number): Color;
277
+ /**
278
+ * lighten the color by percentage
279
+ *
280
+ * @param percentage - percentage to lighten
281
+ */
282
+ lighten(percentage?: number): Color;
283
+ /**
284
+ * darken the color by percentage
285
+ *
286
+ * @param percentage - percentage to darken
287
+ */
288
+ darken(percentage?: number): Color;
289
+ }
290
+
291
+ /**
292
+ * get a random RGB color
293
+ * @returns a random RGB color
294
+ */
295
+ declare function randomRGBColor(): string;
296
+ /**
297
+ * get a random RGBA color
298
+ * @returns a random RGBA color
299
+ */
300
+ declare function randomRGBAColor(): string;
301
+ /**
302
+ * get a random hex color
303
+ * @returns a random hex color
304
+ */
305
+ declare function randomHexColor(): string;
306
+
307
+ /**
308
+ * enhance object
309
+ * @module proxy
310
+ */
311
+ declare function enhance<T extends Record<PropertyKey, any>, E extends Record<PropertyKey, any>>(module: T, extra: E): T;
312
+
313
+ interface CreatePadStringOptions {
314
+ length: number;
315
+ char: string;
316
+ }
317
+ declare function createPadString(options: CreatePadStringOptions): (value: string) => string;
318
+
249
319
  type JoinableValue = string | number | null | undefined;
250
320
  interface JoinOptions {
251
321
  /**
@@ -266,6 +336,15 @@ declare function join(array: JoinableValue[], options?: JoinOptions): string;
266
336
  */
267
337
  declare function slash(input: string): string;
268
338
 
339
+ /**
340
+ * randome a string useing given chars
341
+ *
342
+ * @param length - string length
343
+ * @param chars - string chars
344
+ * @returns random string
345
+ */
346
+ declare function randomString(length?: number, chars?: string): string;
347
+
269
348
  /**
270
349
  * Remove common leading whitespace from a template string
271
350
  * Empty lines at the beginning and end of the template string are also removed.
@@ -319,19 +398,19 @@ interface CleanObjectOptions {
319
398
  /**
320
399
  * clean empty string
321
400
  *
322
- * @default true
401
+ * @default false
323
402
  */
324
403
  cleanEmptyString?: boolean;
325
404
  /**
326
405
  * clean empty array
327
406
  *
328
- * @default true
407
+ * @default false
329
408
  */
330
409
  cleanEmptyArray?: boolean;
331
410
  /**
332
411
  * clean empty object
333
412
  *
334
- * @default true
413
+ * @default false
335
414
  */
336
415
  cleanEmptyObject?: boolean;
337
416
  /**
@@ -428,4 +507,21 @@ declare function interopDefault<T>(mod: Awaitable<T>): Promise<InteropModuleDefa
428
507
  */
429
508
  declare function resolveSubOptions<T extends Record<string, any>, K extends keyof T>(options: T, key: K): Partial<ResolvedOptions<T[K]>>;
430
509
 
431
- export { type AnyFn, type Arrayable, type Awaitable, type Callable, type CleanObjectOptions, type InteropModuleDefault, type JsonArray, type JsonObject, type JsonPrimitive, type JsonValue, type MayBe, NOOP, type NonEmptyString, type Nullable, type Overwrite, type Prettify, type PrettifyV2, type PrimitiveType, type ResolvedOptions, type SortObjectOptions, type ThrottleDebounceOptions, type Whitespace, at, cAF, capitalize, chunk, clamp, cleanObject, days, debounce, ensurePrefix, ensureSuffix, escapeHtml, flattenArrayable, getObjectType, hasOwn, hours, interopDefault, isArray, isArrayEqual, isBigInt, isBoolean, isBrowser, isDeepEqual, isEmptyArray, isEmptyMap, isEmptyObject, isEmptySet, isEmptyString, isEmptyStringOrWhitespace, isError, isFunction, isInteger, isIterable, isMap, isNaN, isNativePromise, isNil, isNonEmptyArray, isNonEmptyString, isNull, isNullOrUndefined, isNumber, isNumbericString, isObject, isPromise, isRegExp, isSet, isString, isUndefined, isWhitespaceString, isZero, join, last, mergeArrayable, minutes, noop, omit, once, pick, rAF, resolveSubOptions, seconds, slash, sortObject, throttle, toArray, unindent, unique, uniqueBy, waitFor, warnOnce, weeks };
510
+ interface RamdomNumberOptions {
511
+ /**
512
+ * include max value
513
+ *
514
+ * @default false
515
+ */
516
+ includeMax?: boolean;
517
+ }
518
+ /**
519
+ * random an integer by given range
520
+ *
521
+ * @param min - min value
522
+ * @param max - max value
523
+ * @returns random integer in range
524
+ */
525
+ declare function randomNumber(min: number, max?: number, options?: RamdomNumberOptions): number;
526
+
527
+ export { type AnyFn, type Arrayable, type Awaitable, type Callable, type CleanObjectOptions, Color, type CreatePadStringOptions, type InteropModuleDefault, type JsonArray, type JsonObject, type JsonPrimitive, type JsonValue, type MayBe, NOOP, type NonEmptyString, type Nullable, type Overwrite, type Prettify, type PrettifyV2, type PrimitiveType, type RamdomNumberOptions, type ResolvedOptions, type SortObjectOptions, type ThrottleDebounceOptions, type Whitespace, at, cAF, capitalize, chunk, clamp, cleanObject, createPadString, days, debounce, enhance, ensurePrefix, ensureSuffix, escapeHtml, flattenArrayable, getObjectType, hasOwn, hours, interopDefault, intersect, isArray, isArrayEqual, isBigInt, isBoolean, isBrowser, isDeepEqual, isEmptyArray, isEmptyMap, isEmptyObject, isEmptySet, isEmptyString, isEmptyStringOrWhitespace, isError, isFunction, isInteger, isIterable, isMap, isNaN, isNativePromise, isNil, isNonEmptyArray, isNonEmptyString, isNull, isNullOrUndefined, isNumber, isNumbericString, isObject, isPromise, isRegExp, isSet, isString, isUndefined, isWhitespaceString, isZero, join, last, mergeArrayable, minutes, noop, omit, once, pick, rAF, randomHexColor, randomNumber, randomRGBAColor, randomRGBColor, randomString, resolveSubOptions, seconds, slash, sortObject, throttle, toArray, unindent, unique, uniqueBy, waitFor, warnOnce, weeks };
package/dist/index.d.ts CHANGED
@@ -207,7 +207,10 @@ type Awaitable<T> = Promise<T> | T;
207
207
  type Callable<T> = AnyFn<any, T> | T;
208
208
  type MayBe<T> = T | undefined;
209
209
  type Nullable<T> = T | null;
210
- type PrimitiveType = bigint | boolean | number | string | symbol | null | undefined;
210
+ /**
211
+ * Overwrite some keys type
212
+ */
213
+ type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
211
214
  /**
212
215
  * Prettify object type
213
216
  */
@@ -215,10 +218,7 @@ type Prettify<T> = {
215
218
  [K in keyof T]: T[K];
216
219
  } & {};
217
220
  type PrettifyV2<T> = Omit<T, never>;
218
- /**
219
- * Overwrite some keys type
220
- */
221
- type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
221
+ type PrimitiveType = bigint | boolean | number | string | symbol | null | undefined;
222
222
  /**
223
223
  * Resolve `boolean | Record<string, any>` to `Record<string, any>`
224
224
  */
@@ -244,8 +244,78 @@ declare function flattenArrayable<T>(array?: Nullable<Arrayable<T | Array<T>>>):
244
244
  */
245
245
  declare function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>;
246
246
 
247
+ /**
248
+ * Get intersect items
249
+ *
250
+ * @returns intersect items
251
+ */
252
+ declare function intersect<T>(a: T[], b: T[]): T[];
253
+
247
254
  declare function isArrayEqual(array1: unknown[], array2: unknown[]): boolean;
248
255
 
256
+ declare class Color {
257
+ red: number;
258
+ green: number;
259
+ blue: number;
260
+ alpha: number;
261
+ constructor(red?: number, green?: number, blue?: number, alpha?: number);
262
+ static fromRGB(red: number, green: number, blue: number): Color;
263
+ static fromRGBA(red: number, green: number, blue: number, alpha: number): Color;
264
+ static fromHex(hex: string): Color;
265
+ get brightness(): number;
266
+ get isDark(): boolean;
267
+ get isLight(): boolean;
268
+ toHexString(isUpperCase?: boolean): string;
269
+ toRGBAString(): string;
270
+ /**
271
+ * add alpha value to {@link Color}
272
+ *
273
+ * @param alpha - alpha value
274
+ * @returns instance of {@link Color}
275
+ */
276
+ withAlpha(alpha?: number): Color;
277
+ /**
278
+ * lighten the color by percentage
279
+ *
280
+ * @param percentage - percentage to lighten
281
+ */
282
+ lighten(percentage?: number): Color;
283
+ /**
284
+ * darken the color by percentage
285
+ *
286
+ * @param percentage - percentage to darken
287
+ */
288
+ darken(percentage?: number): Color;
289
+ }
290
+
291
+ /**
292
+ * get a random RGB color
293
+ * @returns a random RGB color
294
+ */
295
+ declare function randomRGBColor(): string;
296
+ /**
297
+ * get a random RGBA color
298
+ * @returns a random RGBA color
299
+ */
300
+ declare function randomRGBAColor(): string;
301
+ /**
302
+ * get a random hex color
303
+ * @returns a random hex color
304
+ */
305
+ declare function randomHexColor(): string;
306
+
307
+ /**
308
+ * enhance object
309
+ * @module proxy
310
+ */
311
+ declare function enhance<T extends Record<PropertyKey, any>, E extends Record<PropertyKey, any>>(module: T, extra: E): T;
312
+
313
+ interface CreatePadStringOptions {
314
+ length: number;
315
+ char: string;
316
+ }
317
+ declare function createPadString(options: CreatePadStringOptions): (value: string) => string;
318
+
249
319
  type JoinableValue = string | number | null | undefined;
250
320
  interface JoinOptions {
251
321
  /**
@@ -266,6 +336,15 @@ declare function join(array: JoinableValue[], options?: JoinOptions): string;
266
336
  */
267
337
  declare function slash(input: string): string;
268
338
 
339
+ /**
340
+ * randome a string useing given chars
341
+ *
342
+ * @param length - string length
343
+ * @param chars - string chars
344
+ * @returns random string
345
+ */
346
+ declare function randomString(length?: number, chars?: string): string;
347
+
269
348
  /**
270
349
  * Remove common leading whitespace from a template string
271
350
  * Empty lines at the beginning and end of the template string are also removed.
@@ -319,19 +398,19 @@ interface CleanObjectOptions {
319
398
  /**
320
399
  * clean empty string
321
400
  *
322
- * @default true
401
+ * @default false
323
402
  */
324
403
  cleanEmptyString?: boolean;
325
404
  /**
326
405
  * clean empty array
327
406
  *
328
- * @default true
407
+ * @default false
329
408
  */
330
409
  cleanEmptyArray?: boolean;
331
410
  /**
332
411
  * clean empty object
333
412
  *
334
- * @default true
413
+ * @default false
335
414
  */
336
415
  cleanEmptyObject?: boolean;
337
416
  /**
@@ -428,4 +507,21 @@ declare function interopDefault<T>(mod: Awaitable<T>): Promise<InteropModuleDefa
428
507
  */
429
508
  declare function resolveSubOptions<T extends Record<string, any>, K extends keyof T>(options: T, key: K): Partial<ResolvedOptions<T[K]>>;
430
509
 
431
- export { type AnyFn, type Arrayable, type Awaitable, type Callable, type CleanObjectOptions, type InteropModuleDefault, type JsonArray, type JsonObject, type JsonPrimitive, type JsonValue, type MayBe, NOOP, type NonEmptyString, type Nullable, type Overwrite, type Prettify, type PrettifyV2, type PrimitiveType, type ResolvedOptions, type SortObjectOptions, type ThrottleDebounceOptions, type Whitespace, at, cAF, capitalize, chunk, clamp, cleanObject, days, debounce, ensurePrefix, ensureSuffix, escapeHtml, flattenArrayable, getObjectType, hasOwn, hours, interopDefault, isArray, isArrayEqual, isBigInt, isBoolean, isBrowser, isDeepEqual, isEmptyArray, isEmptyMap, isEmptyObject, isEmptySet, isEmptyString, isEmptyStringOrWhitespace, isError, isFunction, isInteger, isIterable, isMap, isNaN, isNativePromise, isNil, isNonEmptyArray, isNonEmptyString, isNull, isNullOrUndefined, isNumber, isNumbericString, isObject, isPromise, isRegExp, isSet, isString, isUndefined, isWhitespaceString, isZero, join, last, mergeArrayable, minutes, noop, omit, once, pick, rAF, resolveSubOptions, seconds, slash, sortObject, throttle, toArray, unindent, unique, uniqueBy, waitFor, warnOnce, weeks };
510
+ interface RamdomNumberOptions {
511
+ /**
512
+ * include max value
513
+ *
514
+ * @default false
515
+ */
516
+ includeMax?: boolean;
517
+ }
518
+ /**
519
+ * random an integer by given range
520
+ *
521
+ * @param min - min value
522
+ * @param max - max value
523
+ * @returns random integer in range
524
+ */
525
+ declare function randomNumber(min: number, max?: number, options?: RamdomNumberOptions): number;
526
+
527
+ export { type AnyFn, type Arrayable, type Awaitable, type Callable, type CleanObjectOptions, Color, type CreatePadStringOptions, type InteropModuleDefault, type JsonArray, type JsonObject, type JsonPrimitive, type JsonValue, type MayBe, NOOP, type NonEmptyString, type Nullable, type Overwrite, type Prettify, type PrettifyV2, type PrimitiveType, type RamdomNumberOptions, type ResolvedOptions, type SortObjectOptions, type ThrottleDebounceOptions, type Whitespace, at, cAF, capitalize, chunk, clamp, cleanObject, createPadString, days, debounce, enhance, ensurePrefix, ensureSuffix, escapeHtml, flattenArrayable, getObjectType, hasOwn, hours, interopDefault, intersect, isArray, isArrayEqual, isBigInt, isBoolean, isBrowser, isDeepEqual, isEmptyArray, isEmptyMap, isEmptyObject, isEmptySet, isEmptyString, isEmptyStringOrWhitespace, isError, isFunction, isInteger, isIterable, isMap, isNaN, isNativePromise, isNil, isNonEmptyArray, isNonEmptyString, isNull, isNullOrUndefined, isNumber, isNumbericString, isObject, isPromise, isRegExp, isSet, isString, isUndefined, isWhitespaceString, isZero, join, last, mergeArrayable, minutes, noop, omit, once, pick, rAF, randomHexColor, randomNumber, randomRGBAColor, randomRGBColor, randomString, resolveSubOptions, seconds, slash, sortObject, throttle, toArray, unindent, unique, uniqueBy, waitFor, warnOnce, weeks };
package/dist/index.js CHANGED
@@ -16,7 +16,9 @@ function isDeepEqual(value1, value2) {
16
16
  if (keys.length !== Object.keys(value2).length) {
17
17
  return false;
18
18
  }
19
- return keys.every((key) => isDeepEqual(value1[key], value2[key]));
19
+ return keys.every(
20
+ (key) => isDeepEqual(value1[key], value2[key])
21
+ );
20
22
  }
21
23
  return Object.is(value1, value2);
22
24
  }
@@ -226,7 +228,10 @@ function throttle(delay, callback, options = {}) {
226
228
  if (!isDebounce && elapsed > delay) {
227
229
  exec(now);
228
230
  } else {
229
- timeoutId = setTimeout(isDebounce ? clear : exec, isDebounce ? delay : delay - elapsed);
231
+ timeoutId = setTimeout(
232
+ isDebounce ? clear : exec,
233
+ isDebounce ? delay : delay - elapsed
234
+ );
230
235
  }
231
236
  }
232
237
  wrapper.cancel = cancel;
@@ -304,6 +309,11 @@ function mergeArrayable(...args) {
304
309
  return args.flatMap((i) => toArray(i));
305
310
  }
306
311
 
312
+ // src/array/intersect.ts
313
+ function intersect(a, b) {
314
+ return a.filter((item) => b.includes(item));
315
+ }
316
+
307
317
  // src/array/isArrayEqual.ts
308
318
  function isArrayEqual(array1, array2) {
309
319
  if (array1.length !== array2.length) {
@@ -312,6 +322,12 @@ function isArrayEqual(array1, array2) {
312
322
  return array1.every((item, idx) => item === array2[idx]);
313
323
  }
314
324
 
325
+ // src/string/pad.ts
326
+ function createPadString(options) {
327
+ const { length, char } = options;
328
+ return (value) => (char.repeat(length) + value).slice(-length);
329
+ }
330
+
315
331
  // src/string/join.ts
316
332
  function join(array, options = {}) {
317
333
  const { separator = "" } = options;
@@ -324,6 +340,30 @@ function slash(input) {
324
340
  return input.replace(/\\/g, "/");
325
341
  }
326
342
 
343
+ // src/number/random.ts
344
+ function randomNumber(min, max = 0, options = {}) {
345
+ if (max === 0) {
346
+ max = min;
347
+ min = 0;
348
+ }
349
+ if (min > max) {
350
+ ;
351
+ [min, max] = [max, min];
352
+ }
353
+ return Math.trunc(
354
+ Math.random() * (max - min + (options.includeMax ? 1 : 0)) + min
355
+ );
356
+ }
357
+
358
+ // src/string/random.ts
359
+ function randomString(length = 16, chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") {
360
+ const result = [];
361
+ for (let i = length; i > 0; --i) {
362
+ result.push(chars[randomNumber(chars.length)]);
363
+ }
364
+ return result.join("");
365
+ }
366
+
327
367
  // src/string/unindent.ts
328
368
  var _RE_FULL_WS = /^\s*$/;
329
369
  function unindent(input) {
@@ -357,6 +397,119 @@ function ensureSuffix(input, suffix) {
357
397
  return input.endsWith(suffix) ? input : `${input}${suffix}`;
358
398
  }
359
399
 
400
+ // src/color/color.ts
401
+ var pad2 = createPadString({ length: 2, char: "0" });
402
+ var RE_VALID_HEX_COLOR = /^#(?:[0-9a-f]{6}|[0-9a-f]{3})$/i;
403
+ function validateHexColor(hex) {
404
+ if (hex.length !== 4 && hex.length !== 7) return false;
405
+ if (!hex.startsWith("#")) return false;
406
+ return RE_VALID_HEX_COLOR.test(hex);
407
+ }
408
+ function normalizeHexString(hex) {
409
+ return hex.length === 6 ? hex : hex.replace(/./g, "$&$&");
410
+ }
411
+ var Color = class _Color {
412
+ constructor(red = 0, green = 0, blue = 0, alpha = 1) {
413
+ this.red = red;
414
+ this.green = green;
415
+ this.blue = blue;
416
+ this.alpha = alpha;
417
+ }
418
+ static fromRGB(red, green, blue) {
419
+ return new _Color(red, green, blue);
420
+ }
421
+ static fromRGBA(red, green, blue, alpha) {
422
+ return new _Color(red, green, blue, alpha);
423
+ }
424
+ static fromHex(hex) {
425
+ if (!validateHexColor(hex)) {
426
+ throw new Error("Invalid hex color");
427
+ }
428
+ const [red, green, blue] = normalizeHexString(hex.slice(1)).match(/.{2}/g)?.map((value) => Number.parseInt(value, 16)) ?? [0, 0, 0];
429
+ return new _Color(red, green, blue);
430
+ }
431
+ get brightness() {
432
+ return (this.red * 299 + this.green * 587 + this.blue * 114) / 1e3;
433
+ }
434
+ get isDark() {
435
+ return this.brightness < 128;
436
+ }
437
+ get isLight() {
438
+ return !this.isDark;
439
+ }
440
+ toHexString(isUpperCase = true) {
441
+ const hexString = `#${pad2(this.red.toString(16))}${pad2(this.green.toString(16))}${pad2(this.blue.toString(16))}`;
442
+ return isUpperCase ? hexString.toUpperCase() : hexString;
443
+ }
444
+ toRGBAString() {
445
+ return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`;
446
+ }
447
+ /**
448
+ * add alpha value to {@link Color}
449
+ *
450
+ * @param alpha - alpha value
451
+ * @returns instance of {@link Color}
452
+ */
453
+ withAlpha(alpha = 1) {
454
+ return new _Color(this.red, this.green, this.blue, alpha);
455
+ }
456
+ /**
457
+ * lighten the color by percentage
458
+ *
459
+ * @param percentage - percentage to lighten
460
+ */
461
+ lighten(percentage = 0) {
462
+ const amount = Math.round(percentage / 100 * 255);
463
+ return new _Color(
464
+ Math.min(this.red + amount, 255),
465
+ Math.min(this.green + amount, 255),
466
+ Math.min(this.blue + amount, 255),
467
+ this.alpha
468
+ );
469
+ }
470
+ /**
471
+ * darken the color by percentage
472
+ *
473
+ * @param percentage - percentage to darken
474
+ */
475
+ darken(percentage = 0) {
476
+ const amount = Math.round(percentage / 100 * 255);
477
+ return new _Color(
478
+ Math.max(this.red - amount, 0),
479
+ Math.max(this.green - amount, 0),
480
+ Math.max(this.blue - amount, 0),
481
+ this.alpha
482
+ );
483
+ }
484
+ };
485
+
486
+ // src/color/random.ts
487
+ var MAX_RGB = 255;
488
+ function randomRGBColor() {
489
+ return `rgb(${randomNumber(MAX_RGB)}, ${randomNumber(MAX_RGB)}, ${randomNumber(MAX_RGB)})`;
490
+ }
491
+ function randomRGBAColor() {
492
+ return `rgba(${randomNumber(MAX_RGB)}, ${randomNumber(MAX_RGB)}, ${randomNumber(MAX_RGB)}, ${Math.random().toFixed(1)})`;
493
+ }
494
+ function randomHexColor() {
495
+ return `#${Math.random().toString(16).slice(2, 8)}`;
496
+ }
497
+
498
+ // src/proxy/enhance.ts
499
+ function enhance(module, extra) {
500
+ return new Proxy(module, {
501
+ get(target, key, receiver) {
502
+ if (Reflect.has(extra, key)) {
503
+ return Reflect.get(extra, key, receiver);
504
+ }
505
+ return Reflect.get(target, key, receiver);
506
+ },
507
+ has(target, key) {
508
+ return Reflect.has(extra, key) || Reflect.has(target, key);
509
+ }
510
+ });
511
+ }
512
+
360
513
  // src/object/omit.ts
361
514
  function omit(object, ...keys) {
362
515
  keys.forEach((key) => delete object[key]);
@@ -388,11 +541,11 @@ function cleanObject(obj, options = {}) {
388
541
  const {
389
542
  cleanUndefined = true,
390
543
  cleanNull = true,
391
- cleanZero = false,
392
544
  cleanNaN = true,
393
- cleanEmptyString = true,
394
- cleanEmptyArray = true,
395
- cleanEmptyObject = true,
545
+ cleanZero = false,
546
+ cleanEmptyString = false,
547
+ cleanEmptyArray = false,
548
+ cleanEmptyObject = false,
396
549
  recursive = true
397
550
  } = options;
398
551
  Object.keys(obj).forEach((key) => {
@@ -550,6 +703,7 @@ function resolveSubOptions(options, key) {
550
703
  return typeof options[key] === "boolean" ? {} : options[key] || {};
551
704
  }
552
705
  export {
706
+ Color,
553
707
  NOOP,
554
708
  at,
555
709
  cAF,
@@ -558,8 +712,10 @@ export {
558
712
  chunk,
559
713
  clamp,
560
714
  cleanObject,
715
+ createPadString,
561
716
  days,
562
717
  debounce,
718
+ enhance,
563
719
  ensurePrefix,
564
720
  ensureSuffix,
565
721
  escapeHtml,
@@ -569,6 +725,7 @@ export {
569
725
  hasOwn,
570
726
  hours,
571
727
  interopDefault,
728
+ intersect,
572
729
  isArray,
573
730
  isArrayEqual,
574
731
  isBigInt,
@@ -616,6 +773,11 @@ export {
616
773
  pascalCase,
617
774
  pick,
618
775
  rAF,
776
+ randomHexColor,
777
+ randomNumber,
778
+ randomRGBAColor,
779
+ randomRGBColor,
780
+ randomString,
619
781
  resolveSubOptions,
620
782
  seconds,
621
783
  slash,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ntnyq/utils",
3
3
  "type": "module",
4
- "version": "0.4.5",
4
+ "version": "0.5.1",
5
5
  "description": "Common used utils.",
6
6
  "keywords": [
7
7
  "utils"
@@ -40,18 +40,18 @@
40
40
  "scule": "^1.3.0"
41
41
  },
42
42
  "devDependencies": {
43
- "@ntnyq/eslint-config": "^3.10.4",
44
- "@ntnyq/prettier-config": "^1.22.0",
45
- "@vitest/coverage-v8": "^3.0.0-beta.3",
46
- "bumpp": "^9.9.2",
47
- "eslint": "^9.17.0",
43
+ "@ntnyq/eslint-config": "^4.0.0-beta.3",
44
+ "@ntnyq/prettier-config": "^2.0.0-beta.2",
45
+ "@vitest/coverage-v8": "^3.0.5",
46
+ "bumpp": "^10.0.3",
47
+ "eslint": "^9.20.0",
48
48
  "husky": "^9.1.7",
49
49
  "nano-staged": "^0.8.0",
50
50
  "npm-run-all2": "^7.0.2",
51
- "prettier": "^3.4.2",
52
- "tsup": "^8.3.5",
53
- "typescript": "^5.7.2",
54
- "vitest": "^3.0.0-beta.3"
51
+ "prettier": "^3.5.0",
52
+ "tsup": "^8.3.6",
53
+ "typescript": "^5.7.3",
54
+ "vitest": "^3.0.5"
55
55
  },
56
56
  "engines": {
57
57
  "node": ">=18.18.0"