@sybz-components/utils 0.0.11 → 0.0.12

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/base.cjs CHANGED
@@ -3,7 +3,7 @@
3
3
  require('vue');
4
4
  require('consola');
5
5
  require('es-toolkit');
6
- const format = require('./shared/utils.D_mBxLLy.cjs');
6
+ const format = require('./shared/utils.DUIw9c4e.cjs');
7
7
  require('element-plus');
8
8
  require('./is.cjs');
9
9
 
package/dist/base.d.cts CHANGED
@@ -455,19 +455,47 @@ declare function toLine(text: string, connect?: string): string;
455
455
  */
456
456
  declare function processWidth(initValue: WidthInput, isBase: true): string;
457
457
  declare function processWidth(initValue: WidthInput, isBase?: false): WidthStyleResult | {};
458
+ interface ThrottleOptions {
459
+ /**
460
+ * 是否在首次调用时立即执行,默认 `true`。
461
+ */
462
+ leading?: boolean;
463
+ /**
464
+ * 是否在节流周期结束时执行最后一次调用,默认 `true`。
465
+ */
466
+ trailing?: boolean;
467
+ }
468
+ type ThrottleOptionsInput = boolean | ThrottleOptions;
469
+ type ThrottledFunction<T extends Func> = ((...args: Parameters<T>) => ReturnType<T> | undefined) & {
470
+ cancel: () => void;
471
+ flush: () => ReturnType<T> | undefined;
472
+ };
458
473
  /**
459
474
  * 创建节流函数。
460
475
  *
461
476
  * @param fn 需要节流执行的函数。
462
477
  * @param delay 节流间隔,单位毫秒,默认 `1000`。
463
- * @returns 节流后的函数。
478
+ * @param options 节流选项。传 `false` 等同于 `{ leading: false }`。
479
+ * @param resultCallback 每次真正执行后触发的结果回调。
480
+ * @returns 带 `cancel()` / `flush()` 方法的节流函数。
464
481
  *
465
482
  * @example
466
483
  * const onResize = throttle(() => {
467
484
  * console.log('resize')
468
485
  * }, 300)
486
+ *
487
+ * @example
488
+ * const save = throttle(saveDraft, 1000, { leading: false, trailing: true })
489
+ *
490
+ * @example
491
+ * const track = throttle(trackPosition, 200, { trailing: false }, (result) => {
492
+ * console.log(result)
493
+ * })
494
+ *
495
+ * track.cancel()
496
+ * track.flush()
469
497
  */
470
- declare function throttle<T extends Func>(fn: T, delay?: number): (...args: Parameters<T>) => void;
498
+ declare function throttle<T extends Func>(fn: T, delay?: number, options?: ThrottleOptionsInput, resultCallback?: (result: ReturnType<T>) => void): ThrottledFunction<T>;
471
499
  /**
472
500
  * 统一处理 Promise 或任务函数执行结果。
473
501
  *
@@ -513,6 +541,13 @@ type DebouncedFunction<T extends Func> = ((...args: Parameters<T>) => Promise<Aw
513
541
  * }, 300)
514
542
  *
515
543
  * await search('sybz')
544
+ *
545
+ * @example
546
+ * const submit = debounce(saveForm, 500, true, (result) => {
547
+ * console.log(result)
548
+ * })
549
+ *
550
+ * submit.cancel()
516
551
  */
517
552
  declare function debounce<T extends Func>(func: T, delay?: number, immediate?: boolean, resultCallback?: (result: ReturnType<T>) => void): DebouncedFunction<T>;
518
553
  /**
package/dist/base.d.mts CHANGED
@@ -455,19 +455,47 @@ declare function toLine(text: string, connect?: string): string;
455
455
  */
456
456
  declare function processWidth(initValue: WidthInput, isBase: true): string;
457
457
  declare function processWidth(initValue: WidthInput, isBase?: false): WidthStyleResult | {};
458
+ interface ThrottleOptions {
459
+ /**
460
+ * 是否在首次调用时立即执行,默认 `true`。
461
+ */
462
+ leading?: boolean;
463
+ /**
464
+ * 是否在节流周期结束时执行最后一次调用,默认 `true`。
465
+ */
466
+ trailing?: boolean;
467
+ }
468
+ type ThrottleOptionsInput = boolean | ThrottleOptions;
469
+ type ThrottledFunction<T extends Func> = ((...args: Parameters<T>) => ReturnType<T> | undefined) & {
470
+ cancel: () => void;
471
+ flush: () => ReturnType<T> | undefined;
472
+ };
458
473
  /**
459
474
  * 创建节流函数。
460
475
  *
461
476
  * @param fn 需要节流执行的函数。
462
477
  * @param delay 节流间隔,单位毫秒,默认 `1000`。
463
- * @returns 节流后的函数。
478
+ * @param options 节流选项。传 `false` 等同于 `{ leading: false }`。
479
+ * @param resultCallback 每次真正执行后触发的结果回调。
480
+ * @returns 带 `cancel()` / `flush()` 方法的节流函数。
464
481
  *
465
482
  * @example
466
483
  * const onResize = throttle(() => {
467
484
  * console.log('resize')
468
485
  * }, 300)
486
+ *
487
+ * @example
488
+ * const save = throttle(saveDraft, 1000, { leading: false, trailing: true })
489
+ *
490
+ * @example
491
+ * const track = throttle(trackPosition, 200, { trailing: false }, (result) => {
492
+ * console.log(result)
493
+ * })
494
+ *
495
+ * track.cancel()
496
+ * track.flush()
469
497
  */
470
- declare function throttle<T extends Func>(fn: T, delay?: number): (...args: Parameters<T>) => void;
498
+ declare function throttle<T extends Func>(fn: T, delay?: number, options?: ThrottleOptionsInput, resultCallback?: (result: ReturnType<T>) => void): ThrottledFunction<T>;
471
499
  /**
472
500
  * 统一处理 Promise 或任务函数执行结果。
473
501
  *
@@ -513,6 +541,13 @@ type DebouncedFunction<T extends Func> = ((...args: Parameters<T>) => Promise<Aw
513
541
  * }, 300)
514
542
  *
515
543
  * await search('sybz')
544
+ *
545
+ * @example
546
+ * const submit = debounce(saveForm, 500, true, (result) => {
547
+ * console.log(result)
548
+ * })
549
+ *
550
+ * submit.cancel()
516
551
  */
517
552
  declare function debounce<T extends Func>(func: T, delay?: number, immediate?: boolean, resultCallback?: (result: ReturnType<T>) => void): DebouncedFunction<T>;
518
553
  /**
package/dist/base.d.ts CHANGED
@@ -455,19 +455,47 @@ declare function toLine(text: string, connect?: string): string;
455
455
  */
456
456
  declare function processWidth(initValue: WidthInput, isBase: true): string;
457
457
  declare function processWidth(initValue: WidthInput, isBase?: false): WidthStyleResult | {};
458
+ interface ThrottleOptions {
459
+ /**
460
+ * 是否在首次调用时立即执行,默认 `true`。
461
+ */
462
+ leading?: boolean;
463
+ /**
464
+ * 是否在节流周期结束时执行最后一次调用,默认 `true`。
465
+ */
466
+ trailing?: boolean;
467
+ }
468
+ type ThrottleOptionsInput = boolean | ThrottleOptions;
469
+ type ThrottledFunction<T extends Func> = ((...args: Parameters<T>) => ReturnType<T> | undefined) & {
470
+ cancel: () => void;
471
+ flush: () => ReturnType<T> | undefined;
472
+ };
458
473
  /**
459
474
  * 创建节流函数。
460
475
  *
461
476
  * @param fn 需要节流执行的函数。
462
477
  * @param delay 节流间隔,单位毫秒,默认 `1000`。
463
- * @returns 节流后的函数。
478
+ * @param options 节流选项。传 `false` 等同于 `{ leading: false }`。
479
+ * @param resultCallback 每次真正执行后触发的结果回调。
480
+ * @returns 带 `cancel()` / `flush()` 方法的节流函数。
464
481
  *
465
482
  * @example
466
483
  * const onResize = throttle(() => {
467
484
  * console.log('resize')
468
485
  * }, 300)
486
+ *
487
+ * @example
488
+ * const save = throttle(saveDraft, 1000, { leading: false, trailing: true })
489
+ *
490
+ * @example
491
+ * const track = throttle(trackPosition, 200, { trailing: false }, (result) => {
492
+ * console.log(result)
493
+ * })
494
+ *
495
+ * track.cancel()
496
+ * track.flush()
469
497
  */
