@ntnyq/utils 0.5.0 → 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 +133 -6
- package/dist/index.d.cts +78 -4
- package/dist/index.d.ts +78 -4
- package/dist/index.js +127 -6
- package/package.json +2 -2
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,
|
|
@@ -31,6 +32,7 @@ __export(index_exports, {
|
|
|
31
32
|
createPadString: () => createPadString,
|
|
32
33
|
days: () => days,
|
|
33
34
|
debounce: () => debounce,
|
|
35
|
+
enhance: () => enhance,
|
|
34
36
|
ensurePrefix: () => ensurePrefix,
|
|
35
37
|
ensureSuffix: () => ensureSuffix,
|
|
36
38
|
escapeHtml: () => escapeHtml,
|
|
@@ -88,6 +90,10 @@ __export(index_exports, {
|
|
|
88
90
|
pascalCase: () => pascalCase,
|
|
89
91
|
pick: () => pick,
|
|
90
92
|
rAF: () => rAF,
|
|
93
|
+
randomHexColor: () => randomHexColor,
|
|
94
|
+
randomNumber: () => randomNumber,
|
|
95
|
+
randomRGBAColor: () => randomRGBAColor,
|
|
96
|
+
randomRGBColor: () => randomRGBColor,
|
|
91
97
|
randomString: () => randomString,
|
|
92
98
|
resolveSubOptions: () => resolveSubOptions,
|
|
93
99
|
seconds: () => seconds,
|
|
@@ -452,7 +458,7 @@ function slash(input) {
|
|
|
452
458
|
}
|
|
453
459
|
|
|
454
460
|
// src/number/random.ts
|
|
455
|
-
function randomNumber(min, max = 0) {
|
|
461
|
+
function randomNumber(min, max = 0, options = {}) {
|
|
456
462
|
if (max === 0) {
|
|
457
463
|
max = min;
|
|
458
464
|
min = 0;
|
|
@@ -461,7 +467,9 @@ function randomNumber(min, max = 0) {
|
|
|
461
467
|
;
|
|
462
468
|
[min, max] = [max, min];
|
|
463
469
|
}
|
|
464
|
-
return Math.trunc(
|
|
470
|
+
return Math.trunc(
|
|
471
|
+
Math.random() * (max - min + (options.includeMax ? 1 : 0)) + min
|
|
472
|
+
);
|
|
465
473
|
}
|
|
466
474
|
|
|
467
475
|
// src/string/random.ts
|
|
@@ -506,6 +514,119 @@ function ensureSuffix(input, suffix) {
|
|
|
506
514
|
return input.endsWith(suffix) ? input : `${input}${suffix}`;
|
|
507
515
|
}
|
|
508
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
|
+
|
|
509
630
|
// src/object/omit.ts
|
|
510
631
|
function omit(object, ...keys) {
|
|
511
632
|
keys.forEach((key) => delete object[key]);
|
|
@@ -537,11 +658,11 @@ function cleanObject(obj, options = {}) {
|
|
|
537
658
|
const {
|
|
538
659
|
cleanUndefined = true,
|
|
539
660
|
cleanNull = true,
|
|
540
|
-
cleanZero = false,
|
|
541
661
|
cleanNaN = true,
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
662
|
+
cleanZero = false,
|
|
663
|
+
cleanEmptyString = false,
|
|
664
|
+
cleanEmptyArray = false,
|
|
665
|
+
cleanEmptyObject = false,
|
|
545
666
|
recursive = true
|
|
546
667
|
} = options;
|
|
547
668
|
Object.keys(obj).forEach((key) => {
|
|
@@ -700,6 +821,7 @@ function resolveSubOptions(options, key) {
|
|
|
700
821
|
}
|
|
701
822
|
// Annotate the CommonJS export names for ESM import in node:
|
|
702
823
|
0 && (module.exports = {
|
|
824
|
+
Color,
|
|
703
825
|
NOOP,
|
|
704
826
|
at,
|
|
705
827
|
cAF,
|
|
@@ -711,6 +833,7 @@ function resolveSubOptions(options, key) {
|
|
|
711
833
|
createPadString,
|
|
712
834
|
days,
|
|
713
835
|
debounce,
|
|
836
|
+
enhance,
|
|
714
837
|
ensurePrefix,
|
|
715
838
|
ensureSuffix,
|
|
716
839
|
escapeHtml,
|
|
@@ -768,6 +891,10 @@ function resolveSubOptions(options, key) {
|
|
|
768
891
|
pascalCase,
|
|
769
892
|
pick,
|
|
770
893
|
rAF,
|
|
894
|
+
randomHexColor,
|
|
895
|
+
randomNumber,
|
|
896
|
+
randomRGBAColor,
|
|
897
|
+
randomRGBColor,
|
|
771
898
|
randomString,
|
|
772
899
|
resolveSubOptions,
|
|
773
900
|
seconds,
|
package/dist/index.d.cts
CHANGED
|
@@ -253,6 +253,63 @@ declare function intersect<T>(a: T[], b: T[]): T[];
|
|
|
253
253
|
|
|
254
254
|
declare function isArrayEqual(array1: unknown[], array2: unknown[]): boolean;
|
|
255
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
|
+
|
|
256
313
|
interface CreatePadStringOptions {
|
|
257
314
|
length: number;
|
|
258
315
|
char: string;
|
|
@@ -341,19 +398,19 @@ interface CleanObjectOptions {
|
|
|
341
398
|
/**
|
|
342
399
|
* clean empty string
|
|
343
400
|
*
|
|
344
|
-
* @default
|
|
401
|
+
* @default false
|
|
345
402
|
*/
|
|
346
403
|
cleanEmptyString?: boolean;
|
|
347
404
|
/**
|
|
348
405
|
* clean empty array
|
|
349
406
|
*
|
|
350
|
-
* @default
|
|
407
|
+
* @default false
|
|
351
408
|
*/
|
|
352
409
|
cleanEmptyArray?: boolean;
|
|
353
410
|
/**
|
|
354
411
|
* clean empty object
|
|
355
412
|
*
|
|
356
|
-
* @default
|
|
413
|
+
* @default false
|
|
357
414
|
*/
|
|
358
415
|
cleanEmptyObject?: boolean;
|
|
359
416
|
/**
|
|
@@ -450,4 +507,21 @@ declare function interopDefault<T>(mod: Awaitable<T>): Promise<InteropModuleDefa
|
|
|
450
507
|
*/
|
|
451
508
|
declare function resolveSubOptions<T extends Record<string, any>, K extends keyof T>(options: T, key: K): Partial<ResolvedOptions<T[K]>>;
|
|
452
509
|
|
|
453
|
-
|
|
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
|
@@ -253,6 +253,63 @@ declare function intersect<T>(a: T[], b: T[]): T[];
|
|
|
253
253
|
|
|
254
254
|
declare function isArrayEqual(array1: unknown[], array2: unknown[]): boolean;
|
|
255
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
|
+
|
|
256
313
|
interface CreatePadStringOptions {
|
|
257
314
|
length: number;
|
|
258
315
|
char: string;
|
|
@@ -341,19 +398,19 @@ interface CleanObjectOptions {
|
|
|
341
398
|
/**
|
|
342
399
|
* clean empty string
|
|
343
400
|
*
|
|
344
|
-
* @default
|
|
401
|
+
* @default false
|
|
345
402
|
*/
|
|
346
403
|
cleanEmptyString?: boolean;
|
|
347
404
|
/**
|
|
348
405
|
* clean empty array
|
|
349
406
|
*
|
|
350
|
-
* @default
|
|
407
|
+
* @default false
|
|
351
408
|
*/
|
|
352
409
|
cleanEmptyArray?: boolean;
|
|
353
410
|
/**
|
|
354
411
|
* clean empty object
|
|
355
412
|
*
|
|
356
|
-
* @default
|
|
413
|
+
* @default false
|
|
357
414
|
*/
|
|
358
415
|
cleanEmptyObject?: boolean;
|
|
359
416
|
/**
|
|
@@ -450,4 +507,21 @@ declare function interopDefault<T>(mod: Awaitable<T>): Promise<InteropModuleDefa
|
|
|
450
507
|
*/
|
|
451
508
|
declare function resolveSubOptions<T extends Record<string, any>, K extends keyof T>(options: T, key: K): Partial<ResolvedOptions<T[K]>>;
|
|
452
509
|
|
|
453
|
-
|
|
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
|
@@ -341,7 +341,7 @@ function slash(input) {
|
|
|
341
341
|
}
|
|
342
342
|
|
|
343
343
|
// src/number/random.ts
|
|
344
|
-
function randomNumber(min, max = 0) {
|
|
344
|
+
function randomNumber(min, max = 0, options = {}) {
|
|
345
345
|
if (max === 0) {
|
|
346
346
|
max = min;
|
|
347
347
|
min = 0;
|
|
@@ -350,7 +350,9 @@ function randomNumber(min, max = 0) {
|
|
|
350
350
|
;
|
|
351
351
|
[min, max] = [max, min];
|
|
352
352
|
}
|
|
353
|
-
return Math.trunc(
|
|
353
|
+
return Math.trunc(
|
|
354
|
+
Math.random() * (max - min + (options.includeMax ? 1 : 0)) + min
|
|
355
|
+
);
|
|
354
356
|
}
|
|
355
357
|
|
|
356
358
|
// src/string/random.ts
|
|
@@ -395,6 +397,119 @@ function ensureSuffix(input, suffix) {
|
|
|
395
397
|
return input.endsWith(suffix) ? input : `${input}${suffix}`;
|
|
396
398
|
}
|
|
397
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
|
+
|
|
398
513
|
// src/object/omit.ts
|
|
399
514
|
function omit(object, ...keys) {
|
|
400
515
|
keys.forEach((key) => delete object[key]);
|
|
@@ -426,11 +541,11 @@ function cleanObject(obj, options = {}) {
|
|
|
426
541
|
const {
|
|
427
542
|
cleanUndefined = true,
|
|
428
543
|
cleanNull = true,
|
|
429
|
-
cleanZero = false,
|
|
430
544
|
cleanNaN = true,
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
545
|
+
cleanZero = false,
|
|
546
|
+
cleanEmptyString = false,
|
|
547
|
+
cleanEmptyArray = false,
|
|
548
|
+
cleanEmptyObject = false,
|
|
434
549
|
recursive = true
|
|
435
550
|
} = options;
|
|
436
551
|
Object.keys(obj).forEach((key) => {
|
|
@@ -588,6 +703,7 @@ function resolveSubOptions(options, key) {
|
|
|
588
703
|
return typeof options[key] === "boolean" ? {} : options[key] || {};
|
|
589
704
|
}
|
|
590
705
|
export {
|
|
706
|
+
Color,
|
|
591
707
|
NOOP,
|
|
592
708
|
at,
|
|
593
709
|
cAF,
|
|
@@ -599,6 +715,7 @@ export {
|
|
|
599
715
|
createPadString,
|
|
600
716
|
days,
|
|
601
717
|
debounce,
|
|
718
|
+
enhance,
|
|
602
719
|
ensurePrefix,
|
|
603
720
|
ensureSuffix,
|
|
604
721
|
escapeHtml,
|
|
@@ -656,6 +773,10 @@ export {
|
|
|
656
773
|
pascalCase,
|
|
657
774
|
pick,
|
|
658
775
|
rAF,
|
|
776
|
+
randomHexColor,
|
|
777
|
+
randomNumber,
|
|
778
|
+
randomRGBAColor,
|
|
779
|
+
randomRGBColor,
|
|
659
780
|
randomString,
|
|
660
781
|
resolveSubOptions,
|
|
661
782
|
seconds,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ntnyq/utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.5.
|
|
4
|
+
"version": "0.5.1",
|
|
5
5
|
"description": "Common used utils.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"utils"
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"@ntnyq/eslint-config": "^4.0.0-beta.3",
|
|
44
44
|
"@ntnyq/prettier-config": "^2.0.0-beta.2",
|
|
45
45
|
"@vitest/coverage-v8": "^3.0.5",
|
|
46
|
-
"bumpp": "^10.0.
|
|
46
|
+
"bumpp": "^10.0.3",
|
|
47
47
|
"eslint": "^9.20.0",
|
|
48
48
|
"husky": "^9.1.7",
|
|
49
49
|
"nano-staged": "^0.8.0",
|