@coderich/util
Advanced tools
Comparing version 0.1.6 to 0.1.7
{ | ||
"name": "@coderich/util", | ||
"main": "src/index.js", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"publishConfig": { | ||
@@ -6,0 +6,0 @@ "access": "public" |
@@ -19,3 +19,10 @@ const FS = require('fs'); | ||
exports.ucFirst = string => string.charAt(0).toUpperCase() + string.slice(1); | ||
exports.isPlainArrayOrObject = obj => Array.isArray(obj) || exports.isPlainObject(obj); | ||
exports.isPlainObject = (obj) => { | ||
if (obj == null) return false; | ||
const proto = Object.getPrototypeOf(obj); | ||
return proto === Object.prototype || proto.toString?.call?.(obj) === '[object Object]'; | ||
}; | ||
exports.filterRe = (arr, fn) => { | ||
@@ -39,8 +46,6 @@ const map = new Map(); | ||
const maxDepth = options.depth ?? Infinity; | ||
const typeFn = options.safe ? exports.isPlainObject : exports.isPlainArrayOrObject; | ||
return exports.map(mixed, el => (function flatten(data, obj = {}, path = [], depth = 0) { | ||
const type = Object.prototype.toString.call(data); | ||
const types = options.safe ? ['[object Object]'] : ['[object Object]', '[object Array]']; | ||
if (depth <= maxDepth && types.includes(type) && Object.keys(data).length && !ObjectId.isValid(data)) { | ||
if (depth <= maxDepth && Object.keys(data).length && typeFn(data)) { | ||
return Object.entries(data).reduce((o, [key, value]) => { | ||
@@ -62,6 +67,6 @@ const $key = options.strict && key.split('.').length > 1 ? `['${key}']` : key; | ||
exports.unflatten = (data, options = {}) => { | ||
const typeFn = options.safe ? exports.isPlainObject : exports.isPlainArrayOrObject; | ||
return exports.map(data, (el) => { | ||
const type = Object.prototype.toString.call(data); | ||
const types = options.safe ? ['[object Object]'] : ['[object Object]', '[object Array]']; | ||
return types.includes(type) ? Object.entries(el).reduce((prev, [key, value]) => { | ||
return typeFn(data) ? Object.entries(el).reduce((prev, [key, value]) => { | ||
return set(prev, key, value); | ||
@@ -68,0 +73,0 @@ }, {}) : el; |
5623
131