diff --git a/README.md b/README.md index cd842c3..d4ca932 100644 --- a/README.md +++ b/README.md @@ -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"); @@ -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. diff --git a/index.js b/index.js index 65129f1..e682c37 100644 --- a/index.js +++ b/index.js @@ -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 }; diff --git a/lib/index.js b/lib/index.js index 290e6fe..a67ac31 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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; diff --git a/test/unit/index.test.js b/test/unit/index.test.js index d94f118..b5e71e9 100644 --- a/test/unit/index.test.js +++ b/test/unit/index.test.js @@ -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); + }); })