myio-js-library 0.1.0 โ 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/README.md +222 -3
- package/dist/index.cjs +425 -121
- package/dist/index.d.cts +184 -0
- package/dist/index.js +406 -120
- package/dist/myio-js-library.umd.js +1 -1
- package/dist/myio-js-library.umd.min.js +1 -1
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -14,6 +14,11 @@ Distributed as **ESM**, **CJS**, and **UMD** (with a pre-minified build for CDN
|
|
|
14
14
|
- ๐ฑ **Device detection** โ context-aware device type identification.
|
|
15
15
|
- ๐งฉ **String utilities** โ normalization helpers.
|
|
16
16
|
- ๐ข **Number utilities** โ safe fixed formatting, percentages.
|
|
17
|
+
- โก **Energy formatting** โ Brazilian locale energy unit formatting (kWh, MWh, GWh).
|
|
18
|
+
- ๐
**Date utilities** โ date formatting, interval detection, Sรฃo Paulo timezone handling.
|
|
19
|
+
- ๐ **CSV export** โ data export to CSV format with proper escaping (returns strings, no DOM manipulation).
|
|
20
|
+
- ๐ท๏ธ **Classification** โ energy entity classification utilities.
|
|
21
|
+
- ๐ **Data access** โ nested object value retrieval with datakey paths.
|
|
17
22
|
- โก **Dual module support** โ ESM and CJS.
|
|
18
23
|
- ๐ **Browser-ready** โ UMD global + CDN link.
|
|
19
24
|
|
|
@@ -79,16 +84,16 @@ const {
|
|
|
79
84
|
<script src="https://unpkg.com/myio-js-library@0.1.0/dist/myio-js-library.umd.min.js"></script>
|
|
80
85
|
<script>
|
|
81
86
|
// Decode payload
|
|
82
|
-
const text =
|
|
87
|
+
const text = MyIOLibrary.decodePayload('AwAVBwo=', 'key');
|
|
83
88
|
console.log(text); // "hello"
|
|
84
89
|
|
|
85
90
|
// Add namespace to data
|
|
86
91
|
const data = { temperature: 25, humidity: 60 };
|
|
87
|
-
const namespaced =
|
|
92
|
+
const namespaced = MyIOLibrary.addNamespace(data, 'sensor1');
|
|
88
93
|
console.log(namespaced); // { "temperature (sensor1)": 25, "humidity (sensor1)": 60 }
|
|
89
94
|
|
|
90
95
|
// Detect device type
|
|
91
|
-
const deviceType =
|
|
96
|
+
const deviceType = MyIOLibrary.detectDeviceType('building', 'floor2-room101');
|
|
92
97
|
console.log(deviceType); // "room"
|
|
93
98
|
</script>
|
|
94
99
|
```
|
|
@@ -300,6 +305,220 @@ numbers.toFixedSafe(NaN); // "โ"
|
|
|
300
305
|
numbers.toFixedSafe(Infinity); // "โ"
|
|
301
306
|
```
|
|
302
307
|
|
|
308
|
+
### Energy Formatting Utilities
|
|
309
|
+
|
|
310
|
+
#### `formatEnergy(value: number, unit: string): string`
|
|
311
|
+
|
|
312
|
+
Formats energy values with Brazilian locale formatting and appropriate units.
|
|
313
|
+
|
|
314
|
+
```javascript
|
|
315
|
+
import { formatEnergy } from 'myio-js-library';
|
|
316
|
+
|
|
317
|
+
formatEnergy(1234.56, 'kWh'); // "1.234,56 kWh"
|
|
318
|
+
formatEnergy(1000, 'MWh'); // "1.000,00 MWh"
|
|
319
|
+
formatEnergy(null, 'kWh'); // "-"
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
#### `formatAllInSameUnit(values: Array<{value: number, unit: string}>, targetUnit: string): string[]`
|
|
323
|
+
|
|
324
|
+
Converts and formats multiple energy values to the same unit.
|
|
325
|
+
|
|
326
|
+
```javascript
|
|
327
|
+
import { formatAllInSameUnit } from 'myio-js-library';
|
|
328
|
+
|
|
329
|
+
const values = [
|
|
330
|
+
{ value: 1, unit: 'kWh' },
|
|
331
|
+
{ value: 1, unit: 'MWh' },
|
|
332
|
+
{ value: 1, unit: 'GWh' }
|
|
333
|
+
];
|
|
334
|
+
|
|
335
|
+
formatAllInSameUnit(values, 'kWh');
|
|
336
|
+
// ['1,00 kWh', '1.000,00 kWh', '1.000.000,00 kWh']
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
#### `fmtPerc(value: number): string`
|
|
340
|
+
|
|
341
|
+
Formats percentage values with Brazilian locale formatting.
|
|
342
|
+
|
|
343
|
+
**Note:** This function uses Brazilian locale formatting (comma as decimal separator). This is a change from the original behavior which used dot notation. For values less than 0.1%, it returns the formatted percentage rather than "<0,1".
|
|
344
|
+
|
|
345
|
+
```javascript
|
|
346
|
+
import { fmtPerc } from 'myio-js-library';
|
|
347
|
+
|
|
348
|
+
fmtPerc(0.1234); // "12,34%"
|
|
349
|
+
fmtPerc(0.5); // "50,00%"
|
|
350
|
+
fmtPerc(0.001); // "0,10%"
|
|
351
|
+
fmtPerc(null); // "-"
|
|
352
|
+
fmtPerc(NaN); // "-"
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
### Date Utilities
|
|
356
|
+
|
|
357
|
+
#### `formatDateToYMD(date: Date | number | string): string`
|
|
358
|
+
|
|
359
|
+
Formats dates to YYYY-MM-DD format.
|
|
360
|
+
|
|
361
|
+
```javascript
|
|
362
|
+
import { formatDateToYMD } from 'myio-js-library';
|
|
363
|
+
|
|
364
|
+
formatDateToYMD(new Date('2023-12-25')); // "2023-12-25"
|
|
365
|
+
formatDateToYMD('2023-01-15T12:00:00Z'); // "2023-01-15"
|
|
366
|
+
formatDateToYMD('invalid'); // ""
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
#### `determineInterval(startDate: Date | string | number, endDate: Date | string | number): string`
|
|
370
|
+
|
|
371
|
+
Determines appropriate time interval based on date range.
|
|
372
|
+
|
|
373
|
+
```javascript
|
|
374
|
+
import { determineInterval } from 'myio-js-library';
|
|
375
|
+
|
|
376
|
+
const start = new Date('2023-01-01');
|
|
377
|
+
const end = new Date('2023-01-05');
|
|
378
|
+
determineInterval(start, end); // "day"
|
|
379
|
+
|
|
380
|
+
const longRange = new Date('2023-06-01');
|
|
381
|
+
determineInterval(start, longRange); // "month"
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
#### `getSaoPauloISOString(date: Date | string | number, edge?: 'start' | 'end'): string`
|
|
385
|
+
|
|
386
|
+
Gets ISO string for date at day edge in Sรฃo Paulo timezone.
|
|
387
|
+
|
|
388
|
+
```javascript
|
|
389
|
+
import { getSaoPauloISOString } from 'myio-js-library';
|
|
390
|
+
|
|
391
|
+
const date = new Date('2023-01-01T12:00:00Z');
|
|
392
|
+
getSaoPauloISOString(date, 'start'); // ISO string for start of day in SP timezone
|
|
393
|
+
getSaoPauloISOString(date, 'end'); // ISO string for end of day in SP timezone
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
#### `getDateRangeArray(startDate: Date | string | number, endDate: Date | string | number, interval?: 'day' | 'week' | 'month' | 'year'): Date[]`
|
|
397
|
+
|
|
398
|
+
Generates array of dates within specified range.
|
|
399
|
+
|
|
400
|
+
```javascript
|
|
401
|
+
import { getDateRangeArray } from 'myio-js-library';
|
|
402
|
+
|
|
403
|
+
const start = new Date('2023-01-01');
|
|
404
|
+
const end = new Date('2023-01-03');
|
|
405
|
+
const dates = getDateRangeArray(start, end, 'day');
|
|
406
|
+
// [Date('2023-01-01'), Date('2023-01-02'), Date('2023-01-03')]
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
### CSV Export Utilities
|
|
410
|
+
|
|
411
|
+
#### `exportToCSV(data: Record<string, any>[], headers: string[], filename?: string): string`
|
|
412
|
+
|
|
413
|
+
Exports data to CSV format with proper escaping.
|
|
414
|
+
|
|
415
|
+
```javascript
|
|
416
|
+
import { exportToCSV } from 'myio-js-library';
|
|
417
|
+
|
|
418
|
+
const data = [
|
|
419
|
+
{ name: 'John', age: 30, city: 'New York' },
|
|
420
|
+
{ name: 'Jane', age: 25, city: 'Los Angeles' }
|
|
421
|
+
];
|
|
422
|
+
const headers = ['name', 'age', 'city'];
|
|
423
|
+
|
|
424
|
+
const csv = exportToCSV(data, headers);
|
|
425
|
+
// "name,age,city\nJohn,30,New York\nJane,25,Los Angeles"
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
#### `exportToCSVAll(storesData: Record<string, Record<string, any>[]>, headers: string[], filename?: string): string`
|
|
429
|
+
|
|
430
|
+
Exports data for multiple stores/entities to CSV format.
|
|
431
|
+
|
|
432
|
+
```javascript
|
|
433
|
+
import { exportToCSVAll } from 'myio-js-library';
|
|
434
|
+
|
|
435
|
+
const storesData = {
|
|
436
|
+
'Store A': [
|
|
437
|
+
{ product: 'Apple', price: 1.50 },
|
|
438
|
+
{ product: 'Banana', price: 0.75 }
|
|
439
|
+
],
|
|
440
|
+
'Store B': [
|
|
441
|
+
{ product: 'Orange', price: 2.00 }
|
|
442
|
+
]
|
|
443
|
+
};
|
|
444
|
+
const headers = ['product', 'price'];
|
|
445
|
+
|
|
446
|
+
const csv = exportToCSVAll(storesData, headers);
|
|
447
|
+
// "Store,product,price\nStore A,Apple,1.5\nStore A,Banana,0.75\nStore B,Orange,2"
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
### Classification Utilities
|
|
451
|
+
|
|
452
|
+
#### `classify(entity: Record<string, any>, criteria: Record<string, any>): {category: string, subcategory?: string, confidence: number}`
|
|
453
|
+
|
|
454
|
+
Classifies energy entities based on their characteristics.
|
|
455
|
+
|
|
456
|
+
```javascript
|
|
457
|
+
import { classify } from 'myio-js-library';
|
|
458
|
+
|
|
459
|
+
const entity = { type: 'consumption', powerRating: 5000 };
|
|
460
|
+
const criteria = {};
|
|
461
|
+
|
|
462
|
+
const result = classify(entity, criteria);
|
|
463
|
+
// { category: 'energy_consumption', subcategory: 'medium_scale', confidence: 1.0 }
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
### Data Access Utilities
|
|
467
|
+
|
|
468
|
+
#### `getValueByDatakey(data: any, datakey: string): any`
|
|
469
|
+
|
|
470
|
+
Retrieves values from nested objects using datakey paths with dot notation and array indices.
|
|
471
|
+
|
|
472
|
+
```javascript
|
|
473
|
+
import { getValueByDatakey } from 'myio-js-library';
|
|
474
|
+
|
|
475
|
+
const data = {
|
|
476
|
+
sensor: {
|
|
477
|
+
temperature: 25,
|
|
478
|
+
readings: [10, 20, 30]
|
|
479
|
+
}
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
getValueByDatakey(data, 'sensor.temperature'); // 25
|
|
483
|
+
getValueByDatakey(data, 'sensor.readings[1]'); // 20
|
|
484
|
+
getValueByDatakey(data, 'nonexistent.path'); // undefined
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
#### `getValueByDatakeyLegacy(dataList: any[], dataSourceNameTarget: string, dataKeyTarget: string): any`
|
|
488
|
+
|
|
489
|
+
Legacy compatibility function for ThingsBoard widgets. Searches for values in data lists by matching dataSourceName and dataKey properties.
|
|
490
|
+
|
|
491
|
+
```javascript
|
|
492
|
+
import { getValueByDatakeyLegacy } from 'myio-js-library';
|
|
493
|
+
|
|
494
|
+
const dataList = [
|
|
495
|
+
{ dataSourceName: 'sensor1', dataKey: 'temperature', value: 25 },
|
|
496
|
+
{ dataSourceName: 'sensor1', dataKey: 'humidity', value: 60 },
|
|
497
|
+
{ dataSourceName: 'sensor2', dataKey: 'temperature', value: 22 }
|
|
498
|
+
];
|
|
499
|
+
|
|
500
|
+
getValueByDatakeyLegacy(dataList, 'sensor1', 'temperature'); // 25
|
|
501
|
+
getValueByDatakeyLegacy(dataList, 'sensor2', 'humidity'); // undefined
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
#### `findValue(data: any, keyOrPath: string, legacyDataKey?: string): any`
|
|
505
|
+
|
|
506
|
+
Unified function that supports both modern path-based access and legacy ThingsBoard-style data access.
|
|
507
|
+
|
|
508
|
+
```javascript
|
|
509
|
+
import { findValue } from 'myio-js-library';
|
|
510
|
+
|
|
511
|
+
// Modern usage (path-based)
|
|
512
|
+
const modernData = { sensor: { temperature: 25 } };
|
|
513
|
+
findValue(modernData, 'sensor.temperature'); // 25
|
|
514
|
+
|
|
515
|
+
// Legacy usage (ThingsBoard-style)
|
|
516
|
+
const legacyData = [
|
|
517
|
+
{ dataSourceName: 'sensor1', dataKey: 'temperature', value: 25 }
|
|
518
|
+
];
|
|
519
|
+
findValue(legacyData, 'sensor1', 'temperature'); // 25
|
|
520
|
+
```
|
|
521
|
+
|
|
303
522
|
## ๐งช Development
|
|
304
523
|
|
|
305
524
|
```bash
|