@ntnyq/utils 0.1.3 → 0.2.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 +127 -5
- package/dist/index.d.cts +59 -4
- package/dist/index.d.ts +59 -4
- package/dist/index.js +118 -4
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -27,11 +27,15 @@ __export(src_exports, {
|
|
|
27
27
|
chunk: () => chunk,
|
|
28
28
|
clamp: () => clamp,
|
|
29
29
|
days: () => days,
|
|
30
|
+
debounce: () => debounce,
|
|
31
|
+
ensurePrefix: () => ensurePrefix,
|
|
32
|
+
ensureSuffix: () => ensureSuffix,
|
|
30
33
|
flatCase: () => flatCase,
|
|
31
34
|
getObjectType: () => getObjectType,
|
|
32
|
-
|
|
35
|
+
hasOwn: () => hasOwn,
|
|
33
36
|
hours: () => hours,
|
|
34
37
|
isArray: () => isArray,
|
|
38
|
+
isArrayEqual: () => isArrayEqual,
|
|
35
39
|
isBoolean: () => isBoolean,
|
|
36
40
|
isBrowser: () => isBrowser,
|
|
37
41
|
isFunction: () => isFunction,
|
|
@@ -49,15 +53,19 @@ __export(src_exports, {
|
|
|
49
53
|
minutes: () => minutes,
|
|
50
54
|
noop: () => noop,
|
|
51
55
|
omit: () => omit,
|
|
56
|
+
once: () => once,
|
|
52
57
|
pascalCase: () => pascalCase,
|
|
53
58
|
pick: () => pick,
|
|
54
59
|
rAF: () => rAF,
|
|
55
60
|
seconds: () => seconds,
|
|
61
|
+
slash: () => slash,
|
|
56
62
|
snakeCase: () => snakeCase,
|
|
57
63
|
splitByCase: () => splitByCase,
|
|
64
|
+
throttle: () => throttle,
|
|
58
65
|
titleCase: () => titleCase,
|
|
59
66
|
toArray: () => toArray,
|
|
60
67
|
trainCase: () => trainCase,
|
|
68
|
+
unindent: () => unindent,
|
|
61
69
|
unique: () => unique,
|
|
62
70
|
uniqueBy: () => uniqueBy,
|
|
63
71
|
upperFirst: () => upperFirst,
|
|
@@ -110,6 +118,19 @@ var noop = () => {
|
|
|
110
118
|
};
|
|
111
119
|
var NOOP = noop;
|
|
112
120
|
|
|
121
|
+
// src/fn/once.ts
|
|
122
|
+
function once(func) {
|
|
123
|
+
let called = false;
|
|
124
|
+
return function(...args) {
|
|
125
|
+
if (called) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
called = true;
|
|
129
|
+
func.apply(this, args);
|
|
130
|
+
return true;
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
113
134
|
// src/env/isBrowser.ts
|
|
114
135
|
var isBrowser = () => typeof document !== "undefined";
|
|
115
136
|
|
|
@@ -247,6 +268,53 @@ function waitFor(ms) {
|
|
|
247
268
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
248
269
|
}
|
|
249
270
|
|
|
271
|
+
// src/misc/throttle.ts
|
|
272
|
+
function throttle(delay, callback, options = {}) {
|
|
273
|
+
const { isDebounce } = options;
|
|
274
|
+
let lastExec = 0;
|
|
275
|
+
let cancelled = false;
|
|
276
|
+
let timeoutId;
|
|
277
|
+
function clearExistingTimeout() {
|
|
278
|
+
if (timeoutId) {
|
|
279
|
+
clearTimeout(timeoutId);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
function cancel() {
|
|
283
|
+
clearExistingTimeout();
|
|
284
|
+
cancelled = true;
|
|
285
|
+
}
|
|
286
|
+
function wrapper(...args) {
|
|
287
|
+
if (cancelled) return;
|
|
288
|
+
const _this = this;
|
|
289
|
+
const now = Date.now();
|
|
290
|
+
const elapsed = now - lastExec;
|
|
291
|
+
function clear() {
|
|
292
|
+
timeoutId = void 0;
|
|
293
|
+
}
|
|
294
|
+
function exec(cur) {
|
|
295
|
+
lastExec = cur || Date.now();
|
|
296
|
+
callback.apply(_this, args);
|
|
297
|
+
}
|
|
298
|
+
if (isDebounce && !timeoutId) {
|
|
299
|
+
exec(now);
|
|
300
|
+
}
|
|
301
|
+
clearExistingTimeout();
|
|
302
|
+
if (!isDebounce && elapsed > delay) {
|
|
303
|
+
exec(now);
|
|
304
|
+
} else {
|
|
305
|
+
timeoutId = setTimeout(isDebounce ? clear : exec, isDebounce ? delay : delay - elapsed);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
wrapper.cancel = cancel;
|
|
309
|
+
return wrapper;
|
|
310
|
+
}
|
|
311
|
+
function debounce(delay, callback, options = {}) {
|
|
312
|
+
return throttle(delay, callback, {
|
|
313
|
+
...options,
|
|
314
|
+
isDebounce: true
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
250
318
|
// src/misc/warnOnce.ts
|
|
251
319
|
var warned = /* @__PURE__ */ new Set();
|
|
252
320
|
var warnOnce = (message) => {
|
|
@@ -286,6 +354,14 @@ function toArray(array) {
|
|
|
286
354
|
return Array.isArray(array) ? array : [array];
|
|
287
355
|
}
|
|
288
356
|
|
|
357
|
+
// src/array/isArrayEqual.ts
|
|
358
|
+
function isArrayEqual(array1, array2) {
|
|
359
|
+
if (array1.length !== array2.length) {
|
|
360
|
+
return false;
|
|
361
|
+
}
|
|
362
|
+
return array1.every((item, idx) => item === array2[idx]);
|
|
363
|
+
}
|
|
364
|
+
|
|
289
365
|
// src/string/join.ts
|
|
290
366
|
function join(array, options = {}) {
|
|
291
367
|
const { separator = "" } = options;
|
|
@@ -293,14 +369,52 @@ function join(array, options = {}) {
|
|
|
293
369
|
return array.filter((v) => Boolean(v) || v === 0).join(separator);
|
|
294
370
|
}
|
|
295
371
|
|
|
372
|
+
// src/string/slash.ts
|
|
373
|
+
function slash(input) {
|
|
374
|
+
return input.replace(/\\/g, "/");
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// src/string/unindent.ts
|
|
378
|
+
var _RE_FULL_WS = /^\s*$/;
|
|
379
|
+
function unindent(input) {
|
|
380
|
+
const lines = (typeof input === "string" ? input : input[0]).split("\n");
|
|
381
|
+
const whitespaceLines = lines.map((line) => _RE_FULL_WS.test(line));
|
|
382
|
+
const commonIndent = lines.reduce((min, line, idx) => {
|
|
383
|
+
if (!whitespaceLines[idx]) {
|
|
384
|
+
return min;
|
|
385
|
+
}
|
|
386
|
+
const indent = line.match(/^\s/)?.[0].length;
|
|
387
|
+
return indent === void 0 ? min : Math.min(min, indent);
|
|
388
|
+
}, Number.POSITIVE_INFINITY);
|
|
389
|
+
let emptylinesHead = 0;
|
|
390
|
+
while (emptylinesHead < lines.length && whitespaceLines[emptylinesHead]) {
|
|
391
|
+
emptylinesHead++;
|
|
392
|
+
}
|
|
393
|
+
let emptylinesTail = 0;
|
|
394
|
+
while (emptylinesTail < lines.length && whitespaceLines[lines.length - emptylinesTail - 1]) {
|
|
395
|
+
emptylinesTail++;
|
|
396
|
+
}
|
|
397
|
+
return lines.slice(emptylinesHead, lines.length - emptylinesTail).map((line) => line.slice(commonIndent)).join("\n");
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// src/string/ensurePrefix.ts
|
|
401
|
+
function ensurePrefix(input, prefix) {
|
|
402
|
+
return input.startsWith(prefix) ? input : `${prefix}${input}`;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// src/string/ensureSuffix.ts
|
|
406
|
+
function ensureSuffix(input, suffix) {
|
|
407
|
+
return input.endsWith(suffix) ? input : `${input}${suffix}`;
|
|
408
|
+
}
|
|
409
|
+
|
|
296
410
|
// src/object/omit.ts
|
|
297
411
|
function omit(object, ...keys) {
|
|
298
412
|
keys.forEach((key) => delete object[key]);
|
|
299
413
|
return object;
|
|
300
414
|
}
|
|
301
415
|
|
|
302
|
-
// src/object/
|
|
303
|
-
function
|
|
416
|
+
// src/object/hasOwn.ts
|
|
417
|
+
function hasOwn(object, key) {
|
|
304
418
|
return Object.prototype.hasOwnProperty.call(object, key);
|
|
305
419
|
}
|
|
306
420
|
|
|
@@ -309,7 +423,7 @@ function pick(object, keys) {
|
|
|
309
423
|
return Object.assign(
|
|
310
424
|
{},
|
|
311
425
|
...keys.map((key) => {
|
|
312
|
-
if (object &&
|
|
426
|
+
if (object && hasOwn(object, key)) {
|
|
313
427
|
return { [key]: object[key] };
|
|
314
428
|
}
|
|
315
429
|
})
|
|
@@ -324,11 +438,15 @@ function pick(object, keys) {
|
|
|
324
438
|
chunk,
|
|
325
439
|
clamp,
|
|
326
440
|
days,
|
|
441
|
+
debounce,
|
|
442
|
+
ensurePrefix,
|
|
443
|
+
ensureSuffix,
|
|
327
444
|
flatCase,
|
|
328
445
|
getObjectType,
|
|
329
|
-
|
|
446
|
+
hasOwn,
|
|
330
447
|
hours,
|
|
331
448
|
isArray,
|
|
449
|
+
isArrayEqual,
|
|
332
450
|
isBoolean,
|
|
333
451
|
isBrowser,
|
|
334
452
|
isFunction,
|
|
@@ -346,15 +464,19 @@ function pick(object, keys) {
|
|
|
346
464
|
minutes,
|
|
347
465
|
noop,
|
|
348
466
|
omit,
|
|
467
|
+
once,
|
|
349
468
|
pascalCase,
|
|
350
469
|
pick,
|
|
351
470
|
rAF,
|
|
352
471
|
seconds,
|
|
472
|
+
slash,
|
|
353
473
|
snakeCase,
|
|
354
474
|
splitByCase,
|
|
475
|
+
throttle,
|
|
355
476
|
titleCase,
|
|
356
477
|
toArray,
|
|
357
478
|
trainCase,
|
|
479
|
+
unindent,
|
|
358
480
|
unique,
|
|
359
481
|
uniqueBy,
|
|
360
482
|
upperFirst,
|
package/dist/index.d.cts
CHANGED
|
@@ -27,6 +27,8 @@ declare const noop: () => void;
|
|
|
27
27
|
*/
|
|
28
28
|
declare const NOOP: () => void;
|
|
29
29
|
|
|
30
|
+
declare function once<T extends unknown[]>(func: (...args: T) => void): (this: unknown, ...args: T) => boolean;
|
|
31
|
+
|
|
30
32
|
/**
|
|
31
33
|
* @file env.ts
|
|
32
34
|
*/
|
|
@@ -98,6 +100,27 @@ declare function clamp(value: number, min?: number, max?: number): number;
|
|
|
98
100
|
*/
|
|
99
101
|
declare function waitFor(ms: number): Promise<unknown>;
|
|
100
102
|
|
|
103
|
+
interface ThrottleDebounceOptions {
|
|
104
|
+
/**
|
|
105
|
+
* @default false
|
|
106
|
+
*/
|
|
107
|
+
isDebounce?: boolean;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Throttle a function to limit its execution to a maximum of once per a specified time interval.
|
|
111
|
+
*
|
|
112
|
+
* @param delay - Zero or greater delay in milliseconds
|
|
113
|
+
* @param callback - A function to be throttled
|
|
114
|
+
* @param options - throttle options
|
|
115
|
+
* @returns A throttled function
|
|
116
|
+
*/
|
|
117
|
+
declare function throttle<T extends ((...args: any[]) => undefined | void) | undefined | null>(delay: number, callback: Exclude<T, undefined | null>, options?: ThrottleDebounceOptions): T & {
|
|
118
|
+
cancel: () => void;
|
|
119
|
+
};
|
|
120
|
+
declare function debounce<T extends ((...args: any[]) => undefined | void) | undefined | null>(delay: number, callback: Exclude<T, undefined | null>, options?: ThrottleDebounceOptions): T & {
|
|
121
|
+
cancel: () => void;
|
|
122
|
+
};
|
|
123
|
+
|
|
101
124
|
declare const warnOnce: (message: string) => void;
|
|
102
125
|
|
|
103
126
|
/**
|
|
@@ -127,7 +150,11 @@ type MayBe<T> = T | undefined;
|
|
|
127
150
|
type AnyFn<T = any, R = any> = (...args: T[]) => R;
|
|
128
151
|
type Arrayable<T> = T | T[];
|
|
129
152
|
type Awaitable<T> = T | Promise<T>;
|
|
130
|
-
type Prettify<T> =
|
|
153
|
+
type Prettify<T> = {
|
|
154
|
+
[K in keyof T]: T[K];
|
|
155
|
+
} & {};
|
|
156
|
+
type PrettifyV2<T> = Omit<T, never>;
|
|
157
|
+
type PrimitiveType = number | bigint | string | boolean | symbol | null | undefined;
|
|
131
158
|
|
|
132
159
|
/**
|
|
133
160
|
* Converts a value to an array.
|
|
@@ -136,10 +163,12 @@ type Prettify<T> = Omit<T, never>;
|
|
|
136
163
|
*/
|
|
137
164
|
declare function toArray<T>(array?: Nullable<Arrayable<T>>): T[];
|
|
138
165
|
|
|
166
|
+
declare function isArrayEqual(array1: unknown[], array2: unknown[]): boolean;
|
|
167
|
+
|
|
139
168
|
type JoinableValue = string | number | null | undefined;
|
|
140
169
|
interface JoinOptions {
|
|
141
170
|
/**
|
|
142
|
-
* @default ''
|
|
171
|
+
* @default ''
|
|
143
172
|
*/
|
|
144
173
|
separator?: string;
|
|
145
174
|
}
|
|
@@ -151,10 +180,36 @@ interface JoinOptions {
|
|
|
151
180
|
*/
|
|
152
181
|
declare function join(array: JoinableValue[], options?: JoinOptions): string;
|
|
153
182
|
|
|
183
|
+
/**
|
|
184
|
+
* Replace backslash to slash
|
|
185
|
+
*/
|
|
186
|
+
declare function slash(input: string): string;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Remove leading whitespace from a template string
|
|
190
|
+
* Empty lines at the beginning and end of the template string are also removed.
|
|
191
|
+
* @param input template string
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
*
|
|
195
|
+
* ```ts
|
|
196
|
+
* const str = unindent`
|
|
197
|
+
* if (foo) {
|
|
198
|
+
* bar()
|
|
199
|
+
* }
|
|
200
|
+
* `
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
declare function unindent(input: TemplateStringsArray | string): string;
|
|
204
|
+
|
|
205
|
+
declare function ensurePrefix(input: string, prefix: string): string;
|
|
206
|
+
|
|
207
|
+
declare function ensureSuffix(input: string, suffix: string): string;
|
|
208
|
+
|
|
154
209
|
declare function omit<T, K extends keyof T>(object: T, ...keys: K[]): Omit<T, K>;
|
|
155
210
|
|
|
156
211
|
declare function pick<T, K extends keyof T>(object: T, keys: K[]): Pick<T, K>;
|
|
157
212
|
|
|
158
|
-
declare function
|
|
213
|
+
declare function hasOwn<T, K extends keyof T>(object: T, key: K): boolean;
|
|
159
214
|
|
|
160
|
-
export { type AnyFn, type Arrayable, type Awaitable, type MayBe, NOOP, type Nullable, type Prettify, cAF, capitalize, chunk, clamp, days, getObjectType,
|
|
215
|
+
export { type AnyFn, type Arrayable, type Awaitable, type MayBe, NOOP, type Nullable, type Prettify, type PrettifyV2, type PrimitiveType, type ThrottleDebounceOptions, cAF, capitalize, chunk, clamp, days, debounce, ensurePrefix, ensureSuffix, getObjectType, hasOwn, hours, isArray, isArrayEqual, isBoolean, isBrowser, isFunction, isInteger, isNativePromise, isNull, isNumber, isPromise, isString, isUndefined, join, minutes, noop, omit, once, pick, rAF, seconds, slash, throttle, toArray, unindent, unique, uniqueBy, waitFor, warnOnce, weeks };
|
package/dist/index.d.ts
CHANGED
|
@@ -27,6 +27,8 @@ declare const noop: () => void;
|
|
|
27
27
|
*/
|
|
28
28
|
declare const NOOP: () => void;
|
|
29
29
|
|
|
30
|
+
declare function once<T extends unknown[]>(func: (...args: T) => void): (this: unknown, ...args: T) => boolean;
|
|
31
|
+
|
|
30
32
|
/**
|
|
31
33
|
* @file env.ts
|
|
32
34
|
*/
|
|
@@ -98,6 +100,27 @@ declare function clamp(value: number, min?: number, max?: number): number;
|
|
|
98
100
|
*/
|
|
99
101
|
declare function waitFor(ms: number): Promise<unknown>;
|
|
100
102
|
|
|
103
|
+
interface ThrottleDebounceOptions {
|
|
104
|
+
/**
|
|
105
|
+
* @default false
|
|
106
|
+
*/
|
|
107
|
+
isDebounce?: boolean;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Throttle a function to limit its execution to a maximum of once per a specified time interval.
|
|
111
|
+
*
|
|
112
|
+
* @param delay - Zero or greater delay in milliseconds
|
|
113
|
+
* @param callback - A function to be throttled
|
|
114
|
+
* @param options - throttle options
|
|
115
|
+
* @returns A throttled function
|
|
116
|
+
*/
|
|
117
|
+
declare function throttle<T extends ((...args: any[]) => undefined | void) | undefined | null>(delay: number, callback: Exclude<T, undefined | null>, options?: ThrottleDebounceOptions): T & {
|
|
118
|
+
cancel: () => void;
|
|
119
|
+
};
|
|
120
|
+
declare function debounce<T extends ((...args: any[]) => undefined | void) | undefined | null>(delay: number, callback: Exclude<T, undefined | null>, options?: ThrottleDebounceOptions): T & {
|
|
121
|
+
cancel: () => void;
|
|
122
|
+
};
|
|
123
|
+
|
|
101
124
|
declare const warnOnce: (message: string) => void;
|
|
102
125
|
|
|
103
126
|
/**
|
|
@@ -127,7 +150,11 @@ type MayBe<T> = T | undefined;
|
|
|
127
150
|
type AnyFn<T = any, R = any> = (...args: T[]) => R;
|
|
128
151
|
type Arrayable<T> = T | T[];
|
|
129
152
|
type Awaitable<T> = T | Promise<T>;
|
|
130
|
-
type Prettify<T> =
|
|
153
|
+
type Prettify<T> = {
|
|
154
|
+
[K in keyof T]: T[K];
|
|
155
|
+
} & {};
|
|
156
|
+
type PrettifyV2<T> = Omit<T, never>;
|
|
157
|
+
type PrimitiveType = number | bigint | string | boolean | symbol | null | undefined;
|
|
131
158
|
|
|
132
159
|
/**
|
|
133
160
|
* Converts a value to an array.
|
|
@@ -136,10 +163,12 @@ type Prettify<T> = Omit<T, never>;
|
|
|
136
163
|
*/
|
|
137
164
|
declare function toArray<T>(array?: Nullable<Arrayable<T>>): T[];
|
|
138
165
|
|
|
166
|
+
declare function isArrayEqual(array1: unknown[], array2: unknown[]): boolean;
|
|
167
|
+
|
|
139
168
|
type JoinableValue = string | number | null | undefined;
|
|
140
169
|
interface JoinOptions {
|
|
141
170
|
/**
|
|
142
|
-
* @default ''
|
|
171
|
+
* @default ''
|
|
143
172
|
*/
|
|
144
173
|
separator?: string;
|
|
145
174
|
}
|
|
@@ -151,10 +180,36 @@ interface JoinOptions {
|
|
|
151
180
|
*/
|
|
152
181
|
declare function join(array: JoinableValue[], options?: JoinOptions): string;
|
|
153
182
|
|
|
183
|
+
/**
|
|
184
|
+
* Replace backslash to slash
|
|
185
|
+
*/
|
|
186
|
+
declare function slash(input: string): string;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Remove leading whitespace from a template string
|
|
190
|
+
* Empty lines at the beginning and end of the template string are also removed.
|
|
191
|
+
* @param input template string
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
*
|
|
195
|
+
* ```ts
|
|
196
|
+
* const str = unindent`
|
|
197
|
+
* if (foo) {
|
|
198
|
+
* bar()
|
|
199
|
+
* }
|
|
200
|
+
* `
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
declare function unindent(input: TemplateStringsArray | string): string;
|
|
204
|
+
|
|
205
|
+
declare function ensurePrefix(input: string, prefix: string): string;
|
|
206
|
+
|
|
207
|
+
declare function ensureSuffix(input: string, suffix: string): string;
|
|
208
|
+
|
|
154
209
|
declare function omit<T, K extends keyof T>(object: T, ...keys: K[]): Omit<T, K>;
|
|
155
210
|
|
|
156
211
|
declare function pick<T, K extends keyof T>(object: T, keys: K[]): Pick<T, K>;
|
|
157
212
|
|
|
158
|
-
declare function
|
|
213
|
+
declare function hasOwn<T, K extends keyof T>(object: T, key: K): boolean;
|
|
159
214
|
|
|
160
|
-
export { type AnyFn, type Arrayable, type Awaitable, type MayBe, NOOP, type Nullable, type Prettify, cAF, capitalize, chunk, clamp, days, getObjectType,
|
|
215
|
+
export { type AnyFn, type Arrayable, type Awaitable, type MayBe, NOOP, type Nullable, type Prettify, type PrettifyV2, type PrimitiveType, type ThrottleDebounceOptions, cAF, capitalize, chunk, clamp, days, debounce, ensurePrefix, ensureSuffix, getObjectType, hasOwn, hours, isArray, isArrayEqual, isBoolean, isBrowser, isFunction, isInteger, isNativePromise, isNull, isNumber, isPromise, isString, isUndefined, join, minutes, noop, omit, once, pick, rAF, seconds, slash, throttle, toArray, unindent, unique, uniqueBy, waitFor, warnOnce, weeks };
|
package/dist/index.js
CHANGED
|
@@ -41,6 +41,19 @@ var noop = () => {
|
|
|
41
41
|
};
|
|
42
42
|
var NOOP = noop;
|
|
43
43
|
|
|
44
|
+
// src/fn/once.ts
|
|
45
|
+
function once(func) {
|
|
46
|
+
let called = false;
|
|
47
|
+
return function(...args) {
|
|
48
|
+
if (called) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
called = true;
|
|
52
|
+
func.apply(this, args);
|
|
53
|
+
return true;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
44
57
|
// src/env/isBrowser.ts
|
|
45
58
|
var isBrowser = () => typeof document !== "undefined";
|
|
46
59
|
|
|
@@ -178,6 +191,53 @@ function waitFor(ms) {
|
|
|
178
191
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
179
192
|
}
|
|
180
193
|
|
|
194
|
+
// src/misc/throttle.ts
|
|
195
|
+
function throttle(delay, callback, options = {}) {
|
|
196
|
+
const { isDebounce } = options;
|
|
197
|
+
let lastExec = 0;
|
|
198
|
+
let cancelled = false;
|
|
199
|
+
let timeoutId;
|
|
200
|
+
function clearExistingTimeout() {
|
|
201
|
+
if (timeoutId) {
|
|
202
|
+
clearTimeout(timeoutId);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
function cancel() {
|
|
206
|
+
clearExistingTimeout();
|
|
207
|
+
cancelled = true;
|
|
208
|
+
}
|
|
209
|
+
function wrapper(...args) {
|
|
210
|
+
if (cancelled) return;
|
|
211
|
+
const _this = this;
|
|
212
|
+
const now = Date.now();
|
|
213
|
+
const elapsed = now - lastExec;
|
|
214
|
+
function clear() {
|
|
215
|
+
timeoutId = void 0;
|
|
216
|
+
}
|
|
217
|
+
function exec(cur) {
|
|
218
|
+
lastExec = cur || Date.now();
|
|
219
|
+
callback.apply(_this, args);
|
|
220
|
+
}
|
|
221
|
+
if (isDebounce && !timeoutId) {
|
|
222
|
+
exec(now);
|
|
223
|
+
}
|
|
224
|
+
clearExistingTimeout();
|
|
225
|
+
if (!isDebounce && elapsed > delay) {
|
|
226
|
+
exec(now);
|
|
227
|
+
} else {
|
|
228
|
+
timeoutId = setTimeout(isDebounce ? clear : exec, isDebounce ? delay : delay - elapsed);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
wrapper.cancel = cancel;
|
|
232
|
+
return wrapper;
|
|
233
|
+
}
|
|
234
|
+
function debounce(delay, callback, options = {}) {
|
|
235
|
+
return throttle(delay, callback, {
|
|
236
|
+
...options,
|
|
237
|
+
isDebounce: true
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
|
|
181
241
|
// src/misc/warnOnce.ts
|
|
182
242
|
var warned = /* @__PURE__ */ new Set();
|
|
183
243
|
var warnOnce = (message) => {
|
|
@@ -217,6 +277,14 @@ function toArray(array) {
|
|
|
217
277
|
return Array.isArray(array) ? array : [array];
|
|
218
278
|
}
|
|
219
279
|
|
|
280
|
+
// src/array/isArrayEqual.ts
|
|
281
|
+
function isArrayEqual(array1, array2) {
|
|
282
|
+
if (array1.length !== array2.length) {
|
|
283
|
+
return false;
|
|
284
|
+
}
|
|
285
|
+
return array1.every((item, idx) => item === array2[idx]);
|
|
286
|
+
}
|
|
287
|
+
|
|
220
288
|
// src/string/join.ts
|
|
221
289
|
function join(array, options = {}) {
|
|
222
290
|
const { separator = "" } = options;
|
|
@@ -224,14 +292,52 @@ function join(array, options = {}) {
|
|
|
224
292
|
return array.filter((v) => Boolean(v) || v === 0).join(separator);
|
|
225
293
|
}
|
|
226
294
|
|
|
295
|
+
// src/string/slash.ts
|
|
296
|
+
function slash(input) {
|
|
297
|
+
return input.replace(/\\/g, "/");
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// src/string/unindent.ts
|
|
301
|
+
var _RE_FULL_WS = /^\s*$/;
|
|
302
|
+
function unindent(input) {
|
|
303
|
+
const lines = (typeof input === "string" ? input : input[0]).split("\n");
|
|
304
|
+
const whitespaceLines = lines.map((line) => _RE_FULL_WS.test(line));
|
|
305
|
+
const commonIndent = lines.reduce((min, line, idx) => {
|
|
306
|
+
if (!whitespaceLines[idx]) {
|
|
307
|
+
return min;
|
|
308
|
+
}
|
|
309
|
+
const indent = line.match(/^\s/)?.[0].length;
|
|
310
|
+
return indent === void 0 ? min : Math.min(min, indent);
|
|
311
|
+
}, Number.POSITIVE_INFINITY);
|
|
312
|
+
let emptylinesHead = 0;
|
|
313
|
+
while (emptylinesHead < lines.length && whitespaceLines[emptylinesHead]) {
|
|
314
|
+
emptylinesHead++;
|
|
315
|
+
}
|
|
316
|
+
let emptylinesTail = 0;
|
|
317
|
+
while (emptylinesTail < lines.length && whitespaceLines[lines.length - emptylinesTail - 1]) {
|
|
318
|
+
emptylinesTail++;
|
|
319
|
+
}
|
|
320
|
+
return lines.slice(emptylinesHead, lines.length - emptylinesTail).map((line) => line.slice(commonIndent)).join("\n");
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// src/string/ensurePrefix.ts
|
|
324
|
+
function ensurePrefix(input, prefix) {
|
|
325
|
+
return input.startsWith(prefix) ? input : `${prefix}${input}`;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// src/string/ensureSuffix.ts
|
|
329
|
+
function ensureSuffix(input, suffix) {
|
|
330
|
+
return input.endsWith(suffix) ? input : `${input}${suffix}`;
|
|
331
|
+
}
|
|
332
|
+
|
|
227
333
|
// src/object/omit.ts
|
|
228
334
|
function omit(object, ...keys) {
|
|
229
335
|
keys.forEach((key) => delete object[key]);
|
|
230
336
|
return object;
|
|
231
337
|
}
|
|
232
338
|
|
|
233
|
-
// src/object/
|
|
234
|
-
function
|
|
339
|
+
// src/object/hasOwn.ts
|
|
340
|
+
function hasOwn(object, key) {
|
|
235
341
|
return Object.prototype.hasOwnProperty.call(object, key);
|
|
236
342
|
}
|
|
237
343
|
|
|
@@ -240,7 +346,7 @@ function pick(object, keys) {
|
|
|
240
346
|
return Object.assign(
|
|
241
347
|
{},
|
|
242
348
|
...keys.map((key) => {
|
|
243
|
-
if (object &&
|
|
349
|
+
if (object && hasOwn(object, key)) {
|
|
244
350
|
return { [key]: object[key] };
|
|
245
351
|
}
|
|
246
352
|
})
|
|
@@ -254,11 +360,15 @@ export {
|
|
|
254
360
|
chunk,
|
|
255
361
|
clamp,
|
|
256
362
|
days,
|
|
363
|
+
debounce,
|
|
364
|
+
ensurePrefix,
|
|
365
|
+
ensureSuffix,
|
|
257
366
|
flatCase,
|
|
258
367
|
getObjectType,
|
|
259
|
-
|
|
368
|
+
hasOwn,
|
|
260
369
|
hours,
|
|
261
370
|
isArray,
|
|
371
|
+
isArrayEqual,
|
|
262
372
|
isBoolean,
|
|
263
373
|
isBrowser,
|
|
264
374
|
isFunction,
|
|
@@ -276,15 +386,19 @@ export {
|
|
|
276
386
|
minutes,
|
|
277
387
|
noop,
|
|
278
388
|
omit,
|
|
389
|
+
once,
|
|
279
390
|
pascalCase,
|
|
280
391
|
pick,
|
|
281
392
|
rAF,
|
|
282
393
|
seconds,
|
|
394
|
+
slash,
|
|
283
395
|
snakeCase,
|
|
284
396
|
splitByCase,
|
|
397
|
+
throttle,
|
|
285
398
|
titleCase,
|
|
286
399
|
toArray,
|
|
287
400
|
trainCase,
|
|
401
|
+
unindent,
|
|
288
402
|
unique,
|
|
289
403
|
uniqueBy,
|
|
290
404
|
upperFirst,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ntnyq/utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"description": "Common used utils.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"utils"
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"scule": "^1.3.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@ntnyq/eslint-config": "^3.0.0-beta.
|
|
43
|
+
"@ntnyq/eslint-config": "^3.0.0-beta.20",
|
|
44
44
|
"@ntnyq/prettier-config": "^1.21.3",
|
|
45
45
|
"@vitest/coverage-v8": "^2.1.2",
|
|
46
46
|
"bumpp": "^9.7.1",
|