@coderich/util
Advanced tools
Comparing version 0.1.16 to 0.1.17
{ | ||
"name": "@coderich/util", | ||
"main": "src/index.js", | ||
"version": "0.1.16", | ||
"version": "0.1.17", | ||
"publishConfig": { | ||
@@ -6,0 +6,0 @@ "access": "public" |
@@ -12,5 +12,6 @@ const FS = require('fs'); | ||
exports.push = (arr, it) => arr[arr.push(it) - 1]; | ||
exports.push = (arr, el) => arr[arr.push(el) - 1]; | ||
exports.uvl = (...values) => values.reduce((prev, value) => (prev === undefined ? value : prev), undefined); | ||
exports.nvl = (...values) => values.reduce((prev, value) => (prev === null ? value : prev), null); | ||
exports.pairs = (...values) => values.flat().reduce((prev, curr, i, arr) => (i % 2 === 0 ? prev.concat([arr.slice(i, i + 2)]) : prev), []); | ||
exports.filterBy = (arr, fn) => arr.filter((b, index) => index === arr.findIndex(a => fn(a, b))); | ||
@@ -23,2 +24,10 @@ exports.ensureArray = a => (Array.isArray(a) ? a : [a].filter(el => el !== undefined)); | ||
exports.findAndReplace = (arr, fn, ...items) => { | ||
return arr.find((el, i, ...rest) => { | ||
const res = fn(el, i, ...rest); | ||
if (res) arr.splice(i, 1, ...items); | ||
return res; | ||
}); | ||
}; | ||
exports.isPlainObject = (obj) => { | ||
@@ -106,7 +115,8 @@ if (obj == null || Array.isArray(obj)) return false; | ||
exports.pathmap = (paths, mixed, fn) => { | ||
exports.pathmap = (paths, mixed, fn = v => v) => { | ||
if (!exports.isPlainObjectOrArray(mixed)) return mixed; | ||
if (typeof paths === 'string') paths = paths.split('.'); | ||
paths = paths.filter(Boolean); | ||
const traverse = (keys, parent) => { | ||
const traverse = (keys, parent, path = [], jsonpath = []) => { | ||
if (exports.isPlainObjectOrArray(parent)) { | ||
@@ -116,9 +126,16 @@ const key = keys.shift(); | ||
// When there are more keys to go; the best we can do is traverse what's there | ||
// Otherwise, when at the last key, we can callback and assign response value | ||
if (keys.length) { | ||
if (isProperty) traverse(keys, parent[key]); | ||
else if (Array.isArray(parent)) parent.forEach(el => traverse([key, ...keys], el)); | ||
} else if (isProperty) { | ||
parent[key] = fn(parent[key], { key, parent }); | ||
if (isProperty) { | ||
traverse(keys, parent[key], path.concat(key), jsonpath.concat(key)); | ||
} else if (Array.isArray(parent)) { | ||
jsonpath.push('[*]'); | ||
parent.forEach((el, i) => traverse([key, ...keys], el, path.concat(i), jsonpath)); | ||
} | ||
} else if (Array.isArray(parent)) { | ||
parent.forEach(el => (el[key] = fn(el[key], { key, parent: el }))); | ||
jsonpath.push('[*]'); | ||
parent.forEach((el, i) => (el[key] = fn(el[key], { key, parent: el, path: path.concat(i, key), jsonpath: jsonpath.concat(key) }))); | ||
} else { | ||
parent[key] = fn(parent[key], { key, parent, path: path.concat(key), jsonpath: jsonpath.concat(key) }); | ||
} | ||
@@ -128,3 +145,8 @@ } | ||
traverse(paths, mixed); | ||
if (paths.length) { | ||
traverse(paths, mixed); | ||
} else { | ||
mixed = fn(mixed, { key: '', parent: mixed, path: [], jsonpath: [] }); | ||
} | ||
return mixed; | ||
@@ -131,0 +153,0 @@ }; |
7493
172