@useless-engineer/utils 0.0.0-stage β†’ 0.1.1

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 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,210 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
1
+ <div align="center">
2
+
3
+ # πŸ› οΈ @useless-engineer/utils
4
+
5
+ **Small, typed utilities distilled from real production bugs.**
6
+ One package. Multiple entry points. Only pay for what you import.
7
+
8
+ [![npm version](https://img.shields.io/npm/v/@useless-engineer/utils?style=for-the-badge&logo=npm&color=CB3837&labelColor=1a1a1a)](https://www.npmjs.com/package/@useless-engineer/utils)
9
+ [![npm downloads](https://img.shields.io/npm/dm/@useless-engineer/utils?style=for-the-badge&color=7c3aed&labelColor=1a1a1a)](https://www.npmjs.com/package/@useless-engineer/utils)
10
+ [![license](https://img.shields.io/npm/l/@useless-engineer/utils?style=for-the-badge&color=22c55e&labelColor=1a1a1a)](./LICENSE)
11
+
12
+ ![TypeScript](https://img.shields.io/badge/TypeScript-ready-3178C6?style=flat-square&logo=typescript&logoColor=white)
13
+ ![Tree-shakeable](https://img.shields.io/badge/tree--shakeable-yes-10b981?style=flat-square)
14
+ ![Zero deps](https://img.shields.io/badge/dependencies-0-f59e0b?style=flat-square)
15
+ ![ESM + CJS](https://img.shields.io/badge/ESM%20%2B%20CJS-both-0ea5e9?style=flat-square)
16
+ ![React Native](https://img.shields.io/badge/React%20Native-supported-61DAFB?style=flat-square&logo=react&logoColor=black)
17
+
18
+ [**πŸ“¦ Install**](#-install) Β·
19
+ [**⚑ Quick start**](#-quick-start) ·
20
+ [**πŸ“š API**](#-api) Β·
21
+ [**πŸ—ΊοΈ Roadmap**](#️-roadmap) Β·
22
+ [**πŸ“Έ Follow**](https://www.instagram.com/useless_engineer.dev/)
23
+
24
+ </div>
25
+
26
+ ---
27
+
28
+ ## ✨ Why this exists
29
+
30
+ Every utility here started as an [Instagram post](https://www.instagram.com/useless_engineer.dev/) about a bug that actually shipped. The post explains the problem; this package is the fix, typed and tested, so you never copy-paste it again.
31
+
32
+ | | |
33
+ | ------------------------ | ------------------------------------------------------ |
34
+ | 🎯 **Focused** | Each function does one thing and has tests |
35
+ | 🌲 **Tree-shakeable** | `sideEffects: false`, import only what you use |
36
+ | 🧩 **Split by platform** | Core, React Native, and more, each its own import path |
37
+ | πŸ”’ **Strictly typed** | Written in TypeScript with strict mode on |
38
+
39
+ ---
40
+
41
+ ## πŸ“¦ Install
42
+
43
+ ```bash
44
+ npm install @useless-engineer/utils
45
+ ```
46
+
47
+ <details>
48
+ <summary><b>pnpm / yarn / bun</b></summary>
49
+
50
+ ```bash
51
+ pnpm add @useless-engineer/utils
52
+ yarn add @useless-engineer/utils
53
+ bun add @useless-engineer/utils
54
+ ```
55
+
56
+ </details>
57
+
58
+ ---
59
+
60
+ ## ⚑ Quick start
61
+
62
+ ```ts
63
+ import { debounce, throttle } from "@useless-engineer/utils";
64
+
65
+ const onSearch = debounce((query: string) => fetchResults(query), 300);
66
+ const onScroll = throttle(() => updateHeader(), 100);
67
+ ```
68
+
69
+ ```ts
70
+ import { isIOS, isAndroid } from "@useless-engineer/utils/react-native";
71
+
72
+ const padding = isIOS ? 20 : 16;
73
+ ```
74
+
75
+ > πŸ’‘ `react` and `react-native` are **optional** peer dependencies. If you only use the core entry, you never need to install either.
76
+
77
+ ---
78
+
79
+ ## 🧭 Entry points
80
+
81
+ | Import path | For | Status |
82
+ | -------------------------------------- | ------------------------- | ----------------------------------------------------------------------------- |
83
+ | `@useless-engineer/utils` | Platform-agnostic helpers | ![live](https://img.shields.io/badge/-live-22c55e?style=flat-square) |
84
+ | `@useless-engineer/utils/react-native` | React Native helpers | ![live](https://img.shields.io/badge/-live-22c55e?style=flat-square) |
85
+ | `@useless-engineer/utils/react` | React hooks | ![soon](https://img.shields.io/badge/-coming%20soon-f59e0b?style=flat-square) |
86
+
87
+ ---
88
+
89
+ ## πŸ“š API
90
+
91
+ ### Core
92
+
93
+ <details>
94
+ <summary><code>debounce(fn, delay)</code> &nbsp; ![live](https://img.shields.io/badge/-live-22c55e?style=flat-square) ![async](https://img.shields.io/badge/-async-6366f1?style=flat-square)</summary>
95
+
96
+ <br>
97
+
98
+ Waits for quiet. Every call resets the timer, and only the last call fires. Good for search inputs and autosave.
99
+
100
+ ```ts
101
+ const save = debounce((text: string) => api.save(text), 500);
102
+
103
+ save("h");
104
+ save("he");
105
+ save("hello"); // only this one runs, 500ms after the last call
106
+
107
+ // In a React effect cleanup, so nothing fires after unmount:
108
+ save.cancel();
109
+ ```
110
+
111
+ | Param | Type | Description |
112
+ | ----------- | ------------------- | ----------------------------------- |
113
+ | `fn` | `(...args) => void` | Function to debounce |
114
+ | `delay` | `number` | Milliseconds of quiet before firing |
115
+ | **returns** | `Debounced` | Callable with a `.cancel()` method |
116
+
117
+ </details>
118
+
119
+ <details>
120
+ <summary><code>throttle(fn, interval, options?)</code> &nbsp; ![live](https://img.shields.io/badge/-live-22c55e?style=flat-square) ![async](https://img.shields.io/badge/-async-6366f1?style=flat-square)</summary>
121
+
122
+ <br>
123
+
124
+ Keeps a steady beat. Runs at most once per interval, however often it's called. Good for scroll, drag, and resize.
125
+
126
+ ```ts
127
+ const onScroll = throttle(() => updateHeader(), 100);
128
+
129
+ // Skip the immediate first call:
130
+ throttle(fn, 100, { leading: false });
131
+
132
+ // Skip the final catch-up call:
133
+ throttle(fn, 100, { trailing: false });
134
+ ```
135
+
136
+ | Option | Default | Description |
137
+ | ---------- | ------- | --------------------------------------------------- |
138
+ | `leading` | `true` | Fire right away on the first call of a burst |
139
+ | `trailing` | `true` | Fire once more at the end with the latest arguments |
140
+
141
+ Returns a function with `.cancel()`.
142
+
143
+ </details>
144
+
145
+ <details>
146
+ <summary><b>debounce or throttle?</b></summary>
147
+
148
+ <br>
149
+
150
+ | | `debounce` | `throttle` |
151
+ | ------------ | --------------------------------- | -------------------------------- |
152
+ | Fires | Once, after calls **stop** | On a fixed beat **during** calls |
153
+ | Use for | Search box, autosave, validation | Scroll, drag, resize |
154
+ | Ask yourself | "Do I only need the final state?" | "Does it need to stay live?" |
155
+
156
+ </details>
157
+
158
+ ### React Native
159
+
160
+ <details>
161
+ <summary><code>isIOS</code> / <code>isAndroid</code> &nbsp; ![live](https://img.shields.io/badge/-live-22c55e?style=flat-square) ![react native](https://img.shields.io/badge/-react%20native-61DAFB?style=flat-square&logoColor=black)</summary>
162
+
163
+ <br>
164
+
165
+ Plain booleans derived from `Platform.OS`.
166
+
167
+ ```ts
168
+ import { isIOS, isAndroid } from "@useless-engineer/utils/react-native";
169
+
170
+ const behavior = isIOS ? "padding" : "height";
171
+ ```
172
+
173
+ </details>
174
+
175
+ ---
176
+
177
+ ## πŸ—ΊοΈ Roadmap
178
+
179
+ Released one or two at a time, each matching a post that's already live.
180
+
181
+ | Next up | Entry | Origin |
182
+ | ------------------------------------------------------------------------------------------------------------------------ | ------------ | ----------------------------------------- |
183
+ | ![next](https://img.shields.io/badge/-next-f59e0b?style=flat-square) `delay`, `withTimeout` | core | Validated timers, `Promise.race` timeouts |
184
+ | ![planned](https://img.shields.io/badge/-planned-64748b?style=flat-square) `safeParseJSON`, `safeStringify`, `deepClone` | core | JSON and shallow-copy gotchas |
185
+ | ![planned](https://img.shields.io/badge/-planned-64748b?style=flat-square) `getAccessibilityProps`, `createShadow` | react-native | Accessibility and iOS-only shadows |
186
+ | ![planned](https://img.shields.io/badge/-planned-64748b?style=flat-square) `useInterval`, `useCountdown`, `useIsMounted` | react | Stale closures and unmounted updates |
187
+
188
+ ---
189
+
190
+ ## 🀝 Contributing
191
+
192
+ Found a bug or have a post idea that deserves a utility? [Open an issue](https://github.com/<your-username>/useless-engineer/issues).
193
+
194
+ ```bash
195
+ npm install
196
+ npm test
197
+ npm run build
198
+ ```
199
+
200
+ ---
201
+
202
+ <div align="center">
203
+
204
+ **Built from real bugs, one post at a time.**
205
+
206
+ [![Instagram](https://img.shields.io/badge/Follow-@useless__engineer.dev-E4405F?style=for-the-badge&logo=instagram&logoColor=white)](https://www.instagram.com/useless_engineer.dev/)
207
+
208
+ MIT Β© Saurabh Bandkar
209
+
210
+ </div>
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;
@@ -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 };
@@ -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 };
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ var reactNative = require('react-native');
4
+
5
+ // src/react-native/index.ts
6
+ var isIOS = reactNative.Platform.OS === "ios";
7
+ var isAndroid = reactNative.Platform.OS === "android";
8
+
9
+ exports.isAndroid = isAndroid;
10
+ exports.isIOS = isIOS;
@@ -0,0 +1,4 @@
1
+ declare const isIOS: boolean;
2
+ declare const isAndroid: boolean;
3
+
4
+ export { isAndroid, isIOS };
@@ -0,0 +1,4 @@
1
+ declare const isIOS: boolean;
2
+ declare const isAndroid: boolean;
3
+
4
+ export { isAndroid, isIOS };
@@ -0,0 +1,7 @@
1
+ import { Platform } from 'react-native';
2
+
3
+ // src/react-native/index.ts
4
+ var isIOS = Platform.OS === "ios";
5
+ var isAndroid = Platform.OS === "android";
6
+
7
+ export { isAndroid, isIOS };
package/package.json CHANGED
@@ -1,6 +1,93 @@
1
1
  {
2
2
  "name": "@useless-engineer/utils",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
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
+ }