kontroll 0.1.2 → 0.1.4

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 CHANGED
@@ -11,6 +11,7 @@
11
11
  ## Features
12
12
  - **100% coverage!**
13
13
  - **Self-explained**: for real, every functions and options have TSDoc comments to explain their behavior plus examples at a **hover** *(based on your IDE)*, apart from the already intuitive logic path.
14
+ - [![jsDocs.io][jsDocs-src]][jsDocs-href]
14
15
  - **Clearable**: you can stop pending timers by calling the returned clearer function or use the `clear(key)` function.
15
16
  - **Promise aware**: avoid duplicated call if your promise haven't settled.
16
17
 
@@ -79,13 +80,15 @@ In you wish them to have the same timer, you can manually set `options.key` like
79
80
 
80
81
  <!-- Badges -->
81
82
 
82
- [npm-version-src]: https://img.shields.io/npm/v/kontroll?style=flat&colorA=18181B&colorB=F0DB4F
83
+ [npm-version-src]: https://img.shields.io/npm/v/kontroll?labelColor=18181B&color=F0DB4F
83
84
  [npm-version-href]: https://npmjs.com/package/kontroll
84
- [npm-downloads-src]: https://img.shields.io/npm/dm/kontroll?style=flat&colorA=18181B&colorB=F0DB4F
85
+ [npm-downloads-src]: https://img.shields.io/npm/dm/kontroll?labelColor=18181B&color=F0DB4F
85
86
  [npm-downloads-href]: https://npmjs.com/package/kontroll
86
- [codecov-src]: https://img.shields.io/codecov/c/gh/namesmt/kontroll/main?style=flat&colorA=18181B&colorB=F0DB4F
87
+ [codecov-src]: https://img.shields.io/codecov/c/gh/namesmt/kontroll/main?labelColor=18181B&color=F0DB4F
87
88
  [codecov-href]: https://codecov.io/gh/namesmt/kontroll
88
- [license-src]: https://img.shields.io/github/license/namesmt/kontroll.svg?style=flat&colorA=18181B&colorB=F0DB4F
89
+ [license-src]: https://img.shields.io/github/license/namesmt/kontroll.svg?labelColor=18181B&color=F0DB4F
89
90
  [license-href]: https://github.com/namesmt/kontroll/blob/main/LICENSE
