@nickyzj2023/utils 1.0.41 → 1.0.42

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.
Files changed (41) hide show
  1. package/biome.json +34 -34
  2. package/dist/index.js +1 -1
  3. package/dist/time.d.ts +40 -0
  4. package/docs/assets/navigation.js +1 -1
  5. package/docs/assets/search.js +1 -1
  6. package/docs/functions/camelToSnake.html +1 -1
  7. package/docs/functions/capitalize.html +1 -1
  8. package/docs/functions/compactStr.html +1 -1
  9. package/docs/functions/debounce.html +187 -0
  10. package/docs/functions/decapitalize.html +1 -1
  11. package/docs/functions/fetcher.html +1 -1
  12. package/docs/functions/imageUrlToBase64.html +1 -1
  13. package/docs/functions/isFalsy.html +1 -1
  14. package/docs/functions/isNil.html +1 -1
  15. package/docs/functions/isObject.html +1 -1
  16. package/docs/functions/isPrimitive.html +1 -1
  17. package/docs/functions/isTruthy.html +1 -1
  18. package/docs/functions/loopUntil.html +1 -1
  19. package/docs/functions/mapKeys.html +1 -1
  20. package/docs/functions/mapValues.html +1 -1
  21. package/docs/functions/mergeObjects.html +1 -1
  22. package/docs/functions/randomInt.html +1 -1
  23. package/docs/functions/sleep.html +1 -1
  24. package/docs/functions/snakeToCamel.html +1 -1
  25. package/docs/functions/throttle.html +187 -0
  26. package/docs/functions/timeLog.html +1 -1
  27. package/docs/functions/to.html +1 -1
  28. package/docs/functions/withCache.html +3 -3
  29. package/docs/modules.html +1 -1
  30. package/docs/types/CamelToSnake.html +1 -1
  31. package/docs/types/Capitalize.html +1 -1
  32. package/docs/types/Decapitalize.html +1 -1
  33. package/docs/types/DeepMapKeys.html +1 -1
  34. package/docs/types/DeepMapValues.html +1 -1
  35. package/docs/types/Falsy.html +1 -1
  36. package/docs/types/Primitive.html +1 -1
  37. package/docs/types/RequestInit.html +1 -1
  38. package/docs/types/SetTtl.html +1 -1
  39. package/docs/types/SnakeToCamel.html +1 -1
  40. package/package.json +24 -24
  41. package/src/time.ts +70 -0
package/package.json CHANGED
@@ -1,26 +1,26 @@
1
1
  {
2
- "name": "@nickyzj2023/utils",
3
- "version": "1.0.41",
4
- "type": "module",
5
- "module": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "scripts": {
8
- "docs": "typedoc src/index.ts --plugin typedoc-material-theme",
9
- "build": "bun build --target=bun --outdir ./dist --minify ./src/index.ts && tsc && bun run docs"
10
- },
11
- "devDependencies": {
12
- "@biomejs/biome": "^2.3.13",
13
- "@types/bun": "^1.3.8",
14
- "typedoc": "^0.28.16"
15
- },
16
- "peerDependencies": {
17
- "typescript": "^5.9.3"
18
- },
19
- "repository": {
20
- "type": "git",
21
- "url": "https://github.com/Nickyzj628/utils.git"
22
- },
23
- "dependencies": {
24
- "typedoc-material-theme": "^1.4.1"
25
- }
2
+ "name": "@nickyzj2023/utils",
3
+ "version": "1.0.42",
4
+ "type": "module",
5
+ "module": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "docs": "typedoc src/index.ts --plugin typedoc-material-theme",
9
+ "build": "bun build --target=bun --outdir ./dist --minify ./src/index.ts && tsc && bun run docs"
10
+ },
11
+ "devDependencies": {
12
+ "@biomejs/biome": "^2.3.14",
13
+ "@types/bun": "^1.3.8",
14
+ "typedoc": "^0.28.16"
15
+ },
16
+ "peerDependencies": {
17
+ "typescript": "^5.9.3"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "https://github.com/Nickyzj628/utils.git"
22
+ },
23
+ "dependencies": {
24
+ "typedoc-material-theme": "^1.4.1"
25
+ }
26
26
  }
package/src/time.ts CHANGED
@@ -9,3 +9,73 @@ export const sleep = async (time = 150) => {
9
9
  setTimeout(resolve, time);
10
10
  });
11
11
  };
12
+
13
+ /**
14
+ * 防抖:在指定时间内只执行最后一次调用
15
+ * @param fn 要防抖的函数
16
+ * @param delay 延迟时间,默认 300ms
17
+ *
18
+ * @remarks
19
+ * 连续触发时,只有最后一次会执行。适合用于搜索框输入、窗口大小调整等场景。
20
+ * 例如:用户输入"hello"过程中,不会触发搜索,只有停下来时才执行。
21
+ *
22
+ * 防抖 vs 节流:
23
+ * - 防抖:等待触发停止后才执行(最后一次)
24
+ * - 节流:按固定节奏执行(每隔多久执行一次)
25
+ *
26
+ * @example
27
+ * const search = debounce((keyword: string) => {
28
+ * console.log('搜索:', keyword);
29
+ * });
30
+ * search('hello'); // 300ms 后执行
31
+ */
32
+ export const debounce = <T extends (...args: any[]) => any>(
33
+ fn: T,
34
+ delay = 300,
35
+ ) => {
36
+ let timer: ReturnType<typeof setTimeout> | null = null;
37
+
38
+ return (...args: Parameters<T>) => {
39
+ if (timer) {
40
+ clearTimeout(timer);
41
+ }
42
+ timer = setTimeout(() => {
43
+ fn(...args);
44
+ }, delay);
45
+ };
46
+ };
47
+
48
+ /**
49
+ * 节流函数 - 在指定时间间隔内最多执行一次调用
50
+ * @param fn 要节流的函数
51
+ * @param delay 间隔时间,默认 300ms
52
+ *
53
+ * @remarks
54
+ * 节流:连续触发时,按照固定间隔执行。适合用于滚动、拖拽等高频触发场景。
55
+ * 例如:滚动页面时,每300ms最多执行一次回调,而不是每次滚动都执行。
56
+ *
57
+ * 防抖 vs 节流:
58
+ * - 防抖:等待触发停止后才执行(最后一次)
59
+ * - 节流:按固定节奏执行(每隔多久执行一次)
60
+ *
61
+ * @example
62
+ * const handleScroll = throttle(() => {
63
+ * console.log('滚动位置:', window.scrollY);
64
+ * }, 200);
65
+ * window.addEventListener('scroll', handleScroll);
66
+ */
67
+ export const throttle = <T extends (...args: any[]) => any>(
68
+ fn: T,
69
+ delay = 300,
70
+ ) => {
71
+ let timer: ReturnType<typeof setTimeout> | null = null;
72
+
73
+ return function (this: any, ...args: Parameters<T>) {
74
+ if (!timer) {
75
+ timer = setTimeout(() => {
76
+ timer = null;
77
+ fn.apply(this, args);
78
+ }, delay);
79
+ }
80
+ };
81
+ };