rattail 0.0.4 → 0.0.6

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/lib/index.cjs CHANGED
@@ -42,13 +42,15 @@ __export(src_exports, {
42
42
  call: () => call,
43
43
  camelize: () => camelize,
44
44
  cancelAnimationFrame: () => cancelAnimationFrame,
45
- capitalizeFirstLetter: () => capitalizeFirstLetter,
46
45
  clamp: () => clamp,
47
46
  clampArrayRange: () => clampArrayRange,
48
47
  classes: () => classes,
48
+ cloneDeep: () => cloneDeep,
49
+ cloneDeepWith: () => cloneDeepWith,
49
50
  createNamespaceFn: () => createNamespaceFn,
50
51
  createStorage: () => createStorage,
51
52
  debounce: () => debounce,
53
+ delay: () => delay,
52
54
  doubleRaf: () => doubleRaf,
53
55
  find: () => find,
54
56
  genNumberKey: () => genNumberKey,
@@ -65,7 +67,9 @@ __export(src_exports, {
65
67
  inMobile: () => inMobile,
66
68
  inViewport: () => inViewport,
67
69
  isArray: () => isArray,
70
+ isArrayBuffer: () => isArrayBuffer,
68
71
  isBoolean: () => isBoolean,
72
+ isDataView: () => isDataView,
69
73
  isDate: () => isDate,
70
74
  isEmpty: () => isEmpty,
71
75
  isFunction: () => isFunction,
@@ -82,10 +86,19 @@ __export(src_exports, {
82
86
  isString: () => isString,
83
87
  isSymbol: () => isSymbol,
84
88
  isTruthy: () => isTruthy,
89
+ isTypedArray: () => isTypedArray,
90
+ isWeakMap: () => isWeakMap,
91
+ isWeakSet: () => isWeakSet,
85
92
  isWindow: () => isWindow,
86
93
  kebabCase: () => kebabCase,
87
94
  localStorage: () => localStorage,
95
+ lowerFirst: () => lowerFirst,
96
+ maxBy: () => maxBy,
97
+ mean: () => mean,
98
+ meanBy: () => meanBy,
99
+ merge: () => merge,
88
100
  mergeWith: () => mergeWith,
101
+ minBy: () => minBy,
89
102
  normalizeToArray: () => normalizeToArray,
90
103
  objectToString: () => objectToString,
91
104
  pascalCase: () => pascalCase,
@@ -97,6 +110,7 @@ __export(src_exports, {
97
110
  removeArrayEmpty: () => removeArrayEmpty,
98
111
  removeItem: () => removeItem,
99
112
  requestAnimationFrame: () => requestAnimationFrame,
113
+ sample: () => sample,
100
114
  sessionStorage: () => sessionStorage,
101
115
  shuffle: () => shuffle,
102
116
  slash: () => slash,
@@ -104,6 +118,7 @@ __export(src_exports, {
104
118
  sumBy: () => sumBy,
105
119
  supportTouch: () => supportTouch,
106
120
  throttle: () => throttle,
121
+ times: () => times,
107
122
  toArrayBuffer: () => toArrayBuffer,
108
123
  toDataURL: () => toDataURL,
109
124
  toNumber: () => toNumber,
@@ -113,7 +128,8 @@ __export(src_exports, {
113
128
  toggleItem: () => toggleItem,
114
129
  tryParseJSON: () => tryParseJSON,
115
130
  uniq: () => uniq,
116
- uniqBy: () => uniqBy
131
+ uniqBy: () => uniqBy,
132
+ upperFirst: () => upperFirst
117
133
  });
118
134
  module.exports = __toCommonJS(src_exports);
119
135
 
@@ -147,16 +163,43 @@ function isPromise(val) {
147
163
  return isObject(val) && isFunction(val.then) && isFunction(val.catch);
148
164
  }
149
165
  function isMap(val) {
150
- return toTypeString(val) === "[object Map]";
166
+ return toRawType(val) === "Map";
151
167
  }
152
168
  function isSet(val) {
153
- return toTypeString(val) === "[object Set]";
169
+ return toRawType(val) === "Set";
154
170
  }
155
171
  function isDate(val) {
156
- return toTypeString(val) === "[object Date]";
172
+ return toRawType(val) === "Date";
157
173
  }
158
174
  function isRegExp(val) {
159
- return toTypeString(val) === "[object RegExp]";
175
+ return toRawType(val) === "RegExp";
176
+ }
177
+ function isWeakMap(val) {
178
+ return toRawType(val) === "WeakMap";
179
+ }
180
+ function isWeakSet(val) {
181
+ return toRawType(val) === "WeakSet";
182
+ }
183
+ function isArrayBuffer(val) {
184
+ return toRawType(val) === "ArrayBuffer";
185
+ }
186
+ function isTypedArray(val) {
187
+ return [
188
+ "Int8Array",
189
+ "Uint8Array",
190
+ "Uint8ClampedArray",
191
+ "Int16Array",
192
+ "Uint16Array",
193
+ "Int32Array",
194
+ "Uint32Array",
195
+ "Float32Array",
196
+ "Float64Array",
197
+ "BigInt64Array",
198
+ "BigUint64Array"
199
+ ].includes(toRawType(val));
200
+ }
201
+ function isDataView(val) {
202
+ return toRawType(val) === "DataView";
160
203
  }
161
204
  function toRawType(value) {
162
205
  return toTypeString(value).slice(8, -1);
@@ -264,12 +307,6 @@ function shuffle(arr) {
264
307
  }
265
308
  return arr;
266
309
  }
267
- function sum(arr) {
268
- return arr.reduce((ret, val) => ret + val, 0);
269
- }
270
- function sumBy(arr, fn) {
271
- return arr.reduce((ret, val) => ret + fn(val), 0);
272
- }
273
310
 
274
311
  // src/string.ts
275
312
  function pascalCase(s) {
@@ -294,9 +331,12 @@ var key = 0;
294
331
  function genStringKey() {
295
332
  return `generated-key-${key++}`;
296
333
  }
297
- function capitalizeFirstLetter(s) {
334
+ function upperFirst(s) {
298
335
  return s.charAt(0).toUpperCase() + s.slice(1);
299
336
  }
337
+ function lowerFirst(s) {
338
+ return s.charAt(0).toLowerCase() + s.slice(1);
339
+ }
300
340
 
301
341
  // src/element.ts
302
342
  function requestAnimationFrame(fn) {
@@ -422,7 +462,7 @@ function createNamespaceFn(namespace) {
422
462
  // src/function.ts
423
463
  function NOOP() {
424
464
  }
425
- function debounce(fn, delay = 0) {
465
+ function debounce(fn, delay2 = 0) {
426
466
  let timer;
427
467
  return function(...args) {
428
468
  if (timer) {
@@ -430,10 +470,10 @@ function debounce(fn, delay = 0) {
430
470
  }
431
471
  timer = setTimeout(() => {
432
472
  fn.apply(this, args);
433
- }, delay);
473
+ }, delay2);
434
474
  };
435
475
  }
436
- function throttle(fn, delay = 200) {
476
+ function throttle(fn, delay2 = 200) {
437
477
  let timer;
438
478
  let start = 0;
439
479
  return function loop(...args) {
@@ -445,13 +485,13 @@ function throttle(fn, delay = 200) {
445
485
  if (timer) {
446
486
  clearTimeout(timer);
447
487
  }
448
- if (elapsed >= delay) {
488
+ if (elapsed >= delay2) {
449
489
  fn.apply(this, args);
450
490
  start = now;
451
491
  } else {
452
492
  timer = setTimeout(() => {
453
493
  loop.apply(this, args);
454
- }, delay - elapsed);
494
+ }, delay2 - elapsed);
455
495
  }
456
496
  };
457
497
  }
@@ -492,15 +532,23 @@ function genNumberKey() {
492
532
  function randomNumber(min = 0, max = 100) {
493
533
  return Math.floor(Math.random() * (max - min + 1)) + min;
494
534
  }
535
+ function times(num, fn) {
536
+ return Array.from({ length: num }, (_, index) => fn(index));
537
+ }
538
+ function delay(time) {
539
+ return new Promise((resolve) => {
540
+ setTimeout(resolve, time);
541
+ });
542
+ }
495
543
 
496
544
  // src/collection.ts
497
- function mergeWith(object, source, callback) {
545
+ function mergeWith(object, source, fn) {
498
546
  function baseMerge(target, src) {
499
547
  for (const key3 in src) {
500
548
  if (hasOwn(src, key3)) {
501
549
  const srcValue = src[key3];
502
550
  const targetValue = target[key3];
503
- const customResult = callback == null ? void 0 : callback(targetValue, srcValue, key3, object, source);
551
+ const customResult = fn(targetValue, srcValue, key3, object, source);
504
552
  if (customResult !== void 0) {
505
553
  target[key3] = customResult;
506
554
  } else if (isObject(srcValue)) {
@@ -518,6 +566,92 @@ function mergeWith(object, source, callback) {
518
566
  }
519
567
  return baseMerge(object, source);
520
568
  }
569
+ function merge(object, source) {
570
+ return mergeWith(object, source, () => void 0);
571
+ }
572
+ function cloneDeep(value) {
573
+ return cloneDeepWith(value, () => void 0);
574
+ }
575
+ function cloneDeepWith(value, fn) {
576
+ const cache = /* @__PURE__ */ new WeakMap();
577
+ function baseCloneDeep(value2, cache2) {
578
+ const customResult = fn(value2);
579
+ if (customResult !== void 0) {
580
+ return customResult;
581
+ }
582
+ if (!isObject(value2)) {
583
+ return value2;
584
+ }
585
+ if (cache2.has(value2)) {
586
+ return cache2.get(value2);
587
+ }
588
+ if (isDate(value2)) {
589
+ return new Date(value2);
590
+ }
591
+ if (isRegExp(value2)) {
592
+ return new RegExp(value2);
593
+ }
594
+ if (isMap(value2)) {
595
+ const result = /* @__PURE__ */ new Map();
596
+ cache2.set(value2, result);
597
+ value2.forEach((val, key3) => {
598
+ result.set(baseCloneDeep(key3, cache2), baseCloneDeep(val, cache2));
599
+ });
600
+ return result;
601
+ }
602
+ if (isSet(value2)) {
603
+ const result = /* @__PURE__ */ new Set();
604
+ cache2.set(value2, result);
605
+ value2.forEach((val) => {
606
+ result.add(baseCloneDeep(val, cache2));
607
+ });
608
+ return result;
609
+ }
610
+ if (toRawType(value2) === "String" || toRawType(value2) === "Number" || toRawType(value2) === "Boolean") {
611
+ return newConstructor(value2, value2.valueOf());
612
+ }
613
+ if (isWeakMap(value2) || isWeakSet(value2)) {
614
+ return {};
615
+ }
616
+ if (isTypedArray(value2)) {
617
+ return newConstructor(value2, baseCloneArrayBuffer(value2.buffer), value2.byteOffset, value2.length);
618
+ }
619
+ if (isDataView(value2)) {
620
+ return newConstructor(value2, baseCloneArrayBuffer(value2.buffer), value2.byteOffset, value2.byteLength);
621
+ }
622
+ if (isArrayBuffer(value2)) {
623
+ return baseCloneArrayBuffer(value2);
624
+ }
625
+ if (isArray(value2)) {
626
+ const result = [];
627
+ cache2.set(value2, result);
628
+ value2.forEach((value3, index) => {
629
+ result[index] = baseCloneDeep(value3, cache2);
630
+ });
631
+ return result;
632
+ }
633
+ if (isPlainObject(value2)) {
634
+ const result = Object.create(Reflect.getPrototypeOf(value2));
635
+ cache2.set(value2, result);
636
+ for (const key3 in value2) {
637
+ if (hasOwn(value2, key3)) {
638
+ result[key3] = baseCloneDeep(value2[key3], cache2);
639
+ }
640
+ }
641
+ return result;
642
+ }
643
+ return value2;
644
+ }
645
+ function baseCloneArrayBuffer(value2) {
646
+ const result = new ArrayBuffer(value2.byteLength);
647
+ new Uint8Array(result).set(new Uint8Array(value2));
648
+ return result;
649
+ }
650
+ function newConstructor(value2, ...args) {
651
+ return new value2.constructor(...args);
652
+ }
653
+ return baseCloneDeep(value, cache);
654
+ }
521
655
 
522
656
  // src/json.ts
523
657
  function tryParseJSON(json) {
@@ -587,6 +721,38 @@ function createStorage(storage) {
587
721
  }
588
722
  var sessionStorage = createStorage(globalThis.sessionStorage);
589
723
  var localStorage = createStorage(globalThis.localStorage);
724
+
725
+ // src/math.ts
726
+ function sum(arr) {
727
+ return arr.reduce((ret, val) => ret + val, 0);
728
+ }
729
+ function sumBy(arr, fn) {
730
+ return arr.reduce((ret, val) => ret + fn(val), 0);
731
+ }
732
+ function minBy(arr, fn) {
733
+ if (!arr.length) {
734
+ return;
735
+ }
736
+ return arr.reduce((result, item) => fn(result) < fn(item) ? result : item, arr[0]);
737
+ }
738
+ function maxBy(arr, fn) {
739
+ if (!arr.length) {
740
+ return;
741
+ }
742
+ return arr.reduce((result, item) => fn(result) > fn(item) ? result : item, arr[0]);
743
+ }
744
+ function mean(arr) {
745
+ return sum(arr) / arr.length;
746
+ }
747
+ function meanBy(arr, fn) {
748
+ return sumBy(arr, fn) / arr.length;
749
+ }
750
+ function sample(arr) {
751
+ if (!arr.length) {
752
+ return;
753
+ }
754
+ return arr[randomNumber(0, arr.length - 1)];
755
+ }
590
756
  // Annotate the CommonJS export names for ESM import in node:
591
757
  0 && (module.exports = {
592
758
  NOOP,
@@ -594,13 +760,15 @@ var localStorage = createStorage(globalThis.localStorage);
594
760
  call,
595
761
  camelize,
596
762
  cancelAnimationFrame,
597
- capitalizeFirstLetter,
598
763
  clamp,
599
764
  clampArrayRange,
600
765
  classes,
766
+ cloneDeep,
767
+ cloneDeepWith,
601
768
  createNamespaceFn,
602
769
  createStorage,
603
770
  debounce,
771
+ delay,
604
772
  doubleRaf,
605
773
  find,
606
774
  genNumberKey,
@@ -617,7 +785,9 @@ var localStorage = createStorage(globalThis.localStorage);
617
785
  inMobile,
618
786
  inViewport,
619
787
  isArray,
788
+ isArrayBuffer,
620
789
  isBoolean,
790
+ isDataView,
621
791
  isDate,
622
792
  isEmpty,
623
793
  isFunction,
@@ -634,10 +804,19 @@ var localStorage = createStorage(globalThis.localStorage);
634
804
  isString,
635
805
  isSymbol,
636
806
  isTruthy,
807
+ isTypedArray,
808
+ isWeakMap,
809
+ isWeakSet,
637
810
  isWindow,
638
811
  kebabCase,
639
812
  localStorage,
813
+ lowerFirst,
814
+ maxBy,
815
+ mean,
816
+ meanBy,
817
+ merge,
640
818
  mergeWith,
819
+ minBy,
641
820
  normalizeToArray,
642
821
  objectToString,
643
822
  pascalCase,
@@ -649,6 +828,7 @@ var localStorage = createStorage(globalThis.localStorage);
649
828
  removeArrayEmpty,
650
829
  removeItem,
651
830
  requestAnimationFrame,
831
+ sample,
652
832
  sessionStorage,
653
833
  shuffle,
654
834
  slash,
@@ -656,6 +836,7 @@ var localStorage = createStorage(globalThis.localStorage);
656
836
  sumBy,
657
837
  supportTouch,
658
838
  throttle,
839
+ times,
659
840
  toArrayBuffer,
660
841
  toDataURL,
661
842
  toNumber,
@@ -665,5 +846,6 @@ var localStorage = createStorage(globalThis.localStorage);
665
846
  toggleItem,
666
847
  tryParseJSON,
667
848
  uniq,
668
- uniqBy
849
+ uniqBy,
850
+ upperFirst
669
851
  });
package/lib/index.d.cts CHANGED
@@ -8,8 +8,6 @@ declare const removeArrayEmpty: <T>(arr: Array<T | null | undefined | "">) => T[
8
8
  declare function find<T>(arr: Array<T>, fn: (item: T, index: number, array: Array<T>) => any, from?: 'start' | 'end'): [T, number] | [null, -1];
9
9
  declare function at<T>(arr: T[], index: number): T | undefined;
10
10
  declare function shuffle<T>(arr: T[]): T[];
11
- declare function sum(arr: number[]): number;
12
- declare function sumBy<T>(arr: T[], fn: (val: T) => number): number;
13
11
 
14
12
  declare function requestAnimationFrame(fn: FrameRequestCallback): number;
15
13
  declare function cancelAnimationFrame(handle: number): void;
@@ -34,8 +32,8 @@ declare function createNamespaceFn<N extends string>(namespace: N): <C extends s
34
32
  };
35
33
 
36
34
  declare function NOOP(): void;
37
- declare function debounce(fn: any, delay?: number): (this: unknown, ...args: any[]) => void;
38
- declare function throttle(fn: any, delay?: number): () => void;
35
+ declare function debounce<F extends (...args: any[]) => any>(fn: F, delay?: number): (this: unknown, ...args: Parameters<F>) => void;
36
+ declare function throttle<F extends (...args: any[]) => any>(fn: F, delay?: number): (this: unknown, ...args: Parameters<F>) => void;
39
37
  declare function call<P extends any[], R>(fn?: ((...arg: P) => R) | ((...arg: P) => R)[] | null, ...args: P): R | R[] | undefined;
40
38
 
41
39
  declare const objectToString: typeof Object.prototype.toString;
@@ -52,6 +50,11 @@ declare function isMap(val: unknown): val is Map<any, any>;
52
50
  declare function isSet(val: unknown): val is Set<any>;
53
51
  declare function isDate(val: unknown): val is Date;
54
52
  declare function isRegExp(val: unknown): val is RegExp;
53
+ declare function isWeakMap(val: unknown): val is WeakMap<any, any>;
54
+ declare function isWeakSet(val: unknown): val is WeakSet<any>;
55
+ declare function isArrayBuffer(val: unknown): val is ArrayBuffer;
56
+ declare function isTypedArray(val: unknown): boolean;
57
+ declare function isDataView(val: unknown): val is DataView;
55
58
  declare function toRawType(value: unknown): string;
56
59
  declare function isFunction(val: unknown): val is Function;
57
60
  declare function isArray(val: unknown): val is Array<any>;
@@ -71,15 +74,21 @@ declare function clamp(num: number, min: number, max: number): number;
71
74
  declare function clampArrayRange(index: number, arr: Array<unknown>): number;
72
75
  declare function genNumberKey(): number;
73
76
  declare function randomNumber(min?: number, max?: number): number;
77
+ declare function times<T>(num: number, fn: (index: number) => T): T[];
78
+ declare function delay(time: number): Promise<void>;
74
79
 
75
80
  declare function pascalCase(s: string): string;
76
81
  declare function camelize(s: string): string;
77
82
  declare function kebabCase(s: string): string;
78
83
  declare function slash(path: string): string;
79
84
  declare function genStringKey(): string;
80
- declare function capitalizeFirstLetter(s: string): string;
85
+ declare function upperFirst(s: string): string;
86
+ declare function lowerFirst(s: string): string;
81
87
 
82
- declare function mergeWith<TObject extends Record<string, any>, TSource extends Record<string, any>>(object: TObject, source: TSource, callback?: (objValue: any, srcValue: any, key: string | number | symbol, object?: TObject, source?: TSource) => any | void): TObject & TSource;
88
+ declare function mergeWith<TObject extends Record<string, any>, TSource extends Record<string, any>>(object: TObject, source: TSource, fn: (objValue: any, srcValue: any, key: string | number | symbol, object?: TObject, source?: TSource) => any): TObject & TSource;
89
+ declare function merge<TObject extends Record<string, any>, TSource extends Record<string, any>>(object: TObject, source: TSource): TObject & TSource;
90
+ declare function cloneDeep<T>(value: T): T;
91
+ declare function cloneDeepWith<T>(value: T, fn: (value: any) => any): T;
83
92
 
84
93
  declare function tryParseJSON<T>(json: string): T | undefined;
85
94
  declare function prettyJSONObject(jsonObject: Record<string, any>): string;
@@ -97,4 +106,12 @@ declare function createStorage(storage: globalThis.Storage): Storage;
97
106
  declare const sessionStorage: Storage;
98
107
  declare const localStorage: Storage;
99
108
 
100
- export { type BEM, type ClassName, type Classes, NOOP, type Storage, at, call, camelize, cancelAnimationFrame, capitalizeFirstLetter, clamp, clampArrayRange, classes, createNamespaceFn, createStorage, debounce, doubleRaf, find, genNumberKey, genStringKey, getAllParentScroller, getGlobalThis, getParentScroller, getRect, getScrollLeft, getScrollTop, getStyle, hasOwn, inBrowser, inMobile, inViewport, isArray, isBoolean, isDate, isEmpty, isFunction, isMap, isNonEmptyArray, isNullish, isNumber, isNumeric, isObject, isPlainObject, isPromise, isRegExp, isSet, isString, isSymbol, isTruthy, isWindow, kebabCase, localStorage, mergeWith, normalizeToArray, objectToString, pascalCase, prettyJSONObject, preventDefault, raf, randomNumber, removeArrayBlank, removeArrayEmpty, removeItem, requestAnimationFrame, sessionStorage, shuffle, slash, sum, sumBy, supportTouch, throttle, toArrayBuffer, toDataURL, toNumber, toRawType, toText, toTypeString, toggleItem, tryParseJSON, uniq, uniqBy };
109
+ declare function sum(arr: number[]): number;
110
+ declare function sumBy<T>(arr: T[], fn: (val: T) => number): number;
111
+ declare function minBy<T>(arr: T[], fn: (val: T) => number): T | undefined;
112
+ declare function maxBy<T>(arr: T[], fn: (val: T) => number): T | undefined;
113
+ declare function mean(arr: number[]): number;
114
+ declare function meanBy<T>(arr: T[], fn: (val: T) => number): number;
115
+ declare function sample<T>(arr: T[]): T | undefined;
116
+
117
+ export { type BEM, type ClassName, type Classes, NOOP, type Storage, at, call, camelize, cancelAnimationFrame, clamp, clampArrayRange, classes, cloneDeep, cloneDeepWith, createNamespaceFn, createStorage, debounce, delay, doubleRaf, find, genNumberKey, genStringKey, getAllParentScroller, getGlobalThis, getParentScroller, getRect, getScrollLeft, getScrollTop, getStyle, hasOwn, inBrowser, inMobile, inViewport, isArray, isArrayBuffer, isBoolean, isDataView, isDate, isEmpty, isFunction, isMap, isNonEmptyArray, isNullish, isNumber, isNumeric, isObject, isPlainObject, isPromise, isRegExp, isSet, isString, isSymbol, isTruthy, isTypedArray, isWeakMap, isWeakSet, isWindow, kebabCase, localStorage, lowerFirst, maxBy, mean, meanBy, merge, mergeWith, minBy, normalizeToArray, objectToString, pascalCase, prettyJSONObject, preventDefault, raf, randomNumber, removeArrayBlank, removeArrayEmpty, removeItem, requestAnimationFrame, sample, sessionStorage, shuffle, slash, sum, sumBy, supportTouch, throttle, times, toArrayBuffer, toDataURL, toNumber, toRawType, toText, toTypeString, toggleItem, tryParseJSON, uniq, uniqBy, upperFirst };
package/lib/index.d.ts CHANGED
@@ -8,8 +8,6 @@ declare const removeArrayEmpty: <T>(arr: Array<T | null | undefined | "">) => T[
8
8
  declare function find<T>(arr: Array<T>, fn: (item: T, index: number, array: Array<T>) => any, from?: 'start' | 'end'): [T, number] | [null, -1];
9
9
  declare function at<T>(arr: T[], index: number): T | undefined;
10
10
  declare function shuffle<T>(arr: T[]): T[];
11
- declare function sum(arr: number[]): number;
12
- declare function sumBy<T>(arr: T[], fn: (val: T) => number): number;
13
11
 
14
12
  declare function requestAnimationFrame(fn: FrameRequestCallback): number;
15
13
  declare function cancelAnimationFrame(handle: number): void;
@@ -34,8 +32,8 @@ declare function createNamespaceFn<N extends string>(namespace: N): <C extends s
34
32
  };
35
33
 
36
34
  declare function NOOP(): void;
37
- declare function debounce(fn: any, delay?: number): (this: unknown, ...args: any[]) => void;
38
- declare function throttle(fn: any, delay?: number): () => void;
35
+ declare function debounce<F extends (...args: any[]) => any>(fn: F, delay?: number): (this: unknown, ...args: Parameters<F>) => void;
36
+ declare function throttle<F extends (...args: any[]) => any>(fn: F, delay?: number): (this: unknown, ...args: Parameters<F>) => void;
39
37
  declare function call<P extends any[], R>(fn?: ((...arg: P) => R) | ((...arg: P) => R)[] | null, ...args: P): R | R[] | undefined;
40
38
 
41
39
  declare const objectToString: typeof Object.prototype.toString;
@@ -52,6 +50,11 @@ declare function isMap(val: unknown): val is Map<any, any>;
52
50
  declare function isSet(val: unknown): val is Set<any>;
53
51
  declare function isDate(val: unknown): val is Date;
54
52
  declare function isRegExp(val: unknown): val is RegExp;
53
+ declare function isWeakMap(val: unknown): val is WeakMap<any, any>;
54
+ declare function isWeakSet(val: unknown): val is WeakSet<any>;
55
+ declare function isArrayBuffer(val: unknown): val is ArrayBuffer;
56
+ declare function isTypedArray(val: unknown): boolean;
57
+ declare function isDataView(val: unknown): val is DataView;
55
58
  declare function toRawType(value: unknown): string;
56
59
  declare function isFunction(val: unknown): val is Function;
57
60
  declare function isArray(val: unknown): val is Array<any>;
@@ -71,15 +74,21 @@ declare function clamp(num: number, min: number, max: number): number;
71
74
  declare function clampArrayRange(index: number, arr: Array<unknown>): number;
72
75
  declare function genNumberKey(): number;
73
76
  declare function randomNumber(min?: number, max?: number): number;
77
+ declare function times<T>(num: number, fn: (index: number) => T): T[];
78
+ declare function delay(time: number): Promise<void>;
74
79
 
75
80
  declare function pascalCase(s: string): string;
76
81
  declare function camelize(s: string): string;
77
82
  declare function kebabCase(s: string): string;
78
83
  declare function slash(path: string): string;
79
84
  declare function genStringKey(): string;
80
- declare function capitalizeFirstLetter(s: string): string;
85
+ declare function upperFirst(s: string): string;
86
+ declare function lowerFirst(s: string): string;
81
87
 
82
- declare function mergeWith<TObject extends Record<string, any>, TSource extends Record<string, any>>(object: TObject, source: TSource, callback?: (objValue: any, srcValue: any, key: string | number | symbol, object?: TObject, source?: TSource) => any | void): TObject & TSource;
88
+ declare function mergeWith<TObject extends Record<string, any>, TSource extends Record<string, any>>(object: TObject, source: TSource, fn: (objValue: any, srcValue: any, key: string | number | symbol, object?: TObject, source?: TSource) => any): TObject & TSource;
89
+ declare function merge<TObject extends Record<string, any>, TSource extends Record<string, any>>(object: TObject, source: TSource): TObject & TSource;
90
+ declare function cloneDeep<T>(value: T): T;
91
+ declare function cloneDeepWith<T>(value: T, fn: (value: any) => any): T;
83
92
 
84
93
  declare function tryParseJSON<T>(json: string): T | undefined;
85
94
  declare function prettyJSONObject(jsonObject: Record<string, any>): string;
@@ -97,4 +106,12 @@ declare function createStorage(storage: globalThis.Storage): Storage;
97
106
  declare const sessionStorage: Storage;
98
107
  declare const localStorage: Storage;
99
108
 
100
- export { type BEM, type ClassName, type Classes, NOOP, type Storage, at, call, camelize, cancelAnimationFrame, capitalizeFirstLetter, clamp, clampArrayRange, classes, createNamespaceFn, createStorage, debounce, doubleRaf, find, genNumberKey, genStringKey, getAllParentScroller, getGlobalThis, getParentScroller, getRect, getScrollLeft, getScrollTop, getStyle, hasOwn, inBrowser, inMobile, inViewport, isArray, isBoolean, isDate, isEmpty, isFunction, isMap, isNonEmptyArray, isNullish, isNumber, isNumeric, isObject, isPlainObject, isPromise, isRegExp, isSet, isString, isSymbol, isTruthy, isWindow, kebabCase, localStorage, mergeWith, normalizeToArray, objectToString, pascalCase, prettyJSONObject, preventDefault, raf, randomNumber, removeArrayBlank, removeArrayEmpty, removeItem, requestAnimationFrame, sessionStorage, shuffle, slash, sum, sumBy, supportTouch, throttle, toArrayBuffer, toDataURL, toNumber, toRawType, toText, toTypeString, toggleItem, tryParseJSON, uniq, uniqBy };
109
+ declare function sum(arr: number[]): number;
110
+ declare function sumBy<T>(arr: T[], fn: (val: T) => number): number;
111
+ declare function minBy<T>(arr: T[], fn: (val: T) => number): T | undefined;
112
+ declare function maxBy<T>(arr: T[], fn: (val: T) => number): T | undefined;
113
+ declare function mean(arr: number[]): number;
114
+ declare function meanBy<T>(arr: T[], fn: (val: T) => number): number;
115
+ declare function sample<T>(arr: T[]): T | undefined;
116
+
117
+ export { type BEM, type ClassName, type Classes, NOOP, type Storage, at, call, camelize, cancelAnimationFrame, clamp, clampArrayRange, classes, cloneDeep, cloneDeepWith, createNamespaceFn, createStorage, debounce, delay, doubleRaf, find, genNumberKey, genStringKey, getAllParentScroller, getGlobalThis, getParentScroller, getRect, getScrollLeft, getScrollTop, getStyle, hasOwn, inBrowser, inMobile, inViewport, isArray, isArrayBuffer, isBoolean, isDataView, isDate, isEmpty, isFunction, isMap, isNonEmptyArray, isNullish, isNumber, isNumeric, isObject, isPlainObject, isPromise, isRegExp, isSet, isString, isSymbol, isTruthy, isTypedArray, isWeakMap, isWeakSet, isWindow, kebabCase, localStorage, lowerFirst, maxBy, mean, meanBy, merge, mergeWith, minBy, normalizeToArray, objectToString, pascalCase, prettyJSONObject, preventDefault, raf, randomNumber, removeArrayBlank, removeArrayEmpty, removeItem, requestAnimationFrame, sample, sessionStorage, shuffle, slash, sum, sumBy, supportTouch, throttle, times, toArrayBuffer, toDataURL, toNumber, toRawType, toText, toTypeString, toggleItem, tryParseJSON, uniq, uniqBy, upperFirst };
package/lib/index.js CHANGED
@@ -48,16 +48,43 @@ function isPromise(val) {
48
48
  return isObject(val) && isFunction(val.then) && isFunction(val.catch);
49
49
  }
50
50
  function isMap(val) {
51
- return toTypeString(val) === "[object Map]";
51
+ return toRawType(val) === "Map";
52
52
  }
53
53
  function isSet(val) {
54
- return toTypeString(val) === "[object Set]";
54
+ return toRawType(val) === "Set";
55
55
  }
56
56
  function isDate(val) {
57
- return toTypeString(val) === "[object Date]";
57
+ return toRawType(val) === "Date";
58
58
  }
59
59
  function isRegExp(val) {
60
- return toTypeString(val) === "[object RegExp]";
60
+ return toRawType(val) === "RegExp";
61
+ }
62
+ function isWeakMap(val) {
63
+ return toRawType(val) === "WeakMap";
64
+ }
65
+ function isWeakSet(val) {
66
+ return toRawType(val) === "WeakSet";
67
+ }
68
+ function isArrayBuffer(val) {
69
+ return toRawType(val) === "ArrayBuffer";
70
+ }
71
+ function isTypedArray(val) {
72
+ return [
73
+ "Int8Array",
74
+ "Uint8Array",
75
+ "Uint8ClampedArray",
76
+ "Int16Array",
77
+ "Uint16Array",
78
+ "Int32Array",
79
+ "Uint32Array",
80
+ "Float32Array",
81
+ "Float64Array",
82
+ "BigInt64Array",
83
+ "BigUint64Array"
84
+ ].includes(toRawType(val));
85
+ }
86
+ function isDataView(val) {
87
+ return toRawType(val) === "DataView";
61
88
  }
62
89
  function toRawType(value) {
63
90
  return toTypeString(value).slice(8, -1);
@@ -165,12 +192,6 @@ function shuffle(arr) {
165
192
  }
166
193
  return arr;
167
194
  }
168
- function sum(arr) {
169
- return arr.reduce((ret, val) => ret + val, 0);
170
- }
171
- function sumBy(arr, fn) {
172
- return arr.reduce((ret, val) => ret + fn(val), 0);
173
- }
174
195
 
175
196
  // src/string.ts
176
197
  function pascalCase(s) {
@@ -195,9 +216,12 @@ var key = 0;
195
216
  function genStringKey() {
196
217
  return `generated-key-${key++}`;
197
218
  }
198
- function capitalizeFirstLetter(s) {
219
+ function upperFirst(s) {
199
220
  return s.charAt(0).toUpperCase() + s.slice(1);
200
221
  }
222
+ function lowerFirst(s) {
223
+ return s.charAt(0).toLowerCase() + s.slice(1);
224
+ }
201
225
 
202
226
  // src/element.ts
203
227
  function requestAnimationFrame(fn) {
@@ -323,7 +347,7 @@ function createNamespaceFn(namespace) {
323
347
  // src/function.ts
324
348
  function NOOP() {
325
349
  }
326
- function debounce(fn, delay = 0) {
350
+ function debounce(fn, delay2 = 0) {
327
351
  let timer;
328
352
  return function(...args) {
329
353
  if (timer) {
@@ -331,10 +355,10 @@ function debounce(fn, delay = 0) {
331
355
  }
332
356
  timer = setTimeout(() => {
333
357
  fn.apply(this, args);
334
- }, delay);
358
+ }, delay2);
335
359
  };
336
360
  }
337
- function throttle(fn, delay = 200) {
361
+ function throttle(fn, delay2 = 200) {
338
362
  let timer;
339
363
  let start = 0;
340
364
  return function loop(...args) {
@@ -346,13 +370,13 @@ function throttle(fn, delay = 200) {
346
370
  if (timer) {
347
371
  clearTimeout(timer);
348
372
  }
349
- if (elapsed >= delay) {
373
+ if (elapsed >= delay2) {
350
374
  fn.apply(this, args);
351
375
  start = now;
352
376
  } else {
353
377
  timer = setTimeout(() => {
354
378
  loop.apply(this, args);
355
- }, delay - elapsed);
379
+ }, delay2 - elapsed);
356
380
  }
357
381
  };
358
382
  }
@@ -393,15 +417,23 @@ function genNumberKey() {
393
417
  function randomNumber(min = 0, max = 100) {
394
418
  return Math.floor(Math.random() * (max - min + 1)) + min;
395
419
  }
420
+ function times(num, fn) {
421
+ return Array.from({ length: num }, (_, index) => fn(index));
422
+ }
423
+ function delay(time) {
424
+ return new Promise((resolve) => {
425
+ setTimeout(resolve, time);
426
+ });
427
+ }
396
428
 
397
429
  // src/collection.ts
398
- function mergeWith(object, source, callback) {
430
+ function mergeWith(object, source, fn) {
399
431
  function baseMerge(target, src) {
400
432
  for (const key3 in src) {
401
433
  if (hasOwn(src, key3)) {
402
434
  const srcValue = src[key3];
403
435
  const targetValue = target[key3];
404
- const customResult = callback == null ? void 0 : callback(targetValue, srcValue, key3, object, source);
436
+ const customResult = fn(targetValue, srcValue, key3, object, source);
405
437
  if (customResult !== void 0) {
406
438
  target[key3] = customResult;
407
439
  } else if (isObject(srcValue)) {
@@ -419,6 +451,92 @@ function mergeWith(object, source, callback) {
419
451
  }
420
452
  return baseMerge(object, source);
421
453
  }
454
+ function merge(object, source) {
455
+ return mergeWith(object, source, () => void 0);
456
+ }
457
+ function cloneDeep(value) {
458
+ return cloneDeepWith(value, () => void 0);
459
+ }
460
+ function cloneDeepWith(value, fn) {
461
+ const cache = /* @__PURE__ */ new WeakMap();
462
+ function baseCloneDeep(value2, cache2) {
463
+ const customResult = fn(value2);
464
+ if (customResult !== void 0) {
465
+ return customResult;
466
+ }
467
+ if (!isObject(value2)) {
468
+ return value2;
469
+ }
470
+ if (cache2.has(value2)) {
471
+ return cache2.get(value2);
472
+ }
473
+ if (isDate(value2)) {
474
+ return new Date(value2);
475
+ }
476
+ if (isRegExp(value2)) {
477
+ return new RegExp(value2);
478
+ }
479
+ if (isMap(value2)) {
480
+ const result = /* @__PURE__ */ new Map();
481
+ cache2.set(value2, result);
482
+ value2.forEach((val, key3) => {
483
+ result.set(baseCloneDeep(key3, cache2), baseCloneDeep(val, cache2));
484
+ });
485
+ return result;
486
+ }
487
+ if (isSet(value2)) {
488
+ const result = /* @__PURE__ */ new Set();
489
+ cache2.set(value2, result);
490
+ value2.forEach((val) => {
491
+ result.add(baseCloneDeep(val, cache2));
492
+ });
493
+ return result;
494
+ }
495
+ if (toRawType(value2) === "String" || toRawType(value2) === "Number" || toRawType(value2) === "Boolean") {
496
+ return newConstructor(value2, value2.valueOf());
497
+ }
498
+ if (isWeakMap(value2) || isWeakSet(value2)) {
499
+ return {};
500
+ }
501
+ if (isTypedArray(value2)) {
502
+ return newConstructor(value2, baseCloneArrayBuffer(value2.buffer), value2.byteOffset, value2.length);
503
+ }
504
+ if (isDataView(value2)) {
505
+ return newConstructor(value2, baseCloneArrayBuffer(value2.buffer), value2.byteOffset, value2.byteLength);
506
+ }
507
+ if (isArrayBuffer(value2)) {
508
+ return baseCloneArrayBuffer(value2);
509
+ }
510
+ if (isArray(value2)) {
511
+ const result = [];
512
+ cache2.set(value2, result);
513
+ value2.forEach((value3, index) => {
514
+ result[index] = baseCloneDeep(value3, cache2);
515
+ });
516
+ return result;
517
+ }
518
+ if (isPlainObject(value2)) {
519
+ const result = Object.create(Reflect.getPrototypeOf(value2));
520
+ cache2.set(value2, result);
521
+ for (const key3 in value2) {
522
+ if (hasOwn(value2, key3)) {
523
+ result[key3] = baseCloneDeep(value2[key3], cache2);
524
+ }
525
+ }
526
+ return result;
527
+ }
528
+ return value2;
529
+ }
530
+ function baseCloneArrayBuffer(value2) {
531
+ const result = new ArrayBuffer(value2.byteLength);
532
+ new Uint8Array(result).set(new Uint8Array(value2));
533
+ return result;
534
+ }
535
+ function newConstructor(value2, ...args) {
536
+ return new value2.constructor(...args);
537
+ }
538
+ return baseCloneDeep(value, cache);
539
+ }
422
540
 
423
541
  // src/json.ts
424
542
  function tryParseJSON(json) {
@@ -488,19 +606,53 @@ function createStorage(storage) {
488
606
  }
489
607
  var sessionStorage = createStorage(globalThis.sessionStorage);
490
608
  var localStorage = createStorage(globalThis.localStorage);
609
+
610
+ // src/math.ts
611
+ function sum(arr) {
612
+ return arr.reduce((ret, val) => ret + val, 0);
613
+ }
614
+ function sumBy(arr, fn) {
615
+ return arr.reduce((ret, val) => ret + fn(val), 0);
616
+ }
617
+ function minBy(arr, fn) {
618
+ if (!arr.length) {
619
+ return;
620
+ }
621
+ return arr.reduce((result, item) => fn(result) < fn(item) ? result : item, arr[0]);
622
+ }
623
+ function maxBy(arr, fn) {
624
+ if (!arr.length) {
625
+ return;
626
+ }
627
+ return arr.reduce((result, item) => fn(result) > fn(item) ? result : item, arr[0]);
628
+ }
629
+ function mean(arr) {
630
+ return sum(arr) / arr.length;
631
+ }
632
+ function meanBy(arr, fn) {
633
+ return sumBy(arr, fn) / arr.length;
634
+ }
635
+ function sample(arr) {
636
+ if (!arr.length) {
637
+ return;
638
+ }
639
+ return arr[randomNumber(0, arr.length - 1)];
640
+ }
491
641
  export {
492
642
  NOOP,
493
643
  at,
494
644
  call,
495
645
  camelize,
496
646
  cancelAnimationFrame,
497
- capitalizeFirstLetter,
498
647
  clamp,
499
648
  clampArrayRange,
500
649
  classes,
650
+ cloneDeep,
651
+ cloneDeepWith,
501
652
  createNamespaceFn,
502
653
  createStorage,
503
654
  debounce,
655
+ delay,
504
656
  doubleRaf,
505
657
  find,
506
658
  genNumberKey,
@@ -517,7 +669,9 @@ export {
517
669
  inMobile,
518
670
  inViewport,
519
671
  isArray,
672
+ isArrayBuffer,
520
673
  isBoolean,
674
+ isDataView,
521
675
  isDate,
522
676
  isEmpty,
523
677
  isFunction,
@@ -534,10 +688,19 @@ export {
534
688
  isString,
535
689
  isSymbol,
536
690
  isTruthy,
691
+ isTypedArray,
692
+ isWeakMap,
693
+ isWeakSet,
537
694
  isWindow,
538
695
  kebabCase,
539
696
  localStorage,
697
+ lowerFirst,
698
+ maxBy,
699
+ mean,
700
+ meanBy,
701
+ merge,
540
702
  mergeWith,
703
+ minBy,
541
704
  normalizeToArray,
542
705
  objectToString,
543
706
  pascalCase,
@@ -549,6 +712,7 @@ export {
549
712
  removeArrayEmpty,
550
713
  removeItem,
551
714
  requestAnimationFrame,
715
+ sample,
552
716
  sessionStorage,
553
717
  shuffle,
554
718
  slash,
@@ -556,6 +720,7 @@ export {
556
720
  sumBy,
557
721
  supportTouch,
558
722
  throttle,
723
+ times,
559
724
  toArrayBuffer,
560
725
  toDataURL,
561
726
  toNumber,
@@ -565,5 +730,6 @@ export {
565
730
  toggleItem,
566
731
  tryParseJSON,
567
732
  uniq,
568
- uniqBy
733
+ uniqBy,
734
+ upperFirst
569
735
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rattail",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "type": "module",
5
5
  "main": "lib/index.cjs",
6
6
  "module": "lib/index.js",
@@ -51,7 +51,7 @@
51
51
  "scripts": {
52
52
  "dev": "tsup src/index.ts --format esm --out-dir=lib --watch --dts",
53
53
  "build": "tsup src/index.ts --format esm,cjs --out-dir=lib --dts --clean",
54
- "lint": "eslint --ext .ts,.js",
54
+ "lint": "eslint . --fix --ext .ts,.js",
55
55
  "format": "prettier --write .",
56
56
  "clean": "rimraf node_modules lib",
57
57
  "test:watch": "vitest --coverage",