object-deep-from-entries
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -1,7 +0,11 @@ | ||
type TEntries = Array<[string | number | Array<string | number>, any]> | ||
type EntryKey = string | number | ||
type TResult = Array<{[key : string] : any} | any> | {[key : string] : any} | ||
type EntryPath = EntryKey | EntryKey[] | ||
declare function objectDeepFromEntries(entries : TEntries) : TResult | ||
type Entry = [EntryPath, unknown] | ||
type Entries = Entry[] | ||
declare function objectDeepFromEntries(entries: Entries): object | unknown[] | ||
export default objectDeepFromEntries |
@@ -1,6 +0,7 @@ | ||
const isNaN = require("./isNaN") | ||
const isPlainObject = require("./isPlainObject") | ||
const isNumber = require("./isNumber") | ||
const getTag = require("./getTag") | ||
const isNaN = require("./isNaN") | ||
const isArray = Array.isArray | ||
const {isArray} = Array | ||
@@ -20,27 +21,40 @@ const hasNumKey = entries => entries.find( | ||
*/ | ||
function deepFromEntries(target, path, value) { | ||
function deepFromEntries(parent, parentKey, path, value) { | ||
const key = path.shift() | ||
const curr = isNaN(key) ? {} : [] | ||
const current = isNaN(key) ? {} : [] | ||
if (!target) { | ||
// TODO: Refactor that function to reduce code. | ||
if (!parent) { | ||
if (path.length === 0) { | ||
curr[key] = value | ||
current[key] = value | ||
return curr | ||
return current | ||
} | ||
curr[key] = deepFromEntries(curr[key], path, value) | ||
current[key] = deepFromEntries(current[key], key, path, value) | ||
return curr | ||
return current | ||
} | ||
if (isPlainObject(parent) || isArray(parent)) { | ||
if (path.length === 0) { | ||
parent[key] = value | ||
return parent | ||
} | ||
parent[key] = deepFromEntries(parent[key], key, path, value) | ||
return parent | ||
} | ||
if (path.length === 0) { | ||
target[key] = value | ||
current[key] = value | ||
return target | ||
return current | ||
} | ||
target[key] = deepFromEntries(target[key], path, value) | ||
current[key] = deepFromEntries(parent[key], key, path, value) | ||
return target | ||
return current | ||
} | ||
@@ -51,5 +65,5 @@ | ||
* | ||
* @param {array} entries | ||
* @param {any[]} entries | ||
* | ||
* @return {object} | ||
* @return {object | unknown[]} | ||
* | ||
@@ -90,7 +104,7 @@ * @api public | ||
for (const entry of entries) { | ||
let path = entry[0] | ||
const value = entry[1] | ||
if (!isArray(path)) { | ||
for (let [path, value] of entries) { | ||
if (isArray(path)) { | ||
// Copy entity path, because Array#shift() is used later. | ||
path = path.slice() | ||
} else { | ||
path = [path] | ||
@@ -101,10 +115,10 @@ } | ||
if (path.length < 1 && (isCollection && isNaN(root))) { | ||
if (path.length === 0 && (isCollection && isNaN(root))) { | ||
res.push({[root]: value}) | ||
} else if (path.length < 1) { | ||
} else if (path.length === 0) { | ||
res[root] = value | ||
} else if (isCollection && isNaN(root)) { | ||
res.push({[root]: deepFromEntries(res[root], path, value)}) | ||
res.push({[root]: deepFromEntries(res[root], root, path, value)}) | ||
} else { | ||
res[root] = deepFromEntries(res[root], path, value) | ||
res[root] = deepFromEntries(res[root], root, path, value) | ||
} | ||
@@ -111,0 +125,0 @@ } |
{ | ||
"name": "object-deep-from-entries", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Make an object or collection from entries deeply.", | ||
@@ -20,20 +20,25 @@ "main": "objectDeepFromEntries.js", | ||
"scripts": { | ||
"test": "node_modules/.bin/ava test.js", | ||
"staged": "node_modules/.bin/lint-staged", | ||
"coverage": "node_modules/.bin/nyc yarn test", | ||
"report": "yarn coverage && node_modules/.bin/nyc report --reporter=html yarn test", | ||
"ci": "yarn coverage && node_modules/.bin/nyc report --reporter=lcov yarn test && node_modules/.bin/codecov", | ||
"precommit": "yarn staged", | ||
"prepush": "yarn test" | ||
"test": "ava", | ||
"eslint": "eslint --fix *.js", | ||
"limit": "size-limit", | ||
"staged": "lint-staged", | ||
"coverage": "c8 npm test", | ||
"report:html": "c8 -r=html npm test", | ||
"ci": "c8 npm test && c8 report --reporter=json", | ||
"_postinstall": "husky install", | ||
"prepublishOnly": "pinst --disable", | ||
"postpublish": "pinst --enable" | ||
}, | ||
"devDependencies": { | ||
"@octetstream/eslint-config": "2.1.0", | ||
"ava": "0.25.0", | ||
"codecov": "3.0.0", | ||
"eslint": "4.19.0", | ||
"eslint-plugin-ava": "4.5.1", | ||
"husky": "0.14.3", | ||
"lint-staged": "7.0.0", | ||
"nyc": "11.6.0" | ||
"@octetstream/eslint-config": "5.0.0", | ||
"@size-limit/preset-small-lib": "5.0.3", | ||
"ava": "3.15.0", | ||
"c8": "7.8.0", | ||
"eslint": "7.32.0", | ||
"eslint-plugin-ava": "12.0.0", | ||
"husky": "7.0.1", | ||
"lint-staged": "11.1.2", | ||
"pinst": "2.1.6", | ||
"size-limit": "5.0.3" | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
7869
10
9
157
1