tiny-essentials 1.13.0 β 1.13.2
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 +1 -0
- package/dist/v1/TinyBasicsEs.js +139 -0
- package/dist/v1/TinyBasicsEs.min.js +1 -1
- package/dist/v1/TinyDragger.js +100 -0
- package/dist/v1/TinyEssentials.js +139 -0
- package/dist/v1/TinyEssentials.min.js +1 -1
- package/dist/v1/TinyUploadClicker.js +100 -0
- package/dist/v1/basics/html.cjs +101 -0
- package/dist/v1/basics/html.d.mts +15 -0
- package/dist/v1/basics/html.mjs +86 -0
- package/dist/v1/basics/index.cjs +2 -0
- package/dist/v1/basics/index.d.mts +3 -1
- package/dist/v1/basics/index.mjs +3 -3
- package/dist/v1/basics/simpleMath.cjs +38 -0
- package/dist/v1/basics/simpleMath.d.mts +24 -0
- package/dist/v1/basics/simpleMath.mjs +29 -0
- package/dist/v1/index.cjs +2 -0
- package/dist/v1/index.d.mts +3 -1
- package/dist/v1/index.mjs +3 -3
- package/docs/v1/basics/html.md +126 -23
- package/docs/v1/basics/simpleMath.md +28 -0
- package/package.json +1 -1
package/docs/v1/basics/html.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
### π `areHtmlElsColliding()`
|
|
2
2
|
|
|
3
3
|
Check if two DOM elements are **colliding on the screen**! Perfect for games, draggable elements, UI interactions, and more.
|
|
4
4
|
|
|
@@ -13,7 +13,7 @@ It compares the bounding rectangles of both elements:
|
|
|
13
13
|
|
|
14
14
|
---
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
#### π§ Syntax
|
|
17
17
|
|
|
18
18
|
```javascript
|
|
19
19
|
areHtmlElsColliding(elem1, elem2);
|
|
@@ -21,7 +21,7 @@ areHtmlElsColliding(elem1, elem2);
|
|
|
21
21
|
|
|
22
22
|
---
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
#### π― Parameters
|
|
25
25
|
|
|
26
26
|
| Parameter | Type | Description |
|
|
27
27
|
| --------- | --------- | ----------------------- |
|
|
@@ -30,7 +30,7 @@ areHtmlElsColliding(elem1, elem2);
|
|
|
30
30
|
|
|
31
31
|
---
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
#### π Return
|
|
34
34
|
|
|
35
35
|
| Type | Description |
|
|
36
36
|
| --------- | ------------------------------------------------------------------ |
|
|
@@ -38,7 +38,7 @@ areHtmlElsColliding(elem1, elem2);
|
|
|
38
38
|
|
|
39
39
|
---
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
#### π¦ Example
|
|
42
42
|
|
|
43
43
|
```javascript
|
|
44
44
|
const box1 = document.getElementById('box1');
|
|
@@ -53,31 +53,31 @@ if (areHtmlElsColliding(box1, box2)) {
|
|
|
53
53
|
|
|
54
54
|
---
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
#### π§ Limitations
|
|
57
57
|
|
|
58
58
|
* Only works with **axis-aligned elements** (rectangular shapes).
|
|
59
59
|
* Does not handle rotated elements or complex shapes.
|
|
60
60
|
|
|
61
61
|
---
|
|
62
62
|
|
|
63
|
-
|
|
63
|
+
### π `readJsonBlob(file: File): Promise<any>`
|
|
64
64
|
|
|
65
65
|
Reads and parses a JSON file using the [`FileReader`](https://developer.mozilla.org/en-US/docs/Web/API/FileReader) API.
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
#### π₯ Parameters
|
|
68
68
|
|
|
69
69
|
* `file` *(File)*: The file object selected by the user (e.g., from an `<input type="file">` element).
|
|
70
70
|
|
|
71
|
-
|
|
71
|
+
#### π€ Returns
|
|
72
72
|
|
|
73
73
|
* `Promise<any>`: Resolves with the parsed JSON object, or rejects with an error if the content is invalid.
|
|
74
74
|
|
|
75
|
-
|
|
75
|
+
#### β οΈ Throws
|
|
76
76
|
|
|
77
77
|
* An error if the content is not valid JSON.
|
|
78
78
|
* An error if the file can't be read.
|
|
79
79
|
|
|
80
|
-
|
|
80
|
+
#### π§ͺ Example
|
|
81
81
|
|
|
82
82
|
```js
|
|
83
83
|
const input = document.querySelector('input[type="file"]');
|
|
@@ -93,36 +93,36 @@ input.addEventListener('change', async () => {
|
|
|
93
93
|
|
|
94
94
|
---
|
|
95
95
|
|
|
96
|
-
|
|
96
|
+
### πΎ `saveJsonFile(filename: string, data: any, spaces: number = 2): void`
|
|
97
97
|
|
|
98
98
|
Converts a JavaScript object to JSON and triggers a download in the browser.
|
|
99
99
|
|
|
100
|
-
|
|
100
|
+
#### π₯ Parameters
|
|
101
101
|
|
|
102
102
|
* `filename` *(string)*: The name of the file to save (e.g., `"data.json"`).
|
|
103
103
|
* `data` *(any)*: The JavaScript object to convert to JSON.
|
|
104
104
|
* `spaces` *(number)* *(optional)*: Indentation level for formatting the JSON string. Default is `2`.
|
|
105
105
|
|
|
106
|
-
|
|
106
|
+
#### π€ Returns
|
|
107
107
|
|
|
108
108
|
* `void`
|
|
109
109
|
|
|
110
|
-
|
|
110
|
+
#### π Behavior
|
|
111
111
|
|
|
112
112
|
Creates a temporary `<a>` element, downloads the file, and cleans up the URL.
|
|
113
113
|
|
|
114
|
-
|
|
114
|
+
#### π§ͺ Example
|
|
115
115
|
|
|
116
116
|
```js
|
|
117
117
|
const data = { name: 'Yasmin', type: 'dev' };
|
|
118
118
|
saveJsonFile('yasmin.json', data);
|
|
119
119
|
```
|
|
120
120
|
|
|
121
|
-
|
|
121
|
+
### π `fetchJson(url, options?): Promise<any>`
|
|
122
122
|
|
|
123
123
|
Loads and parses a JSON from a remote URL using the Fetch API, with support for custom HTTP methods, retries, timeouts, headers, and even external abort controllers.
|
|
124
124
|
|
|
125
|
-
|
|
125
|
+
#### π₯ Parameters
|
|
126
126
|
|
|
127
127
|
* `url` *(string)*: The full URL to fetch JSON from (must start with `http://`, `https://`, `/`, `./`, or `../`).
|
|
128
128
|
* `options` *(object)* *(optional)*:
|
|
@@ -134,7 +134,7 @@ Loads and parses a JSON from a remote URL using the Fetch API, with support for
|
|
|
134
134
|
* `headers` *(object)*: Additional headers to include in the request.
|
|
135
135
|
* `body` *(object)*: Request body. If the value is a plain object, it will be automatically stringified as JSON.
|
|
136
136
|
|
|
137
|
-
|
|
137
|
+
##### `signal` (`AbortSignal` | `null`) β *optional*
|
|
138
138
|
|
|
139
139
|
Custom abort signal. If set:
|
|
140
140
|
|
|
@@ -142,21 +142,21 @@ Custom abort signal. If set:
|
|
|
142
142
|
* Retry logic is **disabled**
|
|
143
143
|
* Abortion is handled externally
|
|
144
144
|
|
|
145
|
-
|
|
145
|
+
#### π€ Returns
|
|
146
146
|
|
|
147
147
|
* `Promise<any>`: Resolves with the parsed JSON data.
|
|
148
148
|
|
|
149
|
-
|
|
149
|
+
#### β οΈ Throws
|
|
150
150
|
|
|
151
151
|
* `Error` if the fetch fails or exceeds the timeout
|
|
152
152
|
* `Error` if the response is not `application/json`
|
|
153
153
|
* `Error` if the result is not a plain JSON object
|
|
154
154
|
|
|
155
|
-
|
|
155
|
+
#### π§ Tip
|
|
156
156
|
|
|
157
157
|
If you pass your own `signal`, this disables both `timeout` and `retries`. Use it when you're managing cancellation manually (e.g. in UI components or async workflows).
|
|
158
158
|
|
|
159
|
-
|
|
159
|
+
#### π§ͺ Example
|
|
160
160
|
|
|
161
161
|
```js
|
|
162
162
|
const controller = new AbortController();
|
|
@@ -241,3 +241,106 @@ getHtmlElPadding(el: Element): HtmlElBoxSides
|
|
|
241
241
|
|
|
242
242
|
* `el`: The target DOM element.
|
|
243
243
|
* **Returns**: Padding values for all sides and summed horizontal (`x`) and vertical (`y`) values.
|
|
244
|
+
|
|
245
|
+
---
|
|
246
|
+
|
|
247
|
+
### π `installWindowHiddenScript`
|
|
248
|
+
|
|
249
|
+
Automatically toggles CSS classes on a given element based on the browser window or tab **visibility** and **focus** state.
|
|
250
|
+
|
|
251
|
+
Perfect for UI states like dimming, pausing animations, or showing "away" statuses.
|
|
252
|
+
|
|
253
|
+
---
|
|
254
|
+
|
|
255
|
+
#### π§ Features
|
|
256
|
+
|
|
257
|
+
* β
Adds or removes custom CSS classes depending on page visibility or focus
|
|
258
|
+
* β
Supports modern and legacy browsers (including IE9)
|
|
259
|
+
* β
Automatically dispatches an initial state check on load
|
|
260
|
+
* β
Returns a cleanup function to remove all listeners
|
|
261
|
+
|
|
262
|
+
---
|
|
263
|
+
|
|
264
|
+
#### π§ͺ Usage
|
|
265
|
+
|
|
266
|
+
```js
|
|
267
|
+
import { installWindowHiddenScript } from 'tiny-essentials';
|
|
268
|
+
|
|
269
|
+
const uninstall = installWindowHiddenScript({
|
|
270
|
+
element: document.getElementById('app'),
|
|
271
|
+
hiddenClass: 'is-hidden',
|
|
272
|
+
visibleClass: 'is-visible',
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
// To remove all listeners later
|
|
276
|
+
uninstall();
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
---
|
|
280
|
+
|
|
281
|
+
#### βοΈ Options
|
|
282
|
+
|
|
283
|
+
| Option | Type | Default | Description |
|
|
284
|
+
| -------------- | ------------- | ----------------- | ----------------------------------------------------------------- |
|
|
285
|
+
| `element` | `HTMLElement` | `document.body` | The element to which the visibility classes will be applied |
|
|
286
|
+
| `hiddenClass` | `string` | `'windowHidden'` | Class name to apply when the window is **not visible or blurred** |
|
|
287
|
+
| `visibleClass` | `string` | `'windowVisible'` | Class name to apply when the window is **visible or focused** |
|
|
288
|
+
|
|
289
|
+
---
|
|
290
|
+
|
|
291
|
+
#### π Return Value
|
|
292
|
+
|
|
293
|
+
```ts
|
|
294
|
+
() => void
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
Returns a function that, when called, will:
|
|
298
|
+
|
|
299
|
+
* π§Ή Remove all attached event listeners
|
|
300
|
+
* β Remove both visibility classes from the target element
|
|
301
|
+
|
|
302
|
+
---
|
|
303
|
+
|
|
304
|
+
#### π¦ Events Supported
|
|
305
|
+
|
|
306
|
+
The script handles multiple events depending on browser support:
|
|
307
|
+
|
|
308
|
+
* `visibilitychange`, `webkitvisibilitychange`, `mozvisibilitychange`, `msvisibilitychange`
|
|
309
|
+
* `focus`, `blur`, `focusin`, `focusout`
|
|
310
|
+
* `pageshow`, `pagehide`
|
|
311
|
+
* IE fallback: `onfocusin`, `onfocusout`
|
|
312
|
+
|
|
313
|
+
---
|
|
314
|
+
|
|
315
|
+
#### π Initial Trigger
|
|
316
|
+
|
|
317
|
+
Immediately after installation, the script simulates a `focus` or `blur` event based on the current visibility state to **ensure the classes are applied from the start**.
|
|
318
|
+
|
|
319
|
+
---
|
|
320
|
+
|
|
321
|
+
#### π§― Uninstalling
|
|
322
|
+
|
|
323
|
+
Donβt forget to call the returned function if you dynamically load/unload components or scripts:
|
|
324
|
+
|
|
325
|
+
```js
|
|
326
|
+
const stopWatching = installWindowHiddenScript(...);
|
|
327
|
+
stopWatching(); // later
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
---
|
|
331
|
+
|
|
332
|
+
#### π¨ CSS Example
|
|
333
|
+
|
|
334
|
+
```css
|
|
335
|
+
.windowVisible {
|
|
336
|
+
opacity: 1;
|
|
337
|
+
pointer-events: auto;
|
|
338
|
+
transition: opacity 0.3s ease;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
.windowHidden {
|
|
342
|
+
opacity: 0.4;
|
|
343
|
+
pointer-events: none;
|
|
344
|
+
transition: opacity 0.3s ease;
|
|
345
|
+
}
|
|
346
|
+
```
|
|
@@ -119,3 +119,31 @@ formatBytes(1073741824, 2, 'MB');
|
|
|
119
119
|
* **Formatting:** Converts bytes to the most appropriate unit (from 'Bytes' to 'YB') based on the byte value.
|
|
120
120
|
* **Max Unit:** The `maxUnit` parameter allows you to limit the highest unit for conversion. If not provided, it will convert all the way up to 'YB'.
|
|
121
121
|
* **Decimals:** The result can be customized with a specified number of decimal places for precision. If `decimals` is `null`, no rounding is applied, and the full precision value is returned.
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
### π’ `genFibonacciSeq()` β Custom Fibonacci Sequence Generator
|
|
126
|
+
|
|
127
|
+
π Flexible, combinable, and vector-friendly!
|
|
128
|
+
|
|
129
|
+
Generates a customizable Fibonacci-like sequence. You can use simple numbers or even 2D/ND vectors by passing a custom `combiner` function.
|
|
130
|
+
|
|
131
|
+
```js
|
|
132
|
+
genFibonacciSeq({
|
|
133
|
+
baseValues: [[0, 1], [1, 1]],
|
|
134
|
+
length: 10,
|
|
135
|
+
combiner: ([x1, y1], [x2, y2]) => [x1 + x2, y1 + y2]
|
|
136
|
+
});
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
#### βοΈ Options
|
|
140
|
+
|
|
141
|
+
* `baseValues` π§¬: Two starting values (default: `[0, 1]`)
|
|
142
|
+
* `length` π: How many items to generate (default: `10`)
|
|
143
|
+
* `combiner` π§ͺ: Custom function to generate next value from the last two (default: `(a, b) => a + b`)
|
|
144
|
+
|
|
145
|
+
#### π§ Returns
|
|
146
|
+
|
|
147
|
+
An array representing the generated sequence.
|
|
148
|
+
|
|
149
|
+
> π **Beta** β Still experimental and fun to tweak!
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tiny-essentials",
|
|
3
|
-
"version": "1.13.
|
|
3
|
+
"version": "1.13.2",
|
|
4
4
|
"description": "Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "npm run test:mjs && npm run test:cjs && npm run test:js",
|