@odinlin/utils 0.0.1 → 0.0.2
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/README.md +8 -3
- package/dist/index.cjs +172 -0
- package/dist/index.d.ts +104 -1
- package/dist/index.mjs +171 -1
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
# @odinlin/utils
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@odinlin/utils)
|
|
4
|
+
[](https://www.paka.dev/npm/@odinlin/utils)
|
|
5
|
+
|
|
3
6
|
Collection of common JavaScript / TypeScript utils
|
|
4
7
|
|
|
5
8
|
## Features
|
|
6
9
|
|
|
7
|
-
- [base](
|
|
10
|
+
- [base](src/base.ts)
|
|
11
|
+
|
|
12
|
+
- [random](src/random.ts)
|
|
8
13
|
|
|
9
|
-
- [
|
|
14
|
+
- [regexp](src/regexp.ts) depend on [any-rule](https://github.com/any86/any-rule)
|
|
10
15
|
|
|
11
|
-
- [
|
|
16
|
+
- [throttle-debounce](src/vendor.ts) depend on [throttle-debounce](https://www.npmjs.com/package/throttle-debounce)
|
|
12
17
|
|
|
13
18
|
## License
|
|
14
19
|
|
package/dist/index.cjs
CHANGED
|
@@ -138,8 +138,179 @@ const regWechatID = /^[a-zA-Z][-_a-zA-Z0-9]{5,19}$/;
|
|
|
138
138
|
const regPasswordV1 = /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_!@#$%^&*`~()-+=]+$)(?![a-z0-9]+$)(?![a-z\W_!@#$%^&*`~()-+=]+$)(?![0-9\W_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9\W_!@#$%^&*`~()-+=]/;
|
|
139
139
|
const regPasswordV2 = /^\S*(?=\S{6,})(?=\S*\d)(?=\S*[A-Z])(?=\S*[a-z])(?=\S*[!@#$%^&*? ])\S*$/;
|
|
140
140
|
|
|
141
|
+
/* eslint-disable no-undefined,no-param-reassign,no-shadow */
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Throttle execution of a function. Especially useful for rate limiting
|
|
145
|
+
* execution of handlers on events like resize and scroll.
|
|
146
|
+
*
|
|
147
|
+
* @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher)
|
|
148
|
+
* are most useful.
|
|
149
|
+
* @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through,
|
|
150
|
+
* as-is, to `callback` when the throttled-function is executed.
|
|
151
|
+
* @param {object} [options] - An object to configure options.
|
|
152
|
+
* @param {boolean} [options.noTrailing] - Optional, defaults to false. If noTrailing is true, callback will only execute every `delay` milliseconds
|
|
153
|
+
* while the throttled-function is being called. If noTrailing is false or unspecified, callback will be executed
|
|
154
|
+
* one final time after the last throttled-function call. (After the throttled-function has not been called for
|
|
155
|
+
* `delay` milliseconds, the internal counter is reset).
|
|
156
|
+
* @param {boolean} [options.noLeading] - Optional, defaults to false. If noLeading is false, the first throttled-function call will execute callback
|
|
157
|
+
* immediately. If noLeading is true, the first the callback execution will be skipped. It should be noted that
|
|
158
|
+
* callback will never executed if both noLeading = true and noTrailing = true.
|
|
159
|
+
* @param {boolean} [options.debounceMode] - If `debounceMode` is true (at begin), schedule `clear` to execute after `delay` ms. If `debounceMode` is
|
|
160
|
+
* false (at end), schedule `callback` to execute after `delay` ms.
|
|
161
|
+
*
|
|
162
|
+
* @returns {Function} A new, throttled, function.
|
|
163
|
+
*/
|
|
164
|
+
function throttle (delay, callback, options) {
|
|
165
|
+
var _ref = options || {},
|
|
166
|
+
_ref$noTrailing = _ref.noTrailing,
|
|
167
|
+
noTrailing = _ref$noTrailing === void 0 ? false : _ref$noTrailing,
|
|
168
|
+
_ref$noLeading = _ref.noLeading,
|
|
169
|
+
noLeading = _ref$noLeading === void 0 ? false : _ref$noLeading,
|
|
170
|
+
_ref$debounceMode = _ref.debounceMode,
|
|
171
|
+
debounceMode = _ref$debounceMode === void 0 ? undefined : _ref$debounceMode;
|
|
172
|
+
/*
|
|
173
|
+
* After wrapper has stopped being called, this timeout ensures that
|
|
174
|
+
* `callback` is executed at the proper times in `throttle` and `end`
|
|
175
|
+
* debounce modes.
|
|
176
|
+
*/
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
var timeoutID;
|
|
180
|
+
var cancelled = false; // Keep track of the last time `callback` was executed.
|
|
181
|
+
|
|
182
|
+
var lastExec = 0; // Function to clear existing timeout
|
|
183
|
+
|
|
184
|
+
function clearExistingTimeout() {
|
|
185
|
+
if (timeoutID) {
|
|
186
|
+
clearTimeout(timeoutID);
|
|
187
|
+
}
|
|
188
|
+
} // Function to cancel next exec
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
function cancel(options) {
|
|
192
|
+
var _ref2 = options || {},
|
|
193
|
+
_ref2$upcomingOnly = _ref2.upcomingOnly,
|
|
194
|
+
upcomingOnly = _ref2$upcomingOnly === void 0 ? false : _ref2$upcomingOnly;
|
|
195
|
+
|
|
196
|
+
clearExistingTimeout();
|
|
197
|
+
cancelled = !upcomingOnly;
|
|
198
|
+
}
|
|
199
|
+
/*
|
|
200
|
+
* The `wrapper` function encapsulates all of the throttling / debouncing
|
|
201
|
+
* functionality and when executed will limit the rate at which `callback`
|
|
202
|
+
* is executed.
|
|
203
|
+
*/
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
function wrapper() {
|
|
207
|
+
for (var _len = arguments.length, arguments_ = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
208
|
+
arguments_[_key] = arguments[_key];
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
var self = this;
|
|
212
|
+
var elapsed = Date.now() - lastExec;
|
|
213
|
+
|
|
214
|
+
if (cancelled) {
|
|
215
|
+
return;
|
|
216
|
+
} // Execute `callback` and update the `lastExec` timestamp.
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
function exec() {
|
|
220
|
+
lastExec = Date.now();
|
|
221
|
+
callback.apply(self, arguments_);
|
|
222
|
+
}
|
|
223
|
+
/*
|
|
224
|
+
* If `debounceMode` is true (at begin) this is used to clear the flag
|
|
225
|
+
* to allow future `callback` executions.
|
|
226
|
+
*/
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
function clear() {
|
|
230
|
+
timeoutID = undefined;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (!noLeading && debounceMode && !timeoutID) {
|
|
234
|
+
/*
|
|
235
|
+
* Since `wrapper` is being called for the first time and
|
|
236
|
+
* `debounceMode` is true (at begin), execute `callback`
|
|
237
|
+
* and noLeading != true.
|
|
238
|
+
*/
|
|
239
|
+
exec();
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
clearExistingTimeout();
|
|
243
|
+
|
|
244
|
+
if (debounceMode === undefined && elapsed > delay) {
|
|
245
|
+
if (noLeading) {
|
|
246
|
+
/*
|
|
247
|
+
* In throttle mode with noLeading, if `delay` time has
|
|
248
|
+
* been exceeded, update `lastExec` and schedule `callback`
|
|
249
|
+
* to execute after `delay` ms.
|
|
250
|
+
*/
|
|
251
|
+
lastExec = Date.now();
|
|
252
|
+
|
|
253
|
+
if (!noTrailing) {
|
|
254
|
+
timeoutID = setTimeout(debounceMode ? clear : exec, delay);
|
|
255
|
+
}
|
|
256
|
+
} else {
|
|
257
|
+
/*
|
|
258
|
+
* In throttle mode without noLeading, if `delay` time has been exceeded, execute
|
|
259
|
+
* `callback`.
|
|
260
|
+
*/
|
|
261
|
+
exec();
|
|
262
|
+
}
|
|
263
|
+
} else if (noTrailing !== true) {
|
|
264
|
+
/*
|
|
265
|
+
* In trailing throttle mode, since `delay` time has not been
|
|
266
|
+
* exceeded, schedule `callback` to execute `delay` ms after most
|
|
267
|
+
* recent execution.
|
|
268
|
+
*
|
|
269
|
+
* If `debounceMode` is true (at begin), schedule `clear` to execute
|
|
270
|
+
* after `delay` ms.
|
|
271
|
+
*
|
|
272
|
+
* If `debounceMode` is false (at end), schedule `callback` to
|
|
273
|
+
* execute after `delay` ms.
|
|
274
|
+
*/
|
|
275
|
+
timeoutID = setTimeout(debounceMode ? clear : exec, debounceMode === undefined ? delay - elapsed : delay);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
wrapper.cancel = cancel; // Return the wrapper function.
|
|
280
|
+
|
|
281
|
+
return wrapper;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/* eslint-disable no-undefined */
|
|
285
|
+
/**
|
|
286
|
+
* Debounce execution of a function. Debouncing, unlike throttling,
|
|
287
|
+
* guarantees that a function is only executed a single time, either at the
|
|
288
|
+
* very beginning of a series of calls, or at the very end.
|
|
289
|
+
*
|
|
290
|
+
* @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
|
|
291
|
+
* @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,
|
|
292
|
+
* to `callback` when the debounced-function is executed.
|
|
293
|
+
* @param {object} [options] - An object to configure options.
|
|
294
|
+
* @param {boolean} [options.atBegin] - Optional, defaults to false. If atBegin is false or unspecified, callback will only be executed `delay` milliseconds
|
|
295
|
+
* after the last debounced-function call. If atBegin is true, callback will be executed only at the first debounced-function call.
|
|
296
|
+
* (After the throttled-function has not been called for `delay` milliseconds, the internal counter is reset).
|
|
297
|
+
*
|
|
298
|
+
* @returns {Function} A new, debounced function.
|
|
299
|
+
*/
|
|
300
|
+
|
|
301
|
+
function debounce (delay, callback, options) {
|
|
302
|
+
var _ref = options || {},
|
|
303
|
+
_ref$atBegin = _ref.atBegin,
|
|
304
|
+
atBegin = _ref$atBegin === void 0 ? false : _ref$atBegin;
|
|
305
|
+
|
|
306
|
+
return throttle(delay, callback, {
|
|
307
|
+
debounceMode: atBegin !== false
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
|
|
141
311
|
exports.assert = assert;
|
|
142
312
|
exports.callString = callString;
|
|
313
|
+
exports.debounce = debounce;
|
|
143
314
|
exports.noop = noop;
|
|
144
315
|
exports.randomDate = randomDate;
|
|
145
316
|
exports.randomHexColor = randomHexColor;
|
|
@@ -229,3 +400,4 @@ exports.regWechatID = regWechatID;
|
|
|
229
400
|
exports.regWindowsFilePath = regWindowsFilePath;
|
|
230
401
|
exports.regWindowsFolderPath = regWindowsFolderPath;
|
|
231
402
|
exports.reghhmmss = reghhmmss;
|
|
403
|
+
exports.throttle = throttle;
|
package/dist/index.d.ts
CHANGED
|
@@ -390,4 +390,107 @@ declare const regPasswordV1: RegExp;
|
|
|
390
390
|
*/
|
|
391
391
|
declare const regPasswordV2: RegExp;
|
|
392
392
|
|
|
393
|
-
|
|
393
|
+
// Type definitions for throttle-debounce 5.0
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
interface CancelOptions {
|
|
397
|
+
upcomingOnly?: boolean;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
interface Cancel {
|
|
401
|
+
cancel: (options?: CancelOptions) => void;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
interface NoReturn<T extends (...args: any[]) => any> {
|
|
405
|
+
(...args: Parameters<T>): void;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
interface ThrottleOptions {
|
|
409
|
+
noTrailing?: boolean;
|
|
410
|
+
noLeading?: boolean;
|
|
411
|
+
debounceMode?: boolean;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
interface DebounceOptions {
|
|
415
|
+
atBegin?: boolean;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
type throttle<T extends (...args: any[]) => any> = NoReturn<T> & Cancel;
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Throttle execution of a function. Especially useful for rate limiting
|
|
422
|
+
* execution of handlers on events like resize and scroll.
|
|
423
|
+
*
|
|
424
|
+
* @param delay
|
|
425
|
+
* A zero-or-greater delay in milliseconds. For event callbacks, values around
|
|
426
|
+
* 100 or 250 (or even higher) are most useful.
|
|
427
|
+
*
|
|
428
|
+
* @param callback
|
|
429
|
+
* A function to be executed after delay milliseconds. The `this` context and
|
|
430
|
+
* all arguments are passed through, as-is, to `callback` when the
|
|
431
|
+
* throttled-function is executed.
|
|
432
|
+
*
|
|
433
|
+
* @param options
|
|
434
|
+
* An object to configure options.
|
|
435
|
+
*
|
|
436
|
+
* @param options.noTrailing
|
|
437
|
+
* Optional, defaults to false. If noTrailing is true, callback will only execute
|
|
438
|
+
* every `delay` milliseconds while the throttled-function is being called. If
|
|
439
|
+
* noTrailing is false or unspecified, callback will be executed one final time
|
|
440
|
+
* after the last throttled-function call. (After the throttled-function has not
|
|
441
|
+
* been called for `delay` milliseconds, the internal counter is reset)
|
|
442
|
+
*
|
|
443
|
+
* @param options.noLeading
|
|
444
|
+
* Optional, defaults to false. If noLeading is false, the first throttled-function
|
|
445
|
+
* call will execute callback immediately. If noLeading is true, the first the
|
|
446
|
+
* callback execution will be skipped. It should be noted that callback will never
|
|
447
|
+
* executed if both noLeading = true and noTrailing = true.
|
|
448
|
+
*
|
|
449
|
+
* @param options.debounceMode If `debounceMode` is true (at begin), schedule
|
|
450
|
+
* `callback` to execute after `delay` ms. If `debounceMode` is false (at end),
|
|
451
|
+
* schedule `callback` to execute after `delay` ms.
|
|
452
|
+
*
|
|
453
|
+
* @return
|
|
454
|
+
* A new, throttled, function.
|
|
455
|
+
*/
|
|
456
|
+
declare function throttle<T extends (...args: any[]) => any>(
|
|
457
|
+
delay: number,
|
|
458
|
+
callback: T,
|
|
459
|
+
options?: ThrottleOptions,
|
|
460
|
+
): throttle<T>;
|
|
461
|
+
type debounce<T extends (...args: any[]) => any> = NoReturn<T> & Cancel;
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Debounce execution of a function. Debouncing, unlike throttling,
|
|
465
|
+
* guarantees that a function is only executed a single time, either at the
|
|
466
|
+
* very beginning of a series of calls, or at the very end.
|
|
467
|
+
*
|
|
468
|
+
* @param delay
|
|
469
|
+
* A zero-or-greater delay in milliseconds. For event callbacks, values around
|
|
470
|
+
* 100 or 250 (or even higher) are most useful.
|
|
471
|
+
*
|
|
472
|
+
* @param callback
|
|
473
|
+
* A function to be executed after delay milliseconds. The `this` context and
|
|
474
|
+
* all arguments are passed through, as-is, to `callback` when the
|
|
475
|
+
* debounced-function is executed.
|
|
476
|
+
*
|
|
477
|
+
* @param options
|
|
478
|
+
* An object to configure options.
|
|
479
|
+
*
|
|
480
|
+
* @param options.atBegin
|
|
481
|
+
* If atBegin is false or unspecified, callback will only be executed `delay`
|
|
482
|
+
* milliseconds after the last debounced-function call. If atBegin is true,
|
|
483
|
+
* callback will be executed only at the first debounced-function call. (After
|
|
484
|
+
* the throttled-function has not been called for `delay` milliseconds, the
|
|
485
|
+
* internal counter is reset).
|
|
486
|
+
*
|
|
487
|
+
* @return
|
|
488
|
+
* A new, debounced function.
|
|
489
|
+
*/
|
|
490
|
+
declare function debounce<T extends (...args: any[]) => any>(
|
|
491
|
+
delay: number,
|
|
492
|
+
callback: T,
|
|
493
|
+
options?: DebounceOptions,
|
|
494
|
+
): debounce<T>;
|
|
495
|
+
|
|
496
|
+
export { assert, callString, debounce, noop, randomDate, randomHexColor, randomRange, randomRgbColor, randomUUID, regASCII, regAmount, regAmountEasy, regBankCardNo, regBase64, regChineseAndNumber, regChineseCharacterAndPoint, regChineseCharacters, regChineseName, regChineseProvince, regCreditCode, regCreditCodeEasy, regDate, regDateEasy, regDateMoment, regDecimal, regDomain, regEd2kEasy, regEmail, regEmailEasy, regEnglishName, regExamScore, regFloat, regFloatEasy, regHHmmss, regHexadecimalColor, regHtmlComments, regHtmlTags, regIMEI, regIPV4, regIPV6, regIdentityCard, regIdentityCardHK, regIdentityCardMO, regIdentityCardTWN, regIdentityCardV1, regIdentityCardV2, regImageUri, regInteger, regIntegerNegative, regIntegerPositive, regJAVAPackageName, regLetterAndNumber, regLetterOrNumber, regLetters, regLettersLower, regLettersUpper, regLicensePlateAll, regLicensePlateLegacy, regLicensePlateNE, regLinuxFilePath, regLinuxFolderPath, regLinuxHideFilePath, regMD5, regMacAddr, regMagnetEasy, regMobile, regMobileEasiest, regMobileEasy, regNotLetter, regOnlyNumber, regPassport, regPasswordV1, regPasswordV2, regPostalCode, regQQ, regRepeat, regSemVer, regStockCodeA, regSubnetMask, regTelephone, regThunderEasy, regTrainNumber, regUUID, regUri, regUriAndPort, regValidAccountV1, regValidAccountV2, regVideoUri, regWechatID, regWindowsFilePath, regWindowsFolderPath, reghhmmss, throttle };
|
package/dist/index.mjs
CHANGED
|
@@ -134,4 +134,174 @@ const regWechatID = /^[a-zA-Z][-_a-zA-Z0-9]{5,19}$/;
|
|
|
134
134
|
const regPasswordV1 = /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_!@#$%^&*`~()-+=]+$)(?![a-z0-9]+$)(?![a-z\W_!@#$%^&*`~()-+=]+$)(?![0-9\W_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9\W_!@#$%^&*`~()-+=]/;
|
|
135
135
|
const regPasswordV2 = /^\S*(?=\S{6,})(?=\S*\d)(?=\S*[A-Z])(?=\S*[a-z])(?=\S*[!@#$%^&*? ])\S*$/;
|
|
136
136
|
|
|
137
|
-
|
|
137
|
+
/* eslint-disable no-undefined,no-param-reassign,no-shadow */
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Throttle execution of a function. Especially useful for rate limiting
|
|
141
|
+
* execution of handlers on events like resize and scroll.
|
|
142
|
+
*
|
|
143
|
+
* @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher)
|
|
144
|
+
* are most useful.
|
|
145
|
+
* @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through,
|
|
146
|
+
* as-is, to `callback` when the throttled-function is executed.
|
|
147
|
+
* @param {object} [options] - An object to configure options.
|
|
148
|
+
* @param {boolean} [options.noTrailing] - Optional, defaults to false. If noTrailing is true, callback will only execute every `delay` milliseconds
|
|
149
|
+
* while the throttled-function is being called. If noTrailing is false or unspecified, callback will be executed
|
|
150
|
+
* one final time after the last throttled-function call. (After the throttled-function has not been called for
|
|
151
|
+
* `delay` milliseconds, the internal counter is reset).
|
|
152
|
+
* @param {boolean} [options.noLeading] - Optional, defaults to false. If noLeading is false, the first throttled-function call will execute callback
|
|
153
|
+
* immediately. If noLeading is true, the first the callback execution will be skipped. It should be noted that
|
|
154
|
+
* callback will never executed if both noLeading = true and noTrailing = true.
|
|
155
|
+
* @param {boolean} [options.debounceMode] - If `debounceMode` is true (at begin), schedule `clear` to execute after `delay` ms. If `debounceMode` is
|
|
156
|
+
* false (at end), schedule `callback` to execute after `delay` ms.
|
|
157
|
+
*
|
|
158
|
+
* @returns {Function} A new, throttled, function.
|
|
159
|
+
*/
|
|
160
|
+
function throttle (delay, callback, options) {
|
|
161
|
+
var _ref = options || {},
|
|
162
|
+
_ref$noTrailing = _ref.noTrailing,
|
|
163
|
+
noTrailing = _ref$noTrailing === void 0 ? false : _ref$noTrailing,
|
|
164
|
+
_ref$noLeading = _ref.noLeading,
|
|
165
|
+
noLeading = _ref$noLeading === void 0 ? false : _ref$noLeading,
|
|
166
|
+
_ref$debounceMode = _ref.debounceMode,
|
|
167
|
+
debounceMode = _ref$debounceMode === void 0 ? undefined : _ref$debounceMode;
|
|
168
|
+
/*
|
|
169
|
+
* After wrapper has stopped being called, this timeout ensures that
|
|
170
|
+
* `callback` is executed at the proper times in `throttle` and `end`
|
|
171
|
+
* debounce modes.
|
|
172
|
+
*/
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
var timeoutID;
|
|
176
|
+
var cancelled = false; // Keep track of the last time `callback` was executed.
|
|
177
|
+
|
|
178
|
+
var lastExec = 0; // Function to clear existing timeout
|
|
179
|
+
|
|
180
|
+
function clearExistingTimeout() {
|
|
181
|
+
if (timeoutID) {
|
|
182
|
+
clearTimeout(timeoutID);
|
|
183
|
+
}
|
|
184
|
+
} // Function to cancel next exec
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
function cancel(options) {
|
|
188
|
+
var _ref2 = options || {},
|
|
189
|
+
_ref2$upcomingOnly = _ref2.upcomingOnly,
|
|
190
|
+
upcomingOnly = _ref2$upcomingOnly === void 0 ? false : _ref2$upcomingOnly;
|
|
191
|
+
|
|
192
|
+
clearExistingTimeout();
|
|
193
|
+
cancelled = !upcomingOnly;
|
|
194
|
+
}
|
|
195
|
+
/*
|
|
196
|
+
* The `wrapper` function encapsulates all of the throttling / debouncing
|
|
197
|
+
* functionality and when executed will limit the rate at which `callback`
|
|
198
|
+
* is executed.
|
|
199
|
+
*/
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
function wrapper() {
|
|
203
|
+
for (var _len = arguments.length, arguments_ = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
204
|
+
arguments_[_key] = arguments[_key];
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
var self = this;
|
|
208
|
+
var elapsed = Date.now() - lastExec;
|
|
209
|
+
|
|
210
|
+
if (cancelled) {
|
|
211
|
+
return;
|
|
212
|
+
} // Execute `callback` and update the `lastExec` timestamp.
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
function exec() {
|
|
216
|
+
lastExec = Date.now();
|
|
217
|
+
callback.apply(self, arguments_);
|
|
218
|
+
}
|
|
219
|
+
/*
|
|
220
|
+
* If `debounceMode` is true (at begin) this is used to clear the flag
|
|
221
|
+
* to allow future `callback` executions.
|
|
222
|
+
*/
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
function clear() {
|
|
226
|
+
timeoutID = undefined;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (!noLeading && debounceMode && !timeoutID) {
|
|
230
|
+
/*
|
|
231
|
+
* Since `wrapper` is being called for the first time and
|
|
232
|
+
* `debounceMode` is true (at begin), execute `callback`
|
|
233
|
+
* and noLeading != true.
|
|
234
|
+
*/
|
|
235
|
+
exec();
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
clearExistingTimeout();
|
|
239
|
+
|
|
240
|
+
if (debounceMode === undefined && elapsed > delay) {
|
|
241
|
+
if (noLeading) {
|
|
242
|
+
/*
|
|
243
|
+
* In throttle mode with noLeading, if `delay` time has
|
|
244
|
+
* been exceeded, update `lastExec` and schedule `callback`
|
|
245
|
+
* to execute after `delay` ms.
|
|
246
|
+
*/
|
|
247
|
+
lastExec = Date.now();
|
|
248
|
+
|
|
249
|
+
if (!noTrailing) {
|
|
250
|
+
timeoutID = setTimeout(debounceMode ? clear : exec, delay);
|
|
251
|
+
}
|
|
252
|
+
} else {
|
|
253
|
+
/*
|
|
254
|
+
* In throttle mode without noLeading, if `delay` time has been exceeded, execute
|
|
255
|
+
* `callback`.
|
|
256
|
+
*/
|
|
257
|
+
exec();
|
|
258
|
+
}
|
|
259
|
+
} else if (noTrailing !== true) {
|
|
260
|
+
/*
|
|
261
|
+
* In trailing throttle mode, since `delay` time has not been
|
|
262
|
+
* exceeded, schedule `callback` to execute `delay` ms after most
|
|
263
|
+
* recent execution.
|
|
264
|
+
*
|
|
265
|
+
* If `debounceMode` is true (at begin), schedule `clear` to execute
|
|
266
|
+
* after `delay` ms.
|
|
267
|
+
*
|
|
268
|
+
* If `debounceMode` is false (at end), schedule `callback` to
|
|
269
|
+
* execute after `delay` ms.
|
|
270
|
+
*/
|
|
271
|
+
timeoutID = setTimeout(debounceMode ? clear : exec, debounceMode === undefined ? delay - elapsed : delay);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
wrapper.cancel = cancel; // Return the wrapper function.
|
|
276
|
+
|
|
277
|
+
return wrapper;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/* eslint-disable no-undefined */
|
|
281
|
+
/**
|
|
282
|
+
* Debounce execution of a function. Debouncing, unlike throttling,
|
|
283
|
+
* guarantees that a function is only executed a single time, either at the
|
|
284
|
+
* very beginning of a series of calls, or at the very end.
|
|
285
|
+
*
|
|
286
|
+
* @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
|
|
287
|
+
* @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,
|
|
288
|
+
* to `callback` when the debounced-function is executed.
|
|
289
|
+
* @param {object} [options] - An object to configure options.
|
|
290
|
+
* @param {boolean} [options.atBegin] - Optional, defaults to false. If atBegin is false or unspecified, callback will only be executed `delay` milliseconds
|
|
291
|
+
* after the last debounced-function call. If atBegin is true, callback will be executed only at the first debounced-function call.
|
|
292
|
+
* (After the throttled-function has not been called for `delay` milliseconds, the internal counter is reset).
|
|
293
|
+
*
|
|
294
|
+
* @returns {Function} A new, debounced function.
|
|
295
|
+
*/
|
|
296
|
+
|
|
297
|
+
function debounce (delay, callback, options) {
|
|
298
|
+
var _ref = options || {},
|
|
299
|
+
_ref$atBegin = _ref.atBegin,
|
|
300
|
+
atBegin = _ref$atBegin === void 0 ? false : _ref$atBegin;
|
|
301
|
+
|
|
302
|
+
return throttle(delay, callback, {
|
|
303
|
+
debounceMode: atBegin !== false
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export { assert, callString, debounce, noop, randomDate, randomHexColor, randomRange, randomRgbColor, randomUUID, regASCII, regAmount, regAmountEasy, regBankCardNo, regBase64, regChineseAndNumber, regChineseCharacterAndPoint, regChineseCharacters, regChineseName, regChineseProvince, regCreditCode, regCreditCodeEasy, regDate, regDateEasy, regDateMoment, regDecimal, regDomain, regEd2kEasy, regEmail, regEmailEasy, regEnglishName, regExamScore, regFloat, regFloatEasy, regHHmmss, regHexadecimalColor, regHtmlComments, regHtmlTags, regIMEI, regIPV4, regIPV6, regIdentityCard, regIdentityCardHK, regIdentityCardMO, regIdentityCardTWN, regIdentityCardV1, regIdentityCardV2, regImageUri, regInteger, regIntegerNegative, regIntegerPositive, regJAVAPackageName, regLetterAndNumber, regLetterOrNumber, regLetters, regLettersLower, regLettersUpper, regLicensePlateAll, regLicensePlateLegacy, regLicensePlateNE, regLinuxFilePath, regLinuxFolderPath, regLinuxHideFilePath, regMD5, regMacAddr, regMagnetEasy, regMobile, regMobileEasiest, regMobileEasy, regNotLetter, regOnlyNumber, regPassport, regPasswordV1, regPasswordV2, regPostalCode, regQQ, regRepeat, regSemVer, regStockCodeA, regSubnetMask, regTelephone, regThunderEasy, regTrainNumber, regUUID, regUri, regUriAndPort, regValidAccountV1, regValidAccountV2, regVideoUri, regWechatID, regWindowsFilePath, regWindowsFolderPath, reghhmmss, throttle };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@odinlin/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "Opinionated collection of common JavaScript / TypeScript utils by @odinlin",
|
|
5
5
|
"author": "odinlin <topazur@163.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -42,6 +42,9 @@
|
|
|
42
42
|
"test": "vitest",
|
|
43
43
|
"lint": "eslint --fix --ext .ts ."
|
|
44
44
|
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"throttle-debounce": "^5.0.0"
|
|
47
|
+
},
|
|
45
48
|
"devDependencies": {
|
|
46
49
|
"@antfu/eslint-config": "^0.27.0",
|
|
47
50
|
"@rollup/plugin-alias": "^3.1.9",
|
|
@@ -49,6 +52,7 @@
|
|
|
49
52
|
"@rollup/plugin-json": "^4.1.0",
|
|
50
53
|
"@rollup/plugin-node-resolve": "^14.1.0",
|
|
51
54
|
"@types/node": "^18.7.18",
|
|
55
|
+
"@types/throttle-debounce": "^5.0.0",
|
|
52
56
|
"bumpp": "^8.2.1",
|
|
53
57
|
"eslint": "^8.23.1",
|
|
54
58
|
"esno": "^0.16.3",
|