Skip to content

Commit

Permalink
Merge pull request #4 from OlegPaska/unFlatten
Browse files Browse the repository at this point in the history
added unflattenObject
  • Loading branch information
ppaska authored Feb 11, 2024
2 parents 280cb1a + 676bea4 commit 6f18fa2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ Loading and parsing data from a common file formats like: CSV, JSON, TSV either
- [**pivot**](https://www.datapipe-js.com/docs/datapipe-js-array#pivot) (array, rowFields, columnField, dataField, aggFunction?, columnValues?) - Returns a reshaped (pivoted) array based on unique column values.
- [**transpose**](https://www.datapipe-js.com/docs/datapipe-js-array#transpose) (array) - Transpose rows to columns in an array
- [**sort**](https://www.datapipe-js.com/docs/datapipe-js-array#sort) ([fieldName(s)]) - Sort array of elements according to a field and direction specified. e.g. sort(array, 'name ASC', 'age DESC')
- [**flattenObject**](https://www.datapipe-js.com/docs/datapipe-js-array#flattenObject) (Object) - flattens complex nested object into simple object. e.g. flattenObject(obj)
- [**unflattenObject**](https://www.datapipe-js.com/docs/datapipe-js-array#unflattenObject) (Object) - unflattens simple object into complex nested object. e.g. unflattenObject(obj)

### Joining data arrays

Expand Down
36 changes: 36 additions & 0 deletions src/array/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,42 @@ export function distinct(array: any[], elementSelector?: Selector): any[] {
* @param data
* @returns
*/

export function unflattenObject(data: any): any {


function setProperty(data: any, pName: string, value: any): any {
const pNames = pName.split('.')
let parent: any = data

for (let i = 0; i < pNames.length - 1; i++) {
if (pNames[i] in parent) {
parent = parent[pNames[i]]
} else {
parent[pNames[i]] = {}
parent = parent[pNames[i]]
}
}
parent[pNames[pNames.length - 1]] = value
}

if (Array.isArray(data)) {
const arr = [];
for (let i = 0; i < data.length; i++) {
const obj = unflattenObject(data[i]);
arr.push(obj);
}
return arr;
} else {
const obj: any = {}
const keys = Object.keys(data);
for (let i = 0; i < keys.length; i++) {
setProperty(obj, keys[i], data[keys[i]])
}
return obj
}
}

export function flattenObject(data: any): any {
if (!data || typeof data !== 'object') {
return data;
Expand Down
9 changes: 7 additions & 2 deletions src/data-pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import {
toObject,
toSeries,
flatten,
flattenObject
flattenObject,
unflattenObject
} from './array';

export class DataPipe {
Expand Down Expand Up @@ -304,7 +305,11 @@ export class DataPipe {
return flattenObject(this.data);
}

flattern(): any {
unflattenObject(): any {
return unflattenObject(this.data);
}

flatten(): any {
return flatten(this.data);
}

Expand Down
11 changes: 10 additions & 1 deletion src/tests/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('Test array methods', () => {
expect(groups.length).toBe(3);
});

it('flattern', () => {
it('flatten', () => {
const testArray = [1, 4, [2, [5, 5, [9, 7]], 11], 0, [], []];
const flatten = pipeFuncs.flatten(testArray);
expect(flatten.length).toBe(9);
Expand All @@ -131,6 +131,15 @@ describe('Test array methods', () => {
expect(Object.keys(flatten[0]).join(',')).toBe('a,d.d1,d.d2');
});

it('unflattenObject', () => {
const testArray = [{"a": 1, "b.e": 2,"b.c.d": 2,"b.c.f": 3,"b.f": 5},{"a": -1, "b.e": -2,"b.c.d": -2,"b.c.f": -3,"b.f": -5}];
const unflatten = pipeFuncs.unflattenObject(testArray);
expect(unflatten.length).toBe(2);
expect(Object.keys(unflatten[0]).join(',')).toBe('a,b');
expect(unflatten[0].b.c['d']).toBe(2);
expect(unflatten[1].b.c['d']).toBe(-2);
});

it('countBy', () => {
const countriesCount = pipeFuncs.countBy(data, i => i.country);
expect(countriesCount['US']).toBe(3);
Expand Down

0 comments on commit 6f18fa2

Please sign in to comment.