Comparing version 4.0.0 to 4.1.0
25
index.js
'use strict'; | ||
const isObject = value => typeof value === 'object' && value !== null; | ||
// Customized for this use-case | ||
const isObject = value => | ||
typeof value === 'object' && | ||
value !== null && | ||
const isObjectCustom = value => | ||
isObject(value) && | ||
!(value instanceof RegExp) && | ||
@@ -11,3 +12,3 @@ !(value instanceof Error) && | ||
const mapObject = (object, fn, options, isSeen = new WeakMap()) => { | ||
const mapObject = (object, mapper, options, isSeen = new WeakMap()) => { | ||
options = { | ||
@@ -28,3 +29,3 @@ deep: false, | ||
const mapArray = array => array.map(element => isObject(element) ? mapObject(element, fn, options, isSeen) : element); | ||
const mapArray = array => array.map(element => isObjectCustom(element) ? mapObject(element, mapper, options, isSeen) : element); | ||
if (Array.isArray(object)) { | ||
@@ -35,8 +36,8 @@ return mapArray(object); | ||
for (const [key, value] of Object.entries(object)) { | ||
let [newKey, newValue] = fn(key, value, object); | ||
let [newKey, newValue] = mapper(key, value, object); | ||
if (options.deep && isObject(newValue)) { | ||
if (options.deep && isObjectCustom(newValue)) { | ||
newValue = Array.isArray(newValue) ? | ||
mapArray(newValue) : | ||
mapObject(newValue, fn, options, isSeen); | ||
mapObject(newValue, mapper, options, isSeen); | ||
} | ||
@@ -50,2 +51,8 @@ | ||
module.exports = mapObject; | ||
module.exports = (object, mapper, options) => { | ||
if (!isObject(object)) { | ||
throw new TypeError(`Expected an object, got \`${object}\` (${typeof object})`); | ||
} | ||
return mapObject(object, mapper, options); | ||
}; |
{ | ||
"name": "map-obj", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "Map object keys and values into a new object", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
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
7054
136