-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Benoit-Welsch/feat-dotParser
Feat DotParser
- Loading branch information
Showing
9 changed files
with
190 additions
and
16 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
### Parser | ||
|
||
Parse data to object. | ||
|
||
#### Dot | ||
|
||
```typescript | ||
import { Parser } from "@lv00/toolkit"; | ||
|
||
const data = `battery.charge: 100 | ||
battery.charge.low: 20 | ||
battery.runtime: 995 | ||
battery.type: PbAc | ||
device.mfr: EATON | ||
device.model: Ellipse PRO 650 | ||
device.serial: P354M05BE0 | ||
device.type: ups | ||
driver.name: usbhid-ups`; | ||
|
||
const result = dot(data); | ||
console.log(result); | ||
// Will return the following object | ||
{ | ||
battery: { | ||
charge: { | ||
_value: "100", | ||
low: "20", | ||
}, | ||
runtime: "995", | ||
type: "PbAc", | ||
}, | ||
device: { | ||
mfr: "EATON", | ||
model: "Ellipse PRO 650", | ||
serial: "P354M05BE0", | ||
type: "ups", | ||
}, | ||
driver: { | ||
name: "usbhid-ups", | ||
}, | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { describe, it, expect } from 'bun:test'; | ||
import { dot } from './dot'; | ||
|
||
const data = `battery.charge: 100 | ||
battery.charge.low: 20 | ||
battery.runtime: 995 | ||
battery.type: PbAc | ||
device.mfr: EATON | ||
device.model: Ellipse PRO 650 | ||
device.serial: P354M05BE0 | ||
device.type: ups | ||
driver.name: usbhid-ups`; | ||
|
||
describe('dot parser', () => { | ||
it('should parse the dot data correctly', () => { | ||
const result = dot(data); | ||
expect(result).toEqual({ | ||
battery: { | ||
charge: { _value: '100', low: '20' }, | ||
runtime: '995', | ||
type: 'PbAc', | ||
}, | ||
device: { | ||
mfr: 'EATON', | ||
model: 'Ellipse PRO 650', | ||
serial: 'P354M05BE0', | ||
type: 'ups', | ||
}, | ||
driver: { name: 'usbhid-ups' }, | ||
}); | ||
}); | ||
|
||
it('should parse the dot data correctly with custom conflict char', () => { | ||
const result = dot(data, '$'); | ||
expect(result).toEqual({ | ||
battery: { | ||
charge: { $value: '100', low: '20' }, | ||
runtime: '995', | ||
type: 'PbAc', | ||
}, | ||
device: { | ||
mfr: 'EATON', | ||
model: 'Ellipse PRO 650', | ||
serial: 'P354M05BE0', | ||
type: 'ups', | ||
}, | ||
driver: { name: 'usbhid-ups' }, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import mergeWith from 'lodash.mergewith'; | ||
|
||
export const dot = (data: string, conflictChar = '_') => { | ||
if (!data) throw new Error('No data provided'); | ||
// Global oject (returned by the function) | ||
const res = {}; | ||
// Array of line | ||
data.split('\n').forEach((line) => { | ||
if (!line) return; | ||
// Separate value from key object | ||
const [keys, value] = line.split(':'); | ||
// Array of key object | ||
const splitted = keys.split('.'); | ||
// Object containing key and value to merge to global object | ||
let valueForKey: string | object = value.trimStart().trimEnd(); | ||
// Create Object from Array of key object | ||
for (let keyIndex = splitted.length - 1; keyIndex >= 0; keyIndex--) { | ||
valueForKey = { [splitted[keyIndex]]: valueForKey }; | ||
} | ||
// Deep merge with conflict detection | ||
mergeWith( | ||
res, | ||
valueForKey, | ||
(objValue: string | object, srcValue: object) => { | ||
if ( | ||
objValue && | ||
typeof objValue !== 'object' && | ||
!Array.isArray(objValue) | ||
) { | ||
return { [`${conflictChar}value`]: objValue, ...srcValue }; | ||
} | ||
return undefined; | ||
}, | ||
); | ||
}); | ||
return res; | ||
}; | ||
|
||
export default dot; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { dot } from './dot'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters