@ntnyq/utils 0.4.5 → 0.5.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.cjs CHANGED
@@ -28,6 +28,7 @@ __export(index_exports, {
28
28
  chunk: () => chunk,
29
29
  clamp: () => clamp,
30
30
  cleanObject: () => cleanObject,
31
+ createPadString: () => createPadString,
31
32
  days: () => days,
32
33
  debounce: () => debounce,
33
34
  ensurePrefix: () => ensurePrefix,
@@ -39,6 +40,7 @@ __export(index_exports, {
39
40
  hasOwn: () => hasOwn,
40
41
  hours: () => hours,
41
42
  interopDefault: () => interopDefault,
43
+ intersect: () => intersect,
42
44
  isArray: () => isArray,
43
45
  isArrayEqual: () => isArrayEqual,
44
46
  isBigInt: () => isBigInt,
@@ -86,6 +88,7 @@ __export(index_exports, {
86
88
  pascalCase: () => pascalCase,
87
89
  pick: () => pick,
88
90
  rAF: () => rAF,
91
+ randomString: () => randomString,
89
92
  resolveSubOptions: () => resolveSubOptions,
90
93
  seconds: () => seconds,
91
94
  slash: () => slash,
@@ -124,7 +127,9 @@ function isDeepEqual(value1, value2) {
124
127
  if (keys.length !== Object.keys(value2).length) {
125
128
  return false;
126
129
  }
127
- return keys.every((key) => isDeepEqual(value1[key], value2[key]));
130
+ return keys.every(
131
+ (key) => isDeepEqual(value1[key], value2[key])
132
+ );
128
133
  }
129
134
  return Object.is(value1, value2);
130
135
  }
@@ -334,7 +339,10 @@ function throttle(delay, callback, options = {}) {
334
339
  if (!isDebounce && elapsed > delay) {
335
340
  exec(now);
336
341
  } else {
337
- timeoutId = setTimeout(isDebounce ? clear : exec, isDebounce ? delay : delay - elapsed);
342
+ timeoutId = setTimeout(
343
+ isDebounce ? clear : exec,
344
+ isDebounce ? delay : delay - elapsed
345
+ );
338
346
  }
339
347
  }
340
348
  wrapper.cancel = cancel;
@@ -412,6 +420,11 @@ function mergeArrayable(...args) {
412
420
  return args.flatMap((i) => toArray(i));
413
421
  }
414
422
 
423
+ // src/array/intersect.ts
424
+ function intersect(a, b) {
425
+ return a.filter((item) => b.includes(item));
426
+ }
427
+
415
428
  // src/array/isArrayEqual.ts
416
429
  function isArrayEqual(array1, array2) {
417
430
  if (array1.length !== array2.length) {
@@ -420,6 +433,12 @@ function isArrayEqual(array1, array2) {
420
433
  return array1.every((item, idx) => item === array2[idx]);
421
434
  }
422
435
 
436
+ // src/string/pad.ts
437
+ function createPadString(options) {
438
+ const { length, char } = options;
439
+ return (value) => (char.repeat(length) + value).slice(-length);
440
+ }
441
+
423
442
  // src/string/join.ts
424
443
  function join(array, options = {}) {
425
444
  const { separator = "" } = options;
@@ -432,6 +451,28 @@ function slash(input) {
432
451
  return input.replace(/\\/g, "/");
433
452
  }
434
453
 
454
+ // src/number/random.ts
455
+ function randomNumber(min, max = 0) {
456
+ if (max === 0) {
457
+ max = min;
458
+ min = 0;
459
+ }
460
+ if (min > max) {
461
+ ;
462
+ [min, max] = [max, min];
463
+ }
464
+ return Math.trunc(Math.random() * (max - min + 1) + min);
465
+ }
466
+
467
+ // src/string/random.ts
468
+ function randomString(length = 16, chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") {
469
+ const result = [];
470
+ for (let i = length; i > 0; --i) {
471
+ result.push(chars[randomNumber(chars.length)]);
472
+ }
473
+ return result.join("");
474
+ }
475
+
435
476
  // src/string/unindent.ts
436
477
  var _RE_FULL_WS = /^\s*$/;
437
478
  function unindent(input) {
@@ -667,6 +708,7 @@ function resolveSubOptions(options, key) {
667
708
  chunk,
668
709
  clamp,
669
710
  cleanObject,
711
+ createPadString,
670
712
  days,
671
713
  debounce,
672
714
  ensurePrefix,
@@ -678,6 +720,7 @@ function resolveSubOptions(options, key) {
678
720
  hasOwn,
679
721
  hours,
680
722
  interopDefault,
723
+ intersect,
681
724
  isArray,
682
725
  isArrayEqual,
683
726
  isBigInt,
@@ -725,6 +768,7 @@ function resolveSubOptions(options, key) {
725
768
  pascalCase,
726
769
  pick,
727
770
  rAF,
771
+ randomString,
728
772
  resolveSubOptions,
729
773
  seconds,
730
774
  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,21 @@ 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
+ interface CreatePadStringOptions {
257
+ length: number;
258
+ char: string;
259
+ }
260
+ declare function createPadString(options: CreatePadStringOptions): (value: string) => string;
261
+
249
262
  type JoinableValue = string | number | null | undefined;
250
263
  interface JoinOptions {
251
264
  /**
@@ -266,6 +279,15 @@ declare function join(array: JoinableValue[], options?: JoinOptions): string;
266
279
  */
267
280
  declare function slash(input: string): string;
268
281
 
282
+ /**
283
+ * randome a string useing given chars
284
+ *
285
+ * @param length - string length
286
+ * @param chars - string chars
287
+ * @returns random string
288
+ */
289
+ declare function randomString(length?: number, chars?: string): string;
290
+
269
291
  /**
270
292
  * Remove common leading whitespace from a template string
271
293
  * Empty lines at the beginning and end of the template string are also removed.
@@ -428,4 +450,4 @@ declare function interopDefault<T>(mod: Awaitable<T>): Promise<InteropModuleDefa
428
450
  */
429
451
  declare function resolveSubOptions<T extends Record<string, any>, K extends keyof T>(options: T, key: K): Partial<ResolvedOptions<T[K]>>;
430
452
 
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 };
453
+ export { type AnyFn, type Arrayable, type Awaitable, type Callable, type CleanObjectOptions, 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 ResolvedOptions, type SortObjectOptions, type ThrottleDebounceOptions, type Whitespace, at, cAF, capitalize, chunk, clamp, cleanObject, createPadString, days, debounce, 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, 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,21 @@ 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
+ interface CreatePadStringOptions {
257
+ length: number;
258
+ char: string;
259
+ }
260
+ declare function createPadString(options: CreatePadStringOptions): (value: string) => string;
261
+
249
262
  type JoinableValue = string | number | null | undefined;
250
263
  interface JoinOptions {
251
264
  /**
@@ -266,6 +279,15 @@ declare function join(array: JoinableValue[], options?: JoinOptions): string;
266
279
  */
267
280
  declare function slash(input: string): string;
268
281
 
282
+ /**
283
+ * randome a string useing given chars
284
+ *
285
+ * @param length - string length
286
+ * @param chars - string chars
287
+ * @returns random string
288
+ */
289
+ declare function randomString(length?: number, chars?: string): string;
290
+
269
291
  /**
270
292
  * Remove common leading whitespace from a template string
271
293
  * Empty lines at the beginning and end of the template string are also removed.
@@ -428,4 +450,4 @@ declare function interopDefault<T>(mod: Awaitable<T>): Promise<InteropModuleDefa
428
450
  */
429
451
  declare function resolveSubOptions<T extends Record<string, any>, K extends keyof T>(options: T, key: K): Partial<ResolvedOptions<T[K]>>;
430
452
 
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 };
453
+ export { type AnyFn, type Arrayable, type Awaitable, type Callable, type CleanObjectOptions, 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 ResolvedOptions, type SortObjectOptions, type ThrottleDebounceOptions, type Whitespace, at, cAF, capitalize, chunk, clamp, cleanObject, createPadString, days, debounce, 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, 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,28 @@ function slash(input) {
324
340
  return input.replace(/\\/g, "/");
325
341
  }
326
342
 
343
+ // src/number/random.ts
344
+ function randomNumber(min, max = 0) {
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(Math.random() * (max - min + 1) + min);
354
+ }
355
+
356
+ // src/string/random.ts
357
+ function randomString(length = 16, chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") {
358
+ const result = [];
359
+ for (let i = length; i > 0; --i) {
360
+ result.push(chars[randomNumber(chars.length)]);
361
+ }
362
+ return result.join("");
363
+ }
364
+
327
365
  // src/string/unindent.ts
328
366
  var _RE_FULL_WS = /^\s*$/;
329
367
  function unindent(input) {
@@ -558,6 +596,7 @@ export {
558
596
  chunk,
559
597
  clamp,
560
598
  cleanObject,
599
+ createPadString,
561
600
  days,
562
601
  debounce,
563
602
  ensurePrefix,
@@ -569,6 +608,7 @@ export {
569
608
  hasOwn,
570
609
  hours,
571
610
  interopDefault,
611
+ intersect,
572
612
  isArray,
573
613
  isArrayEqual,
574
614
  isBigInt,
@@ -616,6 +656,7 @@ export {
616
656
  pascalCase,
617
657
  pick,
618
658
  rAF,
659
+ randomString,
619
660
  resolveSubOptions,
620
661
  seconds,
621
662
  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.0",
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.2",
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"