dphelper 3.7.1 → 3.7.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/docs/README.md +385 -0
- package/docs/SUMMARY.md +83 -0
- package/docs/_config.yml +1 -0
- package/docs/markdown/ai.md +345 -0
- package/docs/markdown/anchor.md +156 -0
- package/docs/markdown/array.md +208 -0
- package/docs/markdown/audio.md +113 -0
- package/docs/markdown/avoid.md +53 -0
- package/docs/markdown/biometric.md +338 -0
- package/docs/markdown/browser.md +203 -0
- package/docs/markdown/check.md +65 -0
- package/docs/markdown/color.md +159 -0
- package/docs/markdown/compress.md +310 -0
- package/docs/markdown/cookie.md +115 -0
- package/docs/markdown/coords.md +127 -0
- package/docs/markdown/credits.md +56 -0
- package/docs/markdown/date.md +260 -0
- package/docs/markdown/disable.md +109 -0
- package/docs/markdown/dispatch.md +108 -0
- package/docs/markdown/element.md +53 -0
- package/docs/markdown/event.md +85 -0
- package/docs/markdown/fetch.md +122 -0
- package/docs/markdown/form.md +302 -0
- package/docs/markdown/format.md +122 -0
- package/docs/markdown/i18n.md +292 -0
- package/docs/markdown/image.md +298 -0
- package/docs/markdown/json.md +269 -0
- package/docs/markdown/load.md +133 -0
- package/docs/markdown/logging.md +99 -0
- package/docs/markdown/math.md +172 -0
- package/docs/markdown/memory.md +85 -0
- package/docs/markdown/navigation.md +152 -0
- package/docs/markdown/net.md +60 -0
- package/docs/markdown/obj.md +242 -0
- package/docs/markdown/path.md +46 -0
- package/docs/markdown/promise.md +94 -0
- package/docs/markdown/sanitize.md +118 -0
- package/docs/markdown/screen.md +78 -0
- package/docs/markdown/scrollbar.md +82 -0
- package/docs/markdown/security.md +289 -0
- package/docs/markdown/shortcut.md +100 -0
- package/docs/markdown/socket.md +134 -0
- package/docs/markdown/sse.md +120 -0
- package/docs/markdown/svg.md +167 -0
- package/docs/markdown/sync.md +147 -0
- package/docs/markdown/system.md +78 -0
- package/docs/markdown/terminal.md +73 -0
- package/docs/markdown/text.md +245 -0
- package/docs/markdown/timer.md +98 -0
- package/docs/markdown/tools.md +111 -0
- package/docs/markdown/translators.md +65 -0
- package/docs/markdown/trigger.md +99 -0
- package/docs/markdown/triggers.md +133 -0
- package/docs/markdown/type.md +109 -0
- package/docs/markdown/types.md +102 -0
- package/docs/markdown/ui.md +45 -0
- package/docs/markdown/window.md +211 -0
- package/docs/markdown/worker.md +223 -0
- package/index.cjs +1 -1
- package/index.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
# json
|
|
2
|
+
|
|
3
|
+
JSON utilities for parsing, validation, conversion, and sanitization.
|
|
4
|
+
|
|
5
|
+
## Functions
|
|
6
|
+
|
|
7
|
+
| Function | Description | Example |
|
|
8
|
+
|----------|-------------|---------|
|
|
9
|
+
| `counter` | Count occurrences of key-value in JSON | `dphelper.json.counter(data, 'status', 'active')` |
|
|
10
|
+
| `toCsv` | Convert JSON array to CSV string | `dphelper.json.toCsv(users)` |
|
|
11
|
+
| `saveCsvAs` | Download JSON as CSV file | `dphelper.json.saveCsvAs(csvData, 'export')` |
|
|
12
|
+
| `is` | Check if string is valid JSON | `dphelper.json.is('{"key": "value"}')` |
|
|
13
|
+
| `parse` | Parse JSON string safely | `dphelper.json.parse('{"key": "value"}')` |
|
|
14
|
+
| `sanitize` | Sanitize malformed JSON string | `dphelper.json.sanitize(input)` |
|
|
15
|
+
| `sanitizeJsonValue` | Sanitize individual JSON values | `dphelper.json.sanitizeJsonValue('hello')` |
|
|
16
|
+
|
|
17
|
+
## Description
|
|
18
|
+
|
|
19
|
+
Complete JSON manipulation toolkit:
|
|
20
|
+
- **Validation** - Check if strings are valid JSON
|
|
21
|
+
- **Parsing** - Safe JSON parsing with error handling
|
|
22
|
+
- **Conversion** - JSON to CSV export
|
|
23
|
+
- **Sanitization** - Clean JSON strings, prevent injection
|
|
24
|
+
|
|
25
|
+
## Usage Examples
|
|
26
|
+
|
|
27
|
+
### JSON Validation
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
// Check if string is valid JSON
|
|
31
|
+
console.log(dphelper.json.is('{"name": "John", "age": 30}')); // true
|
|
32
|
+
console.log(dphelper.json.is('[1, 2, 3]')); // true
|
|
33
|
+
console.log(dphelper.json.is('not valid json')); // false
|
|
34
|
+
console.log(dphelper.json.is('')); // false
|
|
35
|
+
|
|
36
|
+
// Use for API response validation
|
|
37
|
+
function handleResponse(response) {
|
|
38
|
+
const text = await response.text();
|
|
39
|
+
if (!dphelper.json.is(text)) {
|
|
40
|
+
throw new Error('Invalid JSON response');
|
|
41
|
+
}
|
|
42
|
+
return JSON.parse(text);
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### JSON Parsing
|
|
47
|
+
|
|
48
|
+
```javascript
|
|
49
|
+
// Parse JSON safely
|
|
50
|
+
const obj = dphelper.json.parse('{"name": "John", "age": 30}');
|
|
51
|
+
console.log(obj); // { name: "John", age: 30 }
|
|
52
|
+
|
|
53
|
+
// Handles parsing errors gracefully
|
|
54
|
+
const bad = dphelper.json.parse('not json');
|
|
55
|
+
console.log(bad); // "Json not parsable"
|
|
56
|
+
|
|
57
|
+
// Use for user input
|
|
58
|
+
function parseUserInput(input) {
|
|
59
|
+
try {
|
|
60
|
+
return dphelper.json.parse(input);
|
|
61
|
+
} catch {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Counting JSON Properties
|
|
68
|
+
|
|
69
|
+
```javascript
|
|
70
|
+
const data = {
|
|
71
|
+
items: [
|
|
72
|
+
{ id: 1, status: 'active' },
|
|
73
|
+
{ id: 2, status: 'active' },
|
|
74
|
+
{ id: 3, status: 'inactive' },
|
|
75
|
+
{ id: 4, status: 'active' }
|
|
76
|
+
]
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// Count items with specific status
|
|
80
|
+
console.log(dphelper.json.counter(data, 'status', 'active')); // 3
|
|
81
|
+
console.log(dphelper.json.counter(data, 'status', 'inactive')); // 1
|
|
82
|
+
|
|
83
|
+
// Count total keys
|
|
84
|
+
console.log(dphelper.json.counter(data)); // 1 (the 'items' key)
|
|
85
|
+
|
|
86
|
+
// Works with nested arrays
|
|
87
|
+
const users = {
|
|
88
|
+
users: [
|
|
89
|
+
{ role: 'admin', active: true },
|
|
90
|
+
{ role: 'user', active: true },
|
|
91
|
+
{ role: 'user', active: false }
|
|
92
|
+
]
|
|
93
|
+
};
|
|
94
|
+
console.log(dphelper.json.counter(users, 'role', 'user')); // 2
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### JSON to CSV Conversion
|
|
98
|
+
|
|
99
|
+
```javascript
|
|
100
|
+
// Convert array of objects to CSV
|
|
101
|
+
const users = [
|
|
102
|
+
{ name: 'John', age: 30, city: 'NYC' },
|
|
103
|
+
{ name: 'Jane', age: 25, city: 'LA' },
|
|
104
|
+
{ name: 'Bob', age: 35, city: 'Chicago' }
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
const csv = dphelper.json.toCsv(users);
|
|
108
|
+
console.log(csv);
|
|
109
|
+
/*
|
|
110
|
+
name,age,city
|
|
111
|
+
John,30,NYC
|
|
112
|
+
Jane,25,LA
|
|
113
|
+
Bob,35,Chicago
|
|
114
|
+
*/
|
|
115
|
+
|
|
116
|
+
// Export API response to CSV
|
|
117
|
+
async function exportToCsv() {
|
|
118
|
+
const response = await fetch('/api/users');
|
|
119
|
+
const users = await response.json();
|
|
120
|
+
|
|
121
|
+
const csv = dphelper.json.toCsv(users);
|
|
122
|
+
dphelper.json.saveCsvAs(csv, 'users-export');
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Save CSV as File
|
|
127
|
+
|
|
128
|
+
```javascript
|
|
129
|
+
// Download CSV data as file
|
|
130
|
+
const data = [
|
|
131
|
+
{ product: 'Apple', price: 1.99, quantity: 100 },
|
|
132
|
+
{ product: 'Banana', price: 0.59, quantity: 200 }
|
|
133
|
+
];
|
|
134
|
+
|
|
135
|
+
const csv = dphelper.json.toCsv(data);
|
|
136
|
+
dphelper.json.saveCsvAs(csv, 'products');
|
|
137
|
+
|
|
138
|
+
// Filename will include timestamp: "products_20260302120000.csv"
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### JSON Sanitization
|
|
142
|
+
|
|
143
|
+
```javascript
|
|
144
|
+
// Sanitize malformed JSON
|
|
145
|
+
const dirty = '{ "name": "John", "age": 30 }';
|
|
146
|
+
console.log(dphelper.json.sanitize(dirty));
|
|
147
|
+
// '{ "name":"John","age":30}'
|
|
148
|
+
|
|
149
|
+
// Clean up spacing issues
|
|
150
|
+
const messy = '{ "key" : "value" }';
|
|
151
|
+
console.log(dphelper.json.sanitize(messy));
|
|
152
|
+
// '{"key":"value"}'
|
|
153
|
+
|
|
154
|
+
// Handles various data types
|
|
155
|
+
const mixed = '{ "string": "hello", "number": 42, "bool": true, "null": null }';
|
|
156
|
+
console.log(dphelper.json.sanitize(mixed));
|
|
157
|
+
// '{"string":"hello","number":42,"bool":true,"null":""}'
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Value Sanitization
|
|
161
|
+
|
|
162
|
+
```javascript
|
|
163
|
+
// Sanitize individual JSON values
|
|
164
|
+
console.log(dphelper.json.sanitizeJsonValue('hello')); // '"hello"'
|
|
165
|
+
console.log(dphelper.json.sanitizeJsonValue('hello world')); // '"hello world"'
|
|
166
|
+
console.log(dphelper.json.sanitizeJsonValue('test"quote')); // '"test"quote"'
|
|
167
|
+
console.log(dphelper.json.sanitizeJsonValue('line1\nline2')); // '"line1\nline2"'
|
|
168
|
+
console.log(dphelper.json.sanitizeJsonValue('tab\there')); // '"tab here"'
|
|
169
|
+
|
|
170
|
+
// Use for building JSON manually
|
|
171
|
+
function safeJsonString(key, value) {
|
|
172
|
+
const safeValue = dphelper.json.sanitizeJsonValue(value);
|
|
173
|
+
return `"${key}":${safeValue}`;
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Advanced Usage
|
|
178
|
+
|
|
179
|
+
### Complete Data Export
|
|
180
|
+
|
|
181
|
+
```javascript
|
|
182
|
+
class DataExporter {
|
|
183
|
+
constructor() {}
|
|
184
|
+
|
|
185
|
+
exportToCsv(data, filename) {
|
|
186
|
+
if (!Array.isArray(data) || data.length === 0) {
|
|
187
|
+
console.error('Data must be a non-empty array');
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const csv = dphelper.json.toCsv(data);
|
|
192
|
+
dphelper.json.saveCsvAs(csv, filename);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
exportTableToCsv(tableId, filename) {
|
|
196
|
+
const rows = document.querySelectorAll(`#${tableId} tr`);
|
|
197
|
+
const data = Array.from(rows).map(row => {
|
|
198
|
+
return Array.from(row.querySelectorAll('td')).map(td => td.textContent);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
this.exportToCsv(data, filename);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Usage
|
|
206
|
+
const exporter = new DataExporter();
|
|
207
|
+
exporter.exportToCsv(userList, 'users');
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### JSON Validation Middleware
|
|
211
|
+
|
|
212
|
+
```javascript
|
|
213
|
+
function validateJsonMiddleware(req, res, next) {
|
|
214
|
+
let body = '';
|
|
215
|
+
|
|
216
|
+
req.on('data', chunk => {
|
|
217
|
+
body += chunk.toString();
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
req.on('end', () => {
|
|
221
|
+
if (!dphelper.json.is(body)) {
|
|
222
|
+
return res.status(400).json({ error: 'Invalid JSON' });
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
try {
|
|
226
|
+
req.body = dphelper.json.parse(body);
|
|
227
|
+
next();
|
|
228
|
+
} catch (err) {
|
|
229
|
+
return res.status(400).json({ error: 'JSON parse error' });
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### API Response Handler
|
|
236
|
+
|
|
237
|
+
```javascript
|
|
238
|
+
async function fetchJson(url) {
|
|
239
|
+
const response = await fetch(url);
|
|
240
|
+
|
|
241
|
+
if (!response.ok) {
|
|
242
|
+
throw new Error(`HTTP ${response.status}`);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const text = await response.text();
|
|
246
|
+
|
|
247
|
+
if (!dphelper.json.is(text)) {
|
|
248
|
+
throw new Error('Response is not valid JSON');
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return dphelper.json.parse(text);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Usage
|
|
255
|
+
const data = await fetchJson('/api/users');
|
|
256
|
+
console.log(data);
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
## Details
|
|
260
|
+
|
|
261
|
+
- **Author:** Dario Passariello
|
|
262
|
+
- **Version:** 0.0.2
|
|
263
|
+
- **Creation Date:** 20210101
|
|
264
|
+
- **Last Modified:** 20260220
|
|
265
|
+
- **Environment:** Works in both client and server environments
|
|
266
|
+
|
|
267
|
+
---
|
|
268
|
+
|
|
269
|
+
*Automatically generated document*
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# load
|
|
2
|
+
|
|
3
|
+
Dynamic file and script loading utilities for managing modules, components, and resources.
|
|
4
|
+
|
|
5
|
+
## Functions
|
|
6
|
+
|
|
7
|
+
| Function | Description | Example |
|
|
8
|
+
|----------|-------------|---------|
|
|
9
|
+
| `all` | Load all files from glob object | `dphelper.load.all(import.meta.glob('./**/*.ts'))` |
|
|
10
|
+
| `file` | Load a specific file | `dphelper.load.file(path)` |
|
|
11
|
+
| `fileToElement` | Load file content into element | `dphelper.load.fileToElement('#target', '/content.html')` |
|
|
12
|
+
| `script` | Dynamically load scripts | `dphelper.load.script(['/app.js'])` |
|
|
13
|
+
| `toJson` | Create JSON from folder glob | `dphelper.load.toJson(glob, 'routes')` |
|
|
14
|
+
|
|
15
|
+
## Description
|
|
16
|
+
|
|
17
|
+
Dynamic loading utilities:
|
|
18
|
+
- **Module Loading** - Load ES modules via Vite/esbuild
|
|
19
|
+
- **File Loading** - Fetch and inject content
|
|
20
|
+
- **Script Loading** - Dynamic script injection
|
|
21
|
+
- **Caching** - Optional result caching
|
|
22
|
+
|
|
23
|
+
## Usage Examples
|
|
24
|
+
|
|
25
|
+
### Load All Modules (Vite)
|
|
26
|
+
|
|
27
|
+
```javascript
|
|
28
|
+
// Load all TypeScript modules from folder
|
|
29
|
+
dphelper.load.all(
|
|
30
|
+
import.meta.glob('./components/*.ts', { eager: true }),
|
|
31
|
+
'components'
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
// Components are now loaded and cached
|
|
35
|
+
console.log(window.components); // ['./components/Button.ts', ...]
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Load File to Element
|
|
39
|
+
|
|
40
|
+
```javascript
|
|
41
|
+
// Load HTML content into element
|
|
42
|
+
await dphelper.load.fileToElement('#content', '/partials/home.html');
|
|
43
|
+
|
|
44
|
+
// Loads home.html and inserts into #content
|
|
45
|
+
// Content is sanitized for XSS protection
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Dynamic Script Loading
|
|
49
|
+
|
|
50
|
+
```javascript
|
|
51
|
+
// Load multiple scripts
|
|
52
|
+
dphelper.load.script([
|
|
53
|
+
'https://cdn.example.com/lib1.js',
|
|
54
|
+
'https://cdn.example.com/lib2.js'
|
|
55
|
+
], 'body');
|
|
56
|
+
|
|
57
|
+
// Scripts load asynchronously
|
|
58
|
+
// Appended to specified element (default: html)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Create JSON from Glob
|
|
62
|
+
|
|
63
|
+
```javascript
|
|
64
|
+
// Create route registry from folder
|
|
65
|
+
dphelper.load.toJson(
|
|
66
|
+
import.meta.glob('./routes/*.ts'),
|
|
67
|
+
'routes'
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
// Creates window.routes as JSON object
|
|
71
|
+
// Keys are file paths, values are modules
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Advanced Usage
|
|
75
|
+
|
|
76
|
+
### Component Lazy Loading
|
|
77
|
+
|
|
78
|
+
```javascript
|
|
79
|
+
// Load components on demand
|
|
80
|
+
async function loadComponent(name) {
|
|
81
|
+
const modules = import.meta.glob('./components/*.vue');
|
|
82
|
+
const loader = modules[`./components/${name}.vue`];
|
|
83
|
+
|
|
84
|
+
if (loader) {
|
|
85
|
+
return await loader();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
throw new Error(`Component ${name} not found`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Usage
|
|
92
|
+
const Button = await loadComponent('Button');
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Dynamic Module Loading
|
|
96
|
+
|
|
97
|
+
```javascript
|
|
98
|
+
class ModuleLoader {
|
|
99
|
+
constructor(basePath) {
|
|
100
|
+
this.basePath = basePath;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async load(moduleName) {
|
|
104
|
+
const modules = import.meta.glob('./modules/*.js');
|
|
105
|
+
const key = `./modules/${moduleName}.js`;
|
|
106
|
+
|
|
107
|
+
if (modules[key]) {
|
|
108
|
+
return await modules[key]();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
loadAll() {
|
|
115
|
+
dphelper.load.all(
|
|
116
|
+
import.meta.glob('./modules/*.js', { eager: true }),
|
|
117
|
+
'allModules'
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Details
|
|
124
|
+
|
|
125
|
+
- **Author:** Dario Passariello
|
|
126
|
+
- **Version:** 0.0.3
|
|
127
|
+
- **Creation Date:** 20210101
|
|
128
|
+
- **Last Modified:** 20260228
|
|
129
|
+
- **Environment:** Client-side only (browser)
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
*Automatically generated document*
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# logging
|
|
2
|
+
|
|
3
|
+
Advanced logging system.
|
|
4
|
+
|
|
5
|
+
## Functions
|
|
6
|
+
|
|
7
|
+
| Function | Description | Example |
|
|
8
|
+
|----------|-------------|---------|
|
|
9
|
+
| `list` | Get all stored logs | `dphelper.log.list()` |
|
|
10
|
+
| `reg` | Log regular message | `dphelper.log.reg(message)` |
|
|
11
|
+
| `debug` | Log debug message | `dphelper.log.debug(message)` |
|
|
12
|
+
| `error` | Log error message | `dphelper.log.error(message)` |
|
|
13
|
+
|
|
14
|
+
## Description
|
|
15
|
+
|
|
16
|
+
Application logging system:
|
|
17
|
+
- **Multiple Levels** - Regular, debug, and error logging
|
|
18
|
+
- **Log Storage** - Keep history of all logged messages
|
|
19
|
+
- **Retrieval** - Access logs anytime with list()
|
|
20
|
+
- **Development** - Useful for debugging and monitoring
|
|
21
|
+
|
|
22
|
+
## Usage Examples
|
|
23
|
+
|
|
24
|
+
### Basic Logging
|
|
25
|
+
|
|
26
|
+
```javascript
|
|
27
|
+
// Regular log
|
|
28
|
+
dphelper.log.reg('Application started');
|
|
29
|
+
dphelper.log.reg('User logged in');
|
|
30
|
+
|
|
31
|
+
// Debug log
|
|
32
|
+
dphelper.log.debug('Variable value:', someVariable);
|
|
33
|
+
dphelper.log.debug('API Response:', response);
|
|
34
|
+
|
|
35
|
+
// Error log
|
|
36
|
+
dphelper.log.error('Failed to fetch data');
|
|
37
|
+
dphelper.log.error('Invalid input:', error);
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Retrieve Logs
|
|
41
|
+
|
|
42
|
+
```javascript
|
|
43
|
+
// Get all logs
|
|
44
|
+
const allLogs = dphelper.log.list();
|
|
45
|
+
// Returns array of all logged messages
|
|
46
|
+
|
|
47
|
+
// Display in console
|
|
48
|
+
console.table(dphelper.log.list());
|
|
49
|
+
|
|
50
|
+
// Filter by type
|
|
51
|
+
const logs = dphelper.log.list();
|
|
52
|
+
const errors = logs.filter(l => l.type === 'error');
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Debugging Application
|
|
56
|
+
|
|
57
|
+
```javascript
|
|
58
|
+
// Track function execution
|
|
59
|
+
function processData(data) {
|
|
60
|
+
dphelper.log.debug('Processing started', data);
|
|
61
|
+
|
|
62
|
+
try {
|
|
63
|
+
const result = transform(data);
|
|
64
|
+
dphelper.log.debug('Processing complete', result);
|
|
65
|
+
return result;
|
|
66
|
+
} catch (err) {
|
|
67
|
+
dphelper.log.error('Processing failed', err);
|
|
68
|
+
throw err;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Error Tracking
|
|
74
|
+
|
|
75
|
+
```javascript
|
|
76
|
+
// API error handling
|
|
77
|
+
async function fetchData(url) {
|
|
78
|
+
try {
|
|
79
|
+
const response = await fetch(url);
|
|
80
|
+
dphelper.log.reg(`Fetched: ${url}`);
|
|
81
|
+
return response.json();
|
|
82
|
+
} catch (err) {
|
|
83
|
+
dphelper.log.error(`Failed to fetch ${url}`, err);
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Details
|
|
90
|
+
|
|
91
|
+
- **Author:** Dario Passariello
|
|
92
|
+
- **Version:** 0.0.2
|
|
93
|
+
- **Creation Date:** 20210101
|
|
94
|
+
- **Last Modified:** 20260220
|
|
95
|
+
- **Environment:** both (browser + Node.js)
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
*Automatically generated document*
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# math
|
|
2
|
+
|
|
3
|
+
Advanced mathematical functions and utilities.
|
|
4
|
+
|
|
5
|
+
## Functions
|
|
6
|
+
|
|
7
|
+
| Function | Description | Example |
|
|
8
|
+
|----------|-------------|---------|
|
|
9
|
+
| `rnd` | Generate a random number | `dphelper.math.rnd()` |
|
|
10
|
+
| `tmr` | Get current epoch time in seconds | `dphelper.math.tmr()` |
|
|
11
|
+
| `isOdd` | Check if a number is odd | `dphelper.math.isOdd(a)` |
|
|
12
|
+
| `percent` | Calculate percentage (n/tot * 100) | `dphelper.math.percent(n, tot)` |
|
|
13
|
+
| `isPrime` | Check if a number is prime | `dphelper.math.isPrime(n)` |
|
|
14
|
+
|
|
15
|
+
## Description
|
|
16
|
+
|
|
17
|
+
Mathematical utility functions:
|
|
18
|
+
- **Random** - Generate random numbers based on time
|
|
19
|
+
- **Time** - Get epoch timestamp in seconds
|
|
20
|
+
- **Number Tests** - Check odd/even, prime numbers
|
|
21
|
+
- **Percentage** - Calculate percentage of value relative to total
|
|
22
|
+
|
|
23
|
+
## Usage Examples
|
|
24
|
+
|
|
25
|
+
### Random Number Generation
|
|
26
|
+
|
|
27
|
+
```javascript
|
|
28
|
+
// Generate random number
|
|
29
|
+
const randomNum = dphelper.math.rnd();
|
|
30
|
+
// Returns a random number (typically 6 digits)
|
|
31
|
+
|
|
32
|
+
// Use in unique ID generation
|
|
33
|
+
function generateId() {
|
|
34
|
+
return `id_${dphelper.math.rnd()}_${Date.now()}`;
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Time Measurement
|
|
39
|
+
|
|
40
|
+
```javascript
|
|
41
|
+
// Get current epoch in seconds
|
|
42
|
+
const timestamp = dphelper.math.tmr();
|
|
43
|
+
// Returns current Unix timestamp (seconds)
|
|
44
|
+
|
|
45
|
+
// Measure function execution time
|
|
46
|
+
function myFunction() {
|
|
47
|
+
// ... some operations
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const start = dphelper.math.tmr();
|
|
51
|
+
myFunction();
|
|
52
|
+
const end = dphelper.math.tmr();
|
|
53
|
+
console.log(`Execution took ${end - start} seconds`);
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Odd/Even Detection
|
|
57
|
+
|
|
58
|
+
```javascript
|
|
59
|
+
// Check if number is odd
|
|
60
|
+
console.log(dphelper.math.isOdd(5)); // true
|
|
61
|
+
console.log(dphelper.math.isOdd(4)); // false
|
|
62
|
+
console.log(dphelper.math.isOdd(0)); // false
|
|
63
|
+
console.log(dphelper.math.isOdd(-3)); // true
|
|
64
|
+
|
|
65
|
+
// Use in filtering
|
|
66
|
+
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
|
67
|
+
const oddNumbers = numbers.filter(n => dphelper.math.isOdd(n));
|
|
68
|
+
// [1, 3, 5, 7, 9]
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Percentage Calculation
|
|
72
|
+
|
|
73
|
+
```javascript
|
|
74
|
+
// Basic percentage
|
|
75
|
+
const percent = dphelper.math.percent(25, 100);
|
|
76
|
+
// Output: 25
|
|
77
|
+
|
|
78
|
+
// Calculate progress
|
|
79
|
+
const progress = dphelper.math.percent(45, 100);
|
|
80
|
+
console.log(`${progress}% complete`);
|
|
81
|
+
// 45% complete
|
|
82
|
+
|
|
83
|
+
// Sales commission
|
|
84
|
+
function calculateCommission(sales, target) {
|
|
85
|
+
return dphelper.math.percent(sales, target);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
console.log(calculateCommission(50000, 100000)); // 50%
|
|
89
|
+
|
|
90
|
+
// Vote percentage
|
|
91
|
+
const votes = { yes: 450, no: 50, abstained: 25 };
|
|
92
|
+
const total = votes.yes + votes.no + votes.abstained;
|
|
93
|
+
|
|
94
|
+
console.log(`Yes: ${dphelper.math.percent(votes.yes, total)}%`);
|
|
95
|
+
console.log(`No: ${dphelper.math.percent(votes.no, total)}%`);
|
|
96
|
+
// Yes: 81.8%
|
|
97
|
+
// No: 9.1%
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Prime Number Check
|
|
101
|
+
|
|
102
|
+
```javascript
|
|
103
|
+
// Check if number is prime
|
|
104
|
+
console.log(dphelper.math.isPrime(7)); // true
|
|
105
|
+
console.log(dphelper.math.isPrime(4)); // false
|
|
106
|
+
console.log(dphelper.math.isPrime(1)); // false
|
|
107
|
+
console.log(dphelper.math.isPrime(2)); // true
|
|
108
|
+
console.log(dphelper.math.isPrime(17)); // true
|
|
109
|
+
console.log(dphelper.math.isPrime(18)); // false
|
|
110
|
+
|
|
111
|
+
// Find prime numbers in range
|
|
112
|
+
function findPrimes(min, max) {
|
|
113
|
+
const primes = [];
|
|
114
|
+
for (let i = min; i <= max; i++) {
|
|
115
|
+
if (dphelper.math.isPrime(i)) {
|
|
116
|
+
primes.push(i);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return primes;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
console.log(findPrimes(1, 20));
|
|
123
|
+
// [2, 3, 5, 7, 11, 13, 17, 19]
|
|
124
|
+
|
|
125
|
+
// Use in validation
|
|
126
|
+
function isValidPrimeInput(n) {
|
|
127
|
+
return Number.isInteger(n) && n > 0 && dphelper.math.isPrime(n);
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Combined Math Operations
|
|
132
|
+
|
|
133
|
+
```javascript
|
|
134
|
+
class MathUtils {
|
|
135
|
+
// Calculate discount percentage
|
|
136
|
+
static discountPercentage(original, sale) {
|
|
137
|
+
const percent = dphelper.math.percent(sale, original);
|
|
138
|
+
return Math.round(100 - percent);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Check if number is odd prime
|
|
142
|
+
static isOddPrime(n) {
|
|
143
|
+
return dphelper.math.isOdd(n) && dphelper.math.isPrime(n);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Generate random odd number
|
|
147
|
+
static randomOdd(min = 1, max = 100) {
|
|
148
|
+
let num;
|
|
149
|
+
do {
|
|
150
|
+
num = Math.floor(Math.random() * (max - min + 1)) + min;
|
|
151
|
+
} while (!dphelper.math.isOdd(num));
|
|
152
|
+
return num;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
console.log(MathUtils.discountPercentage(100, 75)); // 25
|
|
157
|
+
console.log(MathUtils.isOddPrime(7)); // true
|
|
158
|
+
console.log(MathUtils.isOddPrime(2)); // false
|
|
159
|
+
console.log(MathUtils.randomOdd(1, 50)); // Random odd number
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Details
|
|
163
|
+
|
|
164
|
+
- **Author:** Dario Passariello
|
|
165
|
+
- **Version:** 0.0.2
|
|
166
|
+
- **Creation Date:** 20210101
|
|
167
|
+
- **Last Modified:** 20260220
|
|
168
|
+
- **Environment:** both (browser + Node.js)
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
*Automatically generated document*
|