@useless-engineer/utils 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +11 -3
- package/dist/index.cjs +60 -0
- package/dist/index.d.cts +29 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +57 -0
- package/dist/react-native/index.cjs +10 -0
- package/dist/react-native/index.d.cts +4 -0
- package/dist/react-native/index.d.ts +4 -0
- package/dist/react-native/index.js +7 -0
- package/package.json +91 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 saurabh bandkar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# @useless-engineer/utils
|
|
2
|
+
|
|
3
|
+
Small, typed utilities from [@useless_engineer.dev](https://www.instagram.com/useless_engineer.dev/).
|
|
4
|
+
One package, multiple entry points, so React Native code never pulls in what it doesn't use.
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
import { debounce, throttle } from "@useless-engineer/utils";
|
|
8
|
+
import { isIOS, isAndroid } from "@useless-engineer/utils/react-native";
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
`react` and `react-native` are optional peer dependencies.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/async/debounce.ts
|
|
4
|
+
function debounce(fn, delay) {
|
|
5
|
+
let timer;
|
|
6
|
+
const debounced = (...args) => {
|
|
7
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
8
|
+
timer = setTimeout(() => {
|
|
9
|
+
timer = void 0;
|
|
10
|
+
fn(...args);
|
|
11
|
+
}, delay);
|
|
12
|
+
};
|
|
13
|
+
debounced.cancel = () => {
|
|
14
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
15
|
+
timer = void 0;
|
|
16
|
+
};
|
|
17
|
+
return debounced;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// src/async/throttle.ts
|
|
21
|
+
function throttle(fn, interval, { leading = true, trailing = true } = {}) {
|
|
22
|
+
let timer;
|
|
23
|
+
let lastArgs;
|
|
24
|
+
let lastRun = 0;
|
|
25
|
+
const run = (args) => {
|
|
26
|
+
lastRun = Date.now();
|
|
27
|
+
fn(...args);
|
|
28
|
+
};
|
|
29
|
+
const throttled = (...args) => {
|
|
30
|
+
const now = Date.now();
|
|
31
|
+
if (lastRun === 0 && !leading) lastRun = now;
|
|
32
|
+
const remaining = interval - (now - lastRun);
|
|
33
|
+
if (remaining <= 0) {
|
|
34
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
35
|
+
timer = void 0;
|
|
36
|
+
lastArgs = void 0;
|
|
37
|
+
run(args);
|
|
38
|
+
} else if (trailing) {
|
|
39
|
+
lastArgs = args;
|
|
40
|
+
if (timer === void 0) {
|
|
41
|
+
timer = setTimeout(() => {
|
|
42
|
+
timer = void 0;
|
|
43
|
+
const a = lastArgs;
|
|
44
|
+
lastArgs = void 0;
|
|
45
|
+
if (a) run(a);
|
|
46
|
+
}, remaining);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
throttled.cancel = () => {
|
|
51
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
52
|
+
timer = void 0;
|
|
53
|
+
lastArgs = void 0;
|
|
54
|
+
lastRun = 0;
|
|
55
|
+
};
|
|
56
|
+
return throttled;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
exports.debounce = debounce;
|
|
60
|
+
exports.throttle = throttle;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
interface Debounced<A extends unknown[]> {
|
|
2
|
+
(...args: A): void;
|
|
3
|
+
/** Drop any pending call. Call this from your effect cleanup. */
|
|
4
|
+
cancel: () => void;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Delays `fn` until `delay` ms have passed with no new calls.
|
|
8
|
+
* Every call resets the timer; only the last one fires.
|
|
9
|
+
*/
|
|
10
|
+
declare function debounce<A extends unknown[]>(fn: (...args: A) => void, delay: number): Debounced<A>;
|
|
11
|
+
|
|
12
|
+
interface ThrottleOptions {
|
|
13
|
+
/** Fire immediately on the first call of a burst. Default: true */
|
|
14
|
+
leading?: boolean;
|
|
15
|
+
/** Fire once more at the end of the burst with the latest args. Default: true */
|
|
16
|
+
trailing?: boolean;
|
|
17
|
+
}
|
|
18
|
+
interface Throttled<A extends unknown[]> {
|
|
19
|
+
(...args: A): void;
|
|
20
|
+
/** Drop any pending trailing call and reset. */
|
|
21
|
+
cancel: () => void;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Runs `fn` at most once per `interval` ms, however often it's called.
|
|
25
|
+
* Leading edge keeps UI snappy; trailing edge preserves the final update.
|
|
26
|
+
*/
|
|
27
|
+
declare function throttle<A extends unknown[]>(fn: (...args: A) => void, interval: number, { leading, trailing }?: ThrottleOptions): Throttled<A>;
|
|
28
|
+
|
|
29
|
+
export { type Debounced, type ThrottleOptions, type Throttled, debounce, throttle };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
interface Debounced<A extends unknown[]> {
|
|
2
|
+
(...args: A): void;
|
|
3
|
+
/** Drop any pending call. Call this from your effect cleanup. */
|
|
4
|
+
cancel: () => void;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Delays `fn` until `delay` ms have passed with no new calls.
|
|
8
|
+
* Every call resets the timer; only the last one fires.
|
|
9
|
+
*/
|
|
10
|
+
declare function debounce<A extends unknown[]>(fn: (...args: A) => void, delay: number): Debounced<A>;
|
|
11
|
+
|
|
12
|
+
interface ThrottleOptions {
|
|
13
|
+
/** Fire immediately on the first call of a burst. Default: true */
|
|
14
|
+
leading?: boolean;
|
|
15
|
+
/** Fire once more at the end of the burst with the latest args. Default: true */
|
|
16
|
+
trailing?: boolean;
|
|
17
|
+
}
|
|
18
|
+
interface Throttled<A extends unknown[]> {
|
|
19
|
+
(...args: A): void;
|
|
20
|
+
/** Drop any pending trailing call and reset. */
|
|
21
|
+
cancel: () => void;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Runs `fn` at most once per `interval` ms, however often it's called.
|
|
25
|
+
* Leading edge keeps UI snappy; trailing edge preserves the final update.
|
|
26
|
+
*/
|
|
27
|
+
declare function throttle<A extends unknown[]>(fn: (...args: A) => void, interval: number, { leading, trailing }?: ThrottleOptions): Throttled<A>;
|
|
28
|
+
|
|
29
|
+
export { type Debounced, type ThrottleOptions, type Throttled, debounce, throttle };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// src/async/debounce.ts
|
|
2
|
+
function debounce(fn, delay) {
|
|
3
|
+
let timer;
|
|
4
|
+
const debounced = (...args) => {
|
|
5
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
6
|
+
timer = setTimeout(() => {
|
|
7
|
+
timer = void 0;
|
|
8
|
+
fn(...args);
|
|
9
|
+
}, delay);
|
|
10
|
+
};
|
|
11
|
+
debounced.cancel = () => {
|
|
12
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
13
|
+
timer = void 0;
|
|
14
|
+
};
|
|
15
|
+
return debounced;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// src/async/throttle.ts
|
|
19
|
+
function throttle(fn, interval, { leading = true, trailing = true } = {}) {
|
|
20
|
+
let timer;
|
|
21
|
+
let lastArgs;
|
|
22
|
+
let lastRun = 0;
|
|
23
|
+
const run = (args) => {
|
|
24
|
+
lastRun = Date.now();
|
|
25
|
+
fn(...args);
|
|
26
|
+
};
|
|
27
|
+
const throttled = (...args) => {
|
|
28
|
+
const now = Date.now();
|
|
29
|
+
if (lastRun === 0 && !leading) lastRun = now;
|
|
30
|
+
const remaining = interval - (now - lastRun);
|
|
31
|
+
if (remaining <= 0) {
|
|
32
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
33
|
+
timer = void 0;
|
|
34
|
+
lastArgs = void 0;
|
|
35
|
+
run(args);
|
|
36
|
+
} else if (trailing) {
|
|
37
|
+
lastArgs = args;
|
|
38
|
+
if (timer === void 0) {
|
|
39
|
+
timer = setTimeout(() => {
|
|
40
|
+
timer = void 0;
|
|
41
|
+
const a = lastArgs;
|
|
42
|
+
lastArgs = void 0;
|
|
43
|
+
if (a) run(a);
|
|
44
|
+
}, remaining);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
throttled.cancel = () => {
|
|
49
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
50
|
+
timer = void 0;
|
|
51
|
+
lastArgs = void 0;
|
|
52
|
+
lastRun = 0;
|
|
53
|
+
};
|
|
54
|
+
return throttled;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export { debounce, throttle };
|
package/package.json
CHANGED
|
@@ -1,6 +1,93 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@useless-engineer/utils",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "repo for small, utility functions by useless engineer",
|
|
5
|
+
"author": "Useless Engineer",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"utils",
|
|
8
|
+
"typescript",
|
|
9
|
+
"react-native",
|
|
10
|
+
"debounce",
|
|
11
|
+
"throttle"
|
|
12
|
+
],
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/saurabhbandkar/useless-engineer.git"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"type": "module",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/saurabhbandkar/useless-engineer/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/saurabhbandkar/useless-engineer#readme",
|
|
27
|
+
"main": "./dist/index.cjs",
|
|
28
|
+
"module": "./dist/index.js",
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"import": {
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"default": "./dist/index.js"
|
|
35
|
+
},
|
|
36
|
+
"require": {
|
|
37
|
+
"types": "./dist/index.d.cts",
|
|
38
|
+
"default": "./dist/index.cjs"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"./react-native": {
|
|
42
|
+
"import": {
|
|
43
|
+
"types": "./dist/react-native/index.d.ts",
|
|
44
|
+
"default": "./dist/react-native/index.js"
|
|
45
|
+
},
|
|
46
|
+
"require": {
|
|
47
|
+
"types": "./dist/react-native/index.d.cts",
|
|
48
|
+
"default": "./dist/react-native/index.cjs"
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"./package.json": "./package.json"
|
|
52
|
+
},
|
|
53
|
+
"typesVersions": {
|
|
54
|
+
"*": {
|
|
55
|
+
"react-native": [
|
|
56
|
+
"./dist/react-native/index.d.ts"
|
|
57
|
+
]
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
"scripts": {
|
|
61
|
+
"build": "tsup",
|
|
62
|
+
"typecheck": "tsc --noEmit",
|
|
63
|
+
"test": "node --import tsx --test test/*.test.ts",
|
|
64
|
+
"prepublishOnly": "npm run typecheck && npm test && npm run build"
|
|
65
|
+
},
|
|
66
|
+
"peerDependencies": {
|
|
67
|
+
"react": ">=18",
|
|
68
|
+
"react-native": ">=0.72"
|
|
69
|
+
},
|
|
70
|
+
"peerDependenciesMeta": {
|
|
71
|
+
"react": {
|
|
72
|
+
"optional": true
|
|
73
|
+
},
|
|
74
|
+
"react-native": {
|
|
75
|
+
"optional": true
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
"devDependencies": {
|
|
79
|
+
"@types/node": "^22.0.0",
|
|
80
|
+
"tsup": "^8.0.0",
|
|
81
|
+
"tsx": "^4.0.0",
|
|
82
|
+
"typescript": "^5.5.0"
|
|
83
|
+
},
|
|
84
|
+
"publishConfig": {
|
|
85
|
+
"access": "public"
|
|
86
|
+
},
|
|
87
|
+
"allowScripts": {
|
|
88
|
+
"esbuild": true
|
|
89
|
+
},
|
|
90
|
+
"overrides": {
|
|
91
|
+
"esbuild": "^0.28.2"
|
|
92
|
+
}
|
|
93
|
+
}
|