Skip to content

Commit

Permalink
feat: added Unflat function for
Browse files Browse the repository at this point in the history
  • Loading branch information
Parul-rathva committed Jun 8, 2024
1 parent 47b5199 commit 781b86a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ npm install @kishor82/jsonflat

## Usage

To flata nested JSON object, import the `flat` function from `jsonflat` and use it as follows:
To flat nested JSON object, import the `flat` function from `jsonflat` and use it as follows:

```javascript
const { flat } = require("@kishor82/jsonflat");
Expand Down Expand Up @@ -99,6 +99,44 @@ Output:
}
```

### nestedObject JSON Object

```javascript

const { unflat } = require("@kishor82/jsonflat");

const flattenedObject = {
"person.name.first": "John",
"person.name.last": "Doe",
"person.age": 30,
"person.address.city": "New York",
"person.address.state": "NY"
};

const nestedObject = unflat(flattenedObject);

console.log(nestedObject);

```

Output:

```json
{
person: {
name: {
first: "John",
last: "Doe",
},
age: 30,
address: {
city: "New York",
state: "NY",
},
},
};
```

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Directories are not valid import targets in ESM unless they have an explicit entry point defined.
import jsonflat from "./lib/index.js";

const { flat } = jsonflat;
const { flat, unflat } = jsonflat;

// https://github.com/rollup/rollup/issues/1961#issuecomment-618345638
export { jsonflat as default, flat };
export { jsonflat as default, flat, unflat };
16 changes: 16 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,24 @@ const flat = (data, delimeter = ".", parentKey) => {
}, {});
};

const unflat = (data, delimiter = ".") => {
return Object.keys(data).reduce((unflattenedObject, key) => {
const keys = key.split(delimiter);
keys.reduce((acc, k, i) => {
if (i === keys.length - 1) {
acc[k] = data[key];
} else {
acc[k] = acc[k] || {};
}
return acc[k];
}, unflattenedObject);
return unflattenedObject;
}, {});
};

const jsonflat = {
flat,
unflat,
};

export default jsonflat;
26 changes: 26 additions & 0 deletions test/unit/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,30 @@ describe("unflat functon", () => {
// Assert that the unflat function is defined
expect(unflat).toBeDefined();
});

// Test case for unflattening a simple object
it("Unflattens a simple object", () => {
const data = {
name: "John",
age: 30,
"address.city": "New York",
"address.country": "USA",
};
const expected = {
name: "John",
age: 30,
address: {
city: "New York",
country: "USA",
},
};
expect(unflat(data)).toEqual(expected);
});

// Test case for unflattening an empty object
it("returns an empty object for an empty input", () => {
const data = {};
const expected = {};
expect(unflat(data)).toEqual(expected);
});
})

0 comments on commit 781b86a

Please sign in to comment.