tiny-essentials 1.28.16 → 1.28.17
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/changelog/1/28/17.md +11 -0
- package/dist/v1/TinyBasicsEs.min.js +1 -1
- package/dist/v1/TinyEssentials.min.js +1 -1
- package/dist/v1/TinyMediaPlayer.min.js +1 -1
- package/dist/v1/TinyRadioFm.min.js +1 -1
- package/dist/v1/basics/index.cjs +4 -0
- package/dist/v1/basics/index.d.mts +2 -1
- package/dist/v1/basics/index.d.mts.map +1 -1
- package/dist/v1/basics/index.mjs +2 -1
- package/dist/v1/basics/jsonFilter.cjs +267 -0
- package/dist/v1/basics/jsonFilter.d.mts +91 -0
- package/dist/v1/basics/jsonFilter.d.mts.map +1 -0
- package/dist/v1/basics/jsonFilter.mjs +237 -0
- package/dist/v1/basics/mediaContent.cjs +96 -104
- package/dist/v1/basics/mediaContent.d.mts.map +1 -1
- package/dist/v1/basics/mediaContent.mjs +94 -102
- package/dist/v1/index.cjs +4 -0
- package/dist/v1/index.d.mts +2 -1
- package/dist/v1/index.d.mts.map +1 -1
- package/dist/v1/index.mjs +2 -1
- package/docs/v1/Exports.md +3 -2
- package/docs/v1/README.md +2 -1
- package/docs/v1/basics/jsonFilter.md +194 -0
- package/package.json +5 -1
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# 🛠️ Object Filtering Utilities Documentation
|
|
2
|
+
|
|
3
|
+
A collection of powerful, high-precision JavaScript utilities designed to filter objects based on keys, values, or complex nested conditions.
|
|
4
|
+
|
|
5
|
+
## 📋 Table of Contents
|
|
6
|
+
1. [`jsonFilter`](#jsonfilter) — Shallow filtering.
|
|
7
|
+
2. [`jsonFilterRecursive`](#jsonfilterrecursive) — Deep/Recursive filtering.
|
|
8
|
+
3. [`jsonFilterByKeys`](#jsonfilterbykeys) — Selective key/value extraction.
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## 🔍 `jsonFilter`
|
|
13
|
+
|
|
14
|
+
`jsonFilter` is a shallow filtering function. It creates a new object containing only the entries that satisfy a specific predicate function.
|
|
15
|
+
|
|
16
|
+
### ⚙️ Syntax
|
|
17
|
+
```javascript
|
|
18
|
+
jsonFilter(value, filterContent);
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### 📥 Parameters
|
|
22
|
+
| Parameter | Type | Description |
|
|
23
|
+
| :--- | :--- | :--- |
|
|
24
|
+
| `value` | `Object` | The source object to be filtered. |
|
|
25
|
+
| `filterContent` | `Function` | A callback: `(entry, index, array) => boolean`. |
|
|
26
|
+
|
|
27
|
+
### 🚀 Example
|
|
28
|
+
Filtering an object to only keep entries where the value is a number greater than 10.
|
|
29
|
+
|
|
30
|
+
```javascript
|
|
31
|
+
import { jsonFilter } from 'tiny-essentials/basics/jsonFilter';
|
|
32
|
+
|
|
33
|
+
const data = { a: 5, b: 15, c: 20, d: 'hello' };
|
|
34
|
+
|
|
35
|
+
const result = jsonFilter(data, ([key, value]) => typeof value === 'number' && value > 10);
|
|
36
|
+
|
|
37
|
+
console.log(result);
|
|
38
|
+
// Output: { b: 15, c: 20 }
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## 🌳 `jsonFilterRecursive`
|
|
44
|
+
|
|
45
|
+
`jsonFilterRecursive` performs a deep traversal of a data structure. It allows you to apply specific filtering rules depending on whether the current item is a **primitive**, an **array**, or a **complex container** (Object, Map, or Set).
|
|
46
|
+
|
|
47
|
+
### ⚙️ Syntax
|
|
48
|
+
```javascript
|
|
49
|
+
jsonFilterRecursive(value, filter);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 📥 Parameters
|
|
53
|
+
| Parameter | Type | Description |
|
|
54
|
+
| :--- | :--- | :--- |
|
|
55
|
+
| `value` | `Object \| Map \| Set` | The source structure to be filtered. |
|
|
56
|
+
| `filter` | `Object` | An object containing predicate functions: |
|
|
57
|
+
| `filter.value` | `Function` | Applied to **primitive** values. |
|
|
58
|
+
| `filter.obj` | `Function` | Applied to **nested objects, Maps, or Sets** (used for pruning). |
|
|
59
|
+
| `filter.array` | `Function` | Applied to **arrays**. |
|
|
60
|
+
|
|
61
|
+
> **Note:** `WeakMap` and `WeakSet` are supported but, due to their non-iterable nature, will return a new empty instance of the same type when processed.
|
|
62
|
+
|
|
63
|
+
### 🚀 Example
|
|
64
|
+
Filtering a complex structure containing `Map` and `Set` objects.
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
import { jsonFilterRecursive } from 'tiny-essentials/basics/jsonFilter';
|
|
68
|
+
|
|
69
|
+
const complexData = {
|
|
70
|
+
id: 'Yasmin',
|
|
71
|
+
food: ['pudding'],
|
|
72
|
+
tags: new Set(['premium', 'beta-tester']),
|
|
73
|
+
empty: new Set(),
|
|
74
|
+
metadata: new Map([
|
|
75
|
+
['last_login', '2023-10-27'],
|
|
76
|
+
['login_count', 42]
|
|
77
|
+
]),
|
|
78
|
+
settings: {
|
|
79
|
+
theme: 'dark',
|
|
80
|
+
notifications: true
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const filterConfig = {
|
|
85
|
+
value: ([key, val]) => typeof val === 'string', // Keep strings
|
|
86
|
+
|
|
87
|
+
// Prune Maps or Sets if they become empty after filtering
|
|
88
|
+
obj: (container) => {
|
|
89
|
+
if (container instanceof Map || container instanceof Set) {
|
|
90
|
+
return container.size > 0;
|
|
91
|
+
}
|
|
92
|
+
return container.id !== undefined; // Prune objects that don't have an 'id'
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
array: () => true // Keep all arrays
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const result = jsonFilterRecursive(complexData, filterConfig);
|
|
99
|
+
|
|
100
|
+
console.log(result);
|
|
101
|
+
/*
|
|
102
|
+
Output:
|
|
103
|
+
{
|
|
104
|
+
food: ['pudding'],
|
|
105
|
+
id: 'Yasmin',
|
|
106
|
+
tags: Set { 'premium', 'beta-tester' },
|
|
107
|
+
metadata: Map { 'last_login' => '2023-10-27' }
|
|
108
|
+
}
|
|
109
|
+
*/
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## 🔑 `jsonFilterByKeys`
|
|
115
|
+
|
|
116
|
+
`jsonFilterByKeys` is a highly specialized selector. It allows you to extract specific keys or key-value pairs using simple strings, tuples, or validation functions.
|
|
117
|
+
|
|
118
|
+
### ⚙️ Syntax
|
|
119
|
+
```javascript
|
|
120
|
+
jsonFilterByKeys(item, keys, values);
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### 📥 Parameters
|
|
124
|
+
| Parameter | Type | Description |
|
|
125
|
+
| :--- | :--- | :--- |
|
|
126
|
+
| `item` | `Object` | The source object. |
|
|
127
|
+
| `keys` | `Array` | An array of `string` keys OR `[key, validator/value]` tuples. |
|
|
128
|
+
| `values` | `Array` (opt) | A list of allowed values or validators to apply to the keys. |
|
|
129
|
+
|
|
130
|
+
### 💡 Usage Modes
|
|
131
|
+
|
|
132
|
+
#### 1. Using Simple Keys + Value Whitelist
|
|
133
|
+
If you provide a list of keys and a list of allowed values.
|
|
134
|
+
|
|
135
|
+
```javascript
|
|
136
|
+
import { jsonFilterByKeys } from 'tiny-essentials/basics/jsonFilter';
|
|
137
|
+
|
|
138
|
+
const data = { status: 'active', type: 'user', age: 25 };
|
|
139
|
+
// Keep only 'status' if it is 'active'
|
|
140
|
+
const result = jsonFilterByKeys(data, ['status'], ['active']);
|
|
141
|
+
// Output: { status: 'active' }
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
#### 2. Using Key-Value Tuples (Highly Precise)
|
|
145
|
+
You can pass a tuple `[key, condition]` where the condition is a value or a function.
|
|
146
|
+
|
|
147
|
+
```javascript
|
|
148
|
+
import { jsonFilterByKeys } from 'tiny-essentials/basics/jsonFilter';
|
|
149
|
+
|
|
150
|
+
const data = {
|
|
151
|
+
id: 101,
|
|
152
|
+
role: 'admin',
|
|
153
|
+
score: 95,
|
|
154
|
+
enabled: true
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
// Keep 'id', 'score', and 'role'.
|
|
158
|
+
const result = jsonFilterByKeys(data, [
|
|
159
|
+
'id',
|
|
160
|
+
['role', 'admin'],
|
|
161
|
+
['score', (value) => value > 90]
|
|
162
|
+
]);
|
|
163
|
+
|
|
164
|
+
console.log(result);
|
|
165
|
+
// Output: { id: 101, role: 'admin', score: 95 }
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
#### 3. Using Validators
|
|
169
|
+
You can pass functions to perform complex validation during selection.
|
|
170
|
+
|
|
171
|
+
```javascript
|
|
172
|
+
import { jsonFilterByKeys } from 'tiny-essentials/basics/jsonFilter';
|
|
173
|
+
|
|
174
|
+
const data = {
|
|
175
|
+
name: 'Isabela',
|
|
176
|
+
age: 30,
|
|
177
|
+
score: 9.5
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
// Keep keys where the value is a number greater than 10
|
|
181
|
+
const result = jsonFilterByKeys(data, ['age', 'score'], [(v) => typeof v === 'number' && v > 10]);
|
|
182
|
+
|
|
183
|
+
console.log(result);
|
|
184
|
+
// Output: { age: 30 }
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
---
|
|
188
|
+
|
|
189
|
+
## ⚠️ Error Handling
|
|
190
|
+
|
|
191
|
+
All functions implement strict runtime validation and will throw a `TypeError` if:
|
|
192
|
+
- The input `value` is not a valid object.
|
|
193
|
+
- The `filterContent` or `filter` predicates are not functions.
|
|
194
|
+
- The `keys` or `values` arguments are not arrays when required.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tiny-essentials",
|
|
3
|
-
"version": "1.28.
|
|
3
|
+
"version": "1.28.17",
|
|
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
|
"bin": {
|
|
6
6
|
"tiny-essentials-fork": "./TinyFork.mjs"
|
|
@@ -114,6 +114,10 @@
|
|
|
114
114
|
"require": "./dist/v1/basics/browserDetector.cjs",
|
|
115
115
|
"import": "./dist/v1/basics/browserDetector.mjs"
|
|
116
116
|
},
|
|
117
|
+
"./basics/jsonFilter": {
|
|
118
|
+
"require": "./dist/v1/basics/jsonFilter.cjs",
|
|
119
|
+
"import": "./dist/v1/basics/jsonFilter.mjs"
|
|
120
|
+
},
|
|
117
121
|
"./fileManager": {
|
|
118
122
|
"require": "./dist/v1/fileManager/index.cjs",
|
|
119
123
|
"import": "./dist/v1/fileManager/index.mjs"
|