Skip to content

Commit

Permalink
Merge pull request #3 from Benoit-Welsch/feat-dotParser
Browse files Browse the repository at this point in the history
Feat DotParser
  • Loading branch information
Benoit-Welsch committed Dec 10, 2023
2 parents b6e138a + 36a035c commit d23a761
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 16 deletions.
12 changes: 0 additions & 12 deletions .prettierrc

This file was deleted.

44 changes: 44 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- [Check](#check)
- [Csv](#csv)
- [Parser](#parser)
- [Queue](#queue)
- [Scanner](#scanner)
- [Unit](#unit)
Expand Down Expand Up @@ -63,6 +64,49 @@ csv.toString("|");
csv.clear();
```

### 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",
},
}
```

### Scanner

Read folder synchronously without headache
Expand Down
Binary file modified bun.lockb
Binary file not shown.
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
"type": "module",
"scripts": {
"build": "bun run build.mjs",
"doc": "bun run genDoc.ts"
"doc": "bun run genDoc.ts",
"format": "prettier --write \"src/**/*.ts\" --single-quote --trailing-comma all --tab-width 2"
},
"devDependencies": {
"@types/lodash.mergewith": "^4.6.9",
"@types/node": "^20.10.0",
"bun-plugin-dts": "^0.2.1",
"bun-types": "latest"
"bun-types": "latest",
"prettier": "^3.1.1"
},
"peerDependencies": {
"typescript": "^5.0.0"
Expand All @@ -22,5 +25,8 @@
"default": "./dist/index.js"
}
},
"license": "MIT"
}
"license": "MIT",
"dependencies": {
"lodash.mergewith": "^4.6.2"
}
}
42 changes: 42 additions & 0 deletions src/Parser/Readme.md
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",
},
}
```
50 changes: 50 additions & 0 deletions src/Parser/dot.test.ts
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' },
});
});
});
39 changes: 39 additions & 0 deletions src/Parser/dot.ts
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;
1 change: 1 addition & 0 deletions src/Parser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { dot } from './dot';
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ export const Unit = { sizeToUnitAuto };
// Url
import { buildUrlWithQuery } from './Url';
export const Url = { buildUrlWithQuery };

// Parser
import { dot } from './Parser';
export const Parser = { dot };

0 comments on commit d23a761

Please sign in to comment.