470
- declare function throttle<T extends Func>(fn: T, delay?: number): (...args: Parameters<T>) => void;
498
+ declare function throttle<T extends Func>(fn: T, delay?: number, options?: ThrottleOptionsInput, resultCallback?: (result: ReturnType<T>) => void): ThrottledFunction<T>;
471
499
  /**
472
500
  * 统一处理 Promise 或任务函数执行结果。
473
501
  *
@@ -513,6 +541,13 @@ type DebouncedFunction<T extends Func> = ((...args: Parameters<T>) => Promise<Aw
513
541
  * }, 300)
514
542
  *
515
543
  * await search('sybz')
544
+ *
545
+ * @example
546
+ * const submit = debounce(saveForm, 500, true, (result) => {
547
+ * console.log(result)
548
+ * })
549
+ *
550
+ * submit.cancel()
516
551
  */
517
552
  declare function debounce<T extends Func>(func: T, delay?: number, immediate?: boolean, resultCallback?: (result: ReturnType<T>) => void): DebouncedFunction<T>;
518
553
  /**
package/dist/base.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import 'vue';
2
2
  import 'consola';
3
3
  import 'es-toolkit';
4
- export { $ as $toast, c as clearStorage, a as clone, b as confirm, d as copy, e as debounce, f as delay, g as getStorage, h as getType, i as getUtilsBuildTime, j as getVariable, k as isEmpty, l as log, m as merge, n as mockValue, p as processWidth, r as random, s as setStorage, t as test, o as throttle, q as toLine, u as tryCatch, v as validate, w as validateForm, x as validateOnSubmit } from './shared/utils.CCqAduBt.mjs';
4
+ export { $ as $toast, c as clearStorage, a as clone, b as confirm, d as copy, e as debounce, f as delay, g as getStorage, h as getType, i as getUtilsBuildTime, j as getVariable, k as isEmpty, l as log, m as merge, n as mockValue, p as processWidth, r as random, s as setStorage, t as test, o as throttle, q as toLine, u as tryCatch, v as validate, w as validateForm, x as validateOnSubmit } from './shared/utils.CTnx12Vx.mjs';
5
5
  import 'element-plus';
6
6
  import './is.mjs';
package/dist/format.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const format = require('./shared/utils.D_mBxLLy.cjs');
3
+ const format = require('./shared/utils.DUIw9c4e.cjs');
4
4
  require('./is.cjs');
5
5
  require('consola');
6
6
  require('vue');
package/dist/format.mjs CHANGED
@@ -1,4 +1,4 @@
1
- export { y as formatBytes, z as formatBytesConvert, A as formatDurationTime, B as formatImg, C as formatTextToHtml, D as formatThousands, E as formatTime, F as formatToFixed } from './shared/utils.CCqAduBt.mjs';
1
+ export { y as formatBytes, z as formatBytesConvert, A as formatDurationTime, B as formatImg, C as formatTextToHtml, D as formatThousands, E as formatTime, F as formatToFixed } from './shared/utils.CTnx12Vx.mjs';
2
2
  import './is.mjs';
3
3
  import 'consola';
4
4
  import 'vue';
package/dist/index.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const format = require('./shared/utils.D_mBxLLy.cjs');
3
+ const format = require('./shared/utils.DUIw9c4e.cjs');
4
4
  const day = require('./day.cjs');
5
5
  const is = require('./is.cjs');
6
6
  const ws = require('./ws.cjs');
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- export { $ as $toast, c as clearStorage, a as clone, b as confirm, d as copy, e as debounce, f as delay, y as formatBytes, z as formatBytesConvert, A as formatDurationTime, B as formatImg, C as formatTextToHtml, D as formatThousands, E as formatTime, F as formatToFixed, g as getStorage, h as getType, i as getUtilsBuildTime, j as getVariable, k as isEmpty, l as log, m as merge, n as mockValue, p as processWidth, r as random, s as setStorage, t as test, o as throttle, q as toLine, u as tryCatch, v as validate, w as validateForm, x as validateOnSubmit } from './shared/utils.CCqAduBt.mjs';
1
+ export { $ as $toast, c as clearStorage, a as clone, b as confirm, d as copy, e as debounce, f as delay, y as formatBytes, z as formatBytesConvert, A as formatDurationTime, B as formatImg, C as formatTextToHtml, D as formatThousands, E as formatTime, F as formatToFixed, g as getStorage, h as getType, i as getUtilsBuildTime, j as getVariable, k as isEmpty, l as log, m as merge, n as mockValue, p as processWidth, r as random, s as setStorage, t as test, o as throttle, q as toLine, u as tryCatch, v as validate, w as validateForm, x as validateOnSubmit } from './shared/utils.CTnx12Vx.mjs';
2
2
  export { diffDate, diffDateFromCurrent, formatDate, formatDateToDay, formatDateToMinute } from './day.mjs';
3
3
  export { isArray, isBoolean, isComponent, isDate, isEmptyObject, isFunction, isIOS, isMap, isNumber, isObject, isPlainObject, isPromise, isRegExp, isSVGElement, isSet, isString, isStringNumber, isSymbol, isUrl, objectToString, toRawType, toTypeString } from './is.mjs';
4
4
  export { WS } from './ws.mjs';
@@ -814,23 +814,79 @@ function processWidth(initValue, isBase = false) {
814
814
  }
815
815
  return isBase ? res : { width: res };
816
816
  }
817
- function throttle(fn, delay2 = 1e3) {
818
- let last = 0;
817
+ const normalizeThrottleOptions = (options = {}) => {
818
+ const normalized = typeof options === "boolean" ? { leading: options } : options;
819
+ return {
820
+ leading: normalized.leading !== false,
821
+ trailing: normalized.trailing !== false
822
+ };
823
+ };
824
+ function throttle(fn, delay2 = 1e3, options = {}, resultCallback) {
825
+ const { leading, trailing } = normalizeThrottleOptions(options);
819
826
  let timer = void 0;
820
- return function(...args) {
821
- let context = this;
822
- let now = +/* @__PURE__ */ new Date();
823
- if (now - last < delay2) {
827
+ let previous;
828
+ let lastArgs;
829
+ let lastThis;
830
+ let result;
831
+ const cancel = () => {
832
+ if (timer) {
824
833
  clearTimeout(timer);
825
- timer = setTimeout(function() {
826
- last = now;
827
- fn.apply(context, args);
828
- }, delay2);
829
- } else {
830
- last = now;
831
- fn.apply(context, args);
834
+ timer = void 0;
835
+ }
836
+ previous = void 0;
837
+ lastArgs = void 0;
838
+ lastThis = void 0;
839
+ };
840
+ const invoke = () => {
841
+ if (!lastArgs) return result;
842
+ previous = Date.now();
843
+ const args = lastArgs;
844
+ const context = lastThis;
845
+ lastArgs = void 0;
846
+ lastThis = void 0;
847
+ result = fn.apply(context, args);
848
+ resultCallback?.(result);
849
+ return result;
850
+ };
851
+ const flush = () => {
852
+ if (timer) {
853
+ clearTimeout(timer);
854
+ timer = void 0;
832
855
  }
856
+ return invoke();
857
+ };
858
+ const throttled = function(...args) {
859
+ const now = Date.now();
860
+ lastArgs = args;
861
+ lastThis = this;
862
+ if (previous === void 0) {
863
+ if (leading) {
864
+ return invoke();
865
+ }
866
+ previous = now;
867
+ }
868
+ const remaining = delay2 - (now - previous);
869
+ if (remaining <= 0 || remaining > delay2) {
870
+ if (timer) {
871
+ clearTimeout(timer);
872
+ timer = void 0;
873
+ }
874
+ return invoke();
875
+ }
876
+ if (!timer && trailing) {
877
+ timer = setTimeout(() => {
878
+ timer = void 0;
879
+ if (!leading) {
880
+ previous = void 0;
881
+ }
882
+ invoke();
883
+ }, remaining);
884
+ }
885
+ return result;
833
886
  };
887
+ throttled.cancel = cancel;
888
+ throttled.flush = flush;
889
+ return throttled;
834
890
  }
835
891
  async function tryCatch(task, sendLoading) {
836
892
  const updateLoading = (value) => {
@@ -978,7 +1034,7 @@ function getVariable(propertyName, fallback = "") {
978
1034
  const DEFAULT_BUILD_TIME_FALLBACK = "\u672A\u6CE8\u5165";
979
1035
  function getUtilsBuildTime(fallback = DEFAULT_BUILD_TIME_FALLBACK) {
980
1036
  {
981
- return "2026-07-02 11:57:59";
1037
+ return "2026-07-02 16:09:23";
982
1038
  }
983
1039
  }
984
1040
  function test() {
@@ -190,7 +190,7 @@ function formatImg(photoName, addPath = "", { basePath = "assets/images" } = {})
190
190
  const addLastSlash = addPath.endsWith("/") || !addPath ? addPath : `${addPath}/`;
191
191
  const addLastBasePathSlash = basePath.endsWith("/") || !basePath ? basePath : `${basePath}/`;
192
192
  const mergeSrc = `${addLastSlash}${photoName}`;
193
- return new URL(`../${addLastBasePathSlash}${mergeSrc}`, (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('shared/utils.D_mBxLLy.cjs', document.baseURI).href))).href;
193
+ return new URL(`../${addLastBasePathSlash}${mergeSrc}`, (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('shared/utils.DUIw9c4e.cjs', document.baseURI).href))).href;
194
194
  }
195
195
  function formatToFixed(value, options) {
196
196
  if (typeof options === "number") {
@@ -817,23 +817,79 @@ function processWidth(initValue, isBase = false) {
817
817
  }
818
818
  return isBase ? res : { width: res };
819
819
  }
820
- function throttle(fn, delay2 = 1e3) {
821
- let last = 0;
820
+ const normalizeThrottleOptions = (options = {}) => {
821
+ const normalized = typeof options === "boolean" ? { leading: options } : options;
822
+ return {
823
+ leading: normalized.leading !== false,
824
+ trailing: normalized.trailing !== false
825
+ };
826
+ };
827
+ function throttle(fn, delay2 = 1e3, options = {}, resultCallback) {
828
+ const { leading, trailing } = normalizeThrottleOptions(options);
822
829
  let timer = void 0;
823
- return function(...args) {
824
- let context = this;
825
- let now = +/* @__PURE__ */ new Date();
826
- if (now - last < delay2) {
830
+ let previous;
831
+ let lastArgs;
832
+ let lastThis;
833
+ let result;
834
+ const cancel = () => {
835
+ if (timer) {
827
836
  clearTimeout(timer);
828
- timer = setTimeout(function() {
829
- last = now;
830
- fn.apply(context, args);
831
- }, delay2);
832
- } else {
833
- last = now;
834
- fn.apply(context, args);
837
+ timer = void 0;
838
+ }
839
+ previous = void 0;
840
+ lastArgs = void 0;
841
+ lastThis = void 0;
842
+ };
843
+ const invoke = () => {
844
+ if (!lastArgs) return result;
845
+ previous = Date.now();
846
+ const args = lastArgs;
847
+ const context = lastThis;
848
+ lastArgs = void 0;
849
+ lastThis = void 0;
850
+ result = fn.apply(context, args);
851
+ resultCallback?.(result);
852
+ return result;
853
+ };
854
+ const flush = () => {
855
+ if (timer) {
856
+ clearTimeout(timer);
857
+ timer = void 0;
835
858
  }
859
+ return invoke();
860
+ };
861
+ const throttled = function(...args) {
862
+ const now = Date.now();
863
+ lastArgs = args;
864
+ lastThis = this;
865
+ if (previous === void 0) {
866
+ if (leading) {
867
+ return invoke();
868
+ }
869
+ previous = now;
870
+ }
871
+ const remaining = delay2 - (now - previous);
872
+ if (remaining <= 0 || remaining > delay2) {
873
+ if (timer) {
874
+ clearTimeout(timer);
875
+ timer = void 0;
876
+ }
877
+ return invoke();
878
+ }
879
+ if (!timer && trailing) {
880
+ timer = setTimeout(() => {
881
+ timer = void 0;
882
+ if (!leading) {
883
+ previous = void 0;
884
+ }
885
+ invoke();
886
+ }, remaining);
887
+ }
888
+ return result;
836
889
  };
890
+ throttled.cancel = cancel;
891
+ throttled.flush = flush;
892
+ return throttled;
837
893
  }
838
894
  async function tryCatch(task, sendLoading) {
839
895
  const updateLoading = (value) => {
@@ -981,7 +1037,7 @@ function getVariable(propertyName, fallback = "") {
981
1037
  const DEFAULT_BUILD_TIME_FALLBACK = "\u672A\u6CE8\u5165";
982
1038
  function getUtilsBuildTime(fallback = DEFAULT_BUILD_TIME_FALLBACK) {
983
1039
  {
984
- return "2026-07-02 11:57:59";
1040
+ return "2026-07-02 16:09:23";
985
1041
  }
986
1042
  }
987
1043
  function test() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sybz-components/utils",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "description": "utils of sybz-components",
5
5
  "license": "MIT",
6
6
  "type": "commonjs",