90
- [bundlejs-src]: https://deno.bundlejs.com/badge?q=kontroll@0.1.0&colorA=18181B&colorB=F0DB4F
91
- [bundlejs-href]: https://bundlejs.com/?q=klona
91
+ [bundlejs-src]: https://img.shields.io/bundlejs/size/kontroll?labelColor=18181B&color=F0DB4F
92
+ [bundlejs-href]: https://bundlejs.com/?q=kontroll
93
+ [jsDocs-src]: https://img.shields.io/badge/Check_out-jsDocs.io---?labelColor=18181B&color=F0DB4F
94
+ [jsDocs-href]: https://www.jsdocs.io/package/kontroll
@@ -0,0 +1,129 @@
1
+ type Fn<T = void> = () => T;
2
+ type FnWithArgs<T = void> = (...args: any) => T;
3
+ interface KontrollStore {
4
+ [x: keyof any]: {
5
+ timer: ReturnType<typeof setTimeout>;
6
+ callback: Fn;
7
+ trailing?: [FnWithArgs, ...any];
8
+ };
9
+ }
10
+ declare function clear(callback: Fn): void;
11
+ declare function clear(key: keyof KontrollStore): void;
12
+ type KontrollClearer = Fn;
13
+ interface KontrollBaseOptions {
14
+ /**
15
+ * Specify a known key if needed
16
+ * @default string // .toString() of the inputted callback
17
+ */
18
+ key?: keyof KontrollStore;
19
+ }
20
+ interface KontrollCountdownOptions extends KontrollBaseOptions {
21
+ /**
22
+ * Replaces the current timed callback
23
+ * @default false
24
+ */
25
+ replace?: boolean;
26
+ }
27
+ /**
28
+ * Countdown for a period of ms then execute the first/replaced callback, based on `options.replace`.
29
+ * Calls while the timer haven't finished are dropped.
30
+ * ---
31
+ *
32
+ * Example
33
+ * ```
34
+ * countdown(1000, doSum(1), { key: 'eg' })
35
+ * // 500ms passed
36
+ * countdown(1000, doSum(2))
37
+ * // 500ms passed
38
+ * // Result: 1
39
+ * ```
40
+ * ---
41
+ *
42
+ * Example with `options.replace=true`:
43
+ * ```
44
+ * countdown(1000, doSum(1))
45
+ * // 500ms passed
46
+ * countdown(1000, doSum(2), { replace: true })
47
+ * // 500ms passed
48
+ * // Result: 2
49
+ * ```
50
+ */
51
+ declare function countdown(ms: number, callback: Fn, options?: KontrollCountdownOptions): KontrollClearer;
52
+ interface KontrollDebounceOptions extends KontrollBaseOptions {
53
+ /**
54
+ * Avoiding initial wait for first call
55
+ * @default false
56
+ */
57
+ leading?: boolean;
58
+ }
59
+ /**
60
+ * Creates a timer that will execute the callback upon finish, calls while the timer haven't finished recreates the timer.
61
+ * If `options.leading`, execute callback immediately for initial call.
62
+ * ---
63
+ *
64
+ * Example:
65
+ * ```
66
+ * debounce(1000, doSum(1))
67
+ * // 500ms passed
68
+ * debounce(1000, doSum(2))
69
+ * // 1000ms passed
70
+ * // Result: 2
71
+ * ```
72
+ * ---
73
+ *
74
+ * Example with `options.leading`:
75
+ * ```
76
+ * debounce(1000, doSum(1), { leading: true })
77
+ * // Result: 1
78
+ * // 500ms passed
79
+ * debounce(1000, doSum(2), { leading: true }) // leading doesn't matter anymore in this timer scope
80
+ * // 500ms passed
81
+ * debounce(1000, doSum(3))
82
+ * // 1000ms passed
83
+ * // Result: 3
84
+ * ```
85
+ */
86
+ declare function debounce(ms: number, callback: Fn, options?: KontrollDebounceOptions): KontrollClearer;
87
+ interface KontrollThrottleOptions extends KontrollBaseOptions {
88
+ /**
89
+ * Perform additional execution with last received arguments
90
+ * @default false
91
+ */
92
+ trailing?: boolean;
93
+ }
94
+ /**
95
+ * Executes the callback, and bypass any subsequent calls for a period of ms.
96
+ * Calls while the timer haven't finished are dropped.
97
+ * If `options.trailing` and calls while the timer haven't finished are received,
98
+ * an additional execution with last received arguments is performed.
99
+ * ---
100
+ *
101
+ * Example:
102
+ * ```
103
+ * throttle(1000, doSum(1))
104
+ * // Result: 1
105
+ * // 500ms passed
106
+ * throttle(1000, doSum(2))
107
+ * // 9999ms passed
108
+ * // (Nothing)
109
+ * ```
110
+ * ---
111
+ *
112
+ * Example with `options.trailing`:
113
+ * ```
114
+ * throttle(1000, doSum(1))
115
+ * // Result: 1
116
+ * // 500ms passed
117
+ * throttle(1000, doSum(2), { trailing: true })
118
+ * // 500ms passed
119
+ * // Result: 2
120
+ * // A timer is also created, so calls after that are still dropped:
121
+ * throttle(1000, doSum(3))
122
+ * // 9999ms passed
123
+ * // (Nothing)
124
+ * ```
125
+ *
126
+ */
127
+ declare function throttle(ms: number, callback: Fn, options?: KontrollThrottleOptions): KontrollClearer;
128
+
129
+ export { type KontrollBaseOptions, type KontrollClearer, type KontrollCountdownOptions, type KontrollDebounceOptions, type KontrollStore, type KontrollThrottleOptions, clear, countdown, debounce, throttle };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "kontroll",
3
3
  "type": "module",
4
- "version": "0.1.2",
4
+ "version": "0.1.4",
5
5
  "packageManager": "pnpm@8.12.0",
6
6
  "description": "kontroll (\"control\") is a tiny, dead-simple package for function behavior controls like debounce, countdown, throttle (limit).",
7
7
  "author": "NamesMT <dangquoctrung123@gmail.com>",