array-deep-get-set
Advanced tools
Comparing version 1.0.0 to 1.0.1
35
index.js
@@ -1,4 +0,33 @@ | ||
import get from 'src/get' | ||
import set from 'src/set' | ||
const lodashGet = require('lodash.get') | ||
const lodashSet = require('lodash.set') | ||
export default { get, set } | ||
const get = (object, path = '', defaultValue) => { | ||
const flatArrayIndex = path.indexOf('[]') | ||
if (flatArrayIndex > 0) { | ||
const pathToArray = path.slice(0, flatArrayIndex) | ||
const pathInArray = path.slice(flatArrayIndex + 3, path.length) | ||
const arrayToFlat = lodashGet(object, pathToArray, []).map((item) => | ||
lodashGet(item, pathInArray, null) | ||
) | ||
return arrayToFlat | ||
} | ||
return lodashGet(object, path, defaultValue) | ||
} | ||
const set = (objectToSet, path = '', value) => { | ||
const flatArrayIndex = path.indexOf('[]') | ||
if (flatArrayIndex > 0) { | ||
const pathToArray = path.slice(0, flatArrayIndex) | ||
const pathInArray = path.slice(flatArrayIndex + 3, path.length) | ||
const arrayToSet = get(objectToSet, pathToArray, []) | ||
const arraySet = value.map((item, index) => ({ | ||
...(arrayToSet[index] ?? []), | ||
[pathInArray]: item, | ||
})) | ||
return lodashSet(objectToSet, pathToArray, arraySet) | ||
} | ||
return lodashSet(objectToSet, path, value) | ||
} | ||
module.exports = {get, set} |
{ | ||
"name": "array-deep-get-set", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"version": "1.0.1", | ||
"author": "Thibaut Mottet", | ||
"license": "ISC", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
@@ -12,0 +10,0 @@ "lodash.get": "4.4.2", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
4521
50
1
7