Comparing version 1.1.4 to 2.0.0
@@ -7,3 +7,2 @@ --- | ||
assignees: '' | ||
--- | ||
@@ -10,0 +9,0 @@ |
@@ -7,3 +7,2 @@ --- | ||
assignees: '' | ||
--- | ||
@@ -10,0 +9,0 @@ |
@@ -0,1 +1,15 @@ | ||
# [2.0.0](https://github.com/jessie-codes/safe-flat/compare/1.1.4...2.0.0) (2020-10-06) | ||
### Features | ||
* add an unflatten function that will take a flattened object and return a nested object ([cc7c6ef](https://github.com/jessie-codes/safe-flat/commit/cc7c6efe7062bddefcaa987c18d5ebd159b090fa)), closes [#32](https://github.com/jessie-codes/safe-flat/issues/32) | ||
### BREAKING CHANGES | ||
* New version now exports an object containing flatten and unflatten functions | ||
## [1.1.4](https://github.com/jessie-codes/safe-flat/compare/1.1.3...1.1.4) (2020-09-12) | ||
@@ -2,0 +16,0 @@ |
@@ -13,6 +13,7 @@ # Contributing | ||
(DCO)](https://developercertificate.org/) version 1.1 (`git --signoff`). | ||
2. Ensure all tests are passing (`npm test`). | ||
3. Adhere to [standardjs](http://standardjs.com) guidelines. | ||
4. Add tests for any new features or to show that bugs have been fixed. | ||
5. Update the README.md with details of changes to the interface. | ||
6. You may merge the Pull Request in once you have the sign-off of the package owner. | ||
2. Commits follow the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) standard. | ||
3. Ensure all tests are passing (`npm test`). | ||
4. Adhere to [standardjs](http://standardjs.com) guidelines. | ||
5. Add tests for any new features or to show that bugs have been fixed. | ||
6. Update the README.md with details of changes to the interface. | ||
7. You may merge the Pull Request in once you have the sign-off of the package owner. |
{ | ||
"name": "safe-flat", | ||
"version": "1.1.4", | ||
"version": "2.0.0", | ||
"description": "Safely flatten a nested JavaScript object.", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"commit": "git add --all && npx git-cz", | ||
"commit": "git add --all && npx git-cz --signoff", | ||
"coverage": "nyc report --reporter=text-lcov | coveralls", | ||
@@ -9,0 +9,0 @@ "fix": "eslint src test --fix", |
@@ -25,3 +25,3 @@ # safe-flat | ||
``` javascript | ||
const flatten = require('safe-flat') | ||
const { flatten } = require('safe-flat') | ||
@@ -67,2 +67,40 @@ const original = { | ||
*/ | ||
``` | ||
### unflatten(obj, [delimiter]) | ||
Unflats an object back to its original nested form. Optionally takes a custom `delimiter`, otherwise uses `.` by default. Circular references denoted by `[Circular]` are treated as Strings. | ||
``` javascript | ||
const { unflatten } = require('safe-flat') | ||
const original = { | ||
'a.b.c.0.val': 'one', | ||
'a.b.c.1.val': 'two', | ||
'a.b.c.2': '[Circular]', | ||
'a.b.d': 'three', | ||
'a.e': 'four', | ||
'a.b.f': '[Circular]' | ||
} | ||
const unflat = unflatten(original) | ||
/*{ | ||
a:{ | ||
b:{ | ||
c:[ | ||
{ | ||
val:'one' | ||
}, | ||
{ | ||
val:'two' | ||
}, | ||
'[Circular]' | ||
], | ||
d:'three', | ||
f:'[Circular]' | ||
}, | ||
e:'four' | ||
} | ||
}*/ | ||
``` |
@@ -7,3 +7,3 @@ const defaultDelimiter = '.' | ||
module.exports = (obj, delimiter) => { | ||
const flatten = (obj, delimiter) => { | ||
const result = {} | ||
@@ -38,1 +38,26 @@ const seperator = delimiter || defaultDelimiter | ||
} | ||
const unflatten = (obj, delimiter) => { | ||
const result = {} | ||
const seperator = delimiter || defaultDelimiter | ||
if (typeof obj !== 'object' || isDate(obj)) return obj | ||
const unflat = (original) => { | ||
Object.keys(original).forEach((key) => { | ||
const newKeys = key.split(seperator) | ||
newKeys.reduce((o, k, i) => { | ||
return o[k] || (o[k] = isNaN(Number(newKeys[i + 1])) ? (newKeys.length - 1 === i ? original[key] : {}) : []) | ||
}, result) | ||
}) | ||
} | ||
unflat(obj) | ||
return result | ||
} | ||
module.exports = { | ||
flatten, | ||
unflatten | ||
} |
const test = require('ava') | ||
const flatten = require('../src/index.js') | ||
const { flatten } = require('../src/index.js') | ||
@@ -4,0 +4,0 @@ test('it should return a flattened object', (t) => { |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
21618
15
296
104
1