Comparing version 2.0.0 to 3.0.0
43
index.js
'use strict'; | ||
// customized for this use-case | ||
// Customized for this use-case | ||
const isObject = x => | ||
@@ -11,33 +11,36 @@ typeof x === 'object' && | ||
module.exports = function mapObj(obj, fn, opts, seen) { | ||
opts = Object.assign({ | ||
module.exports = function mapObj(object, fn, options, seen) { | ||
options = Object.assign({ | ||
deep: false, | ||
target: {} | ||
}, opts); | ||
}, options); | ||
seen = seen || new WeakMap(); | ||
if (seen.has(obj)) { | ||
return seen.get(obj); | ||
if (seen.has(object)) { | ||
return seen.get(object); | ||
} | ||
seen.set(obj, opts.target); | ||
seen.set(object, options.target); | ||
const target = opts.target; | ||
delete opts.target; | ||
const {target} = options; | ||
delete options.target; | ||
for (const key of Object.keys(obj)) { | ||
const val = obj[key]; | ||
const res = fn(key, val, obj); | ||
let newVal = res[1]; | ||
const mapArray = array => array.map(x => isObject(x) ? mapObj(x, fn, options, seen) : x); | ||
if (Array.isArray(object)) { | ||
return mapArray(object); | ||
} | ||
if (opts.deep && isObject(newVal)) { | ||
if (Array.isArray(newVal)) { | ||
newVal = newVal.map(x => isObject(x) ? mapObj(x, fn, opts, seen) : x); | ||
} else { | ||
newVal = mapObj(newVal, fn, opts, seen); | ||
} | ||
/// TODO: Use `Object.entries()` when targeting Node.js 8 | ||
for (const key of Object.keys(object)) { | ||
const value = object[key]; | ||
let [newKey, newValue] = fn(key, value, object); | ||
if (options.deep && isObject(newValue)) { | ||
newValue = Array.isArray(newValue) ? | ||
mapArray(newValue) : | ||
mapObj(newValue, fn, options, seen); | ||
} | ||
target[res[0]] = newVal; | ||
target[newKey] = newValue; | ||
} | ||
@@ -44,0 +47,0 @@ |
{ | ||
"name": "map-obj", | ||
"version": "2.0.0", | ||
"description": "Map object keys and values into a new object", | ||
"license": "MIT", | ||
"repository": "sindresorhus/map-obj", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"map", | ||
"obj", | ||
"object", | ||
"key", | ||
"keys", | ||
"value", | ||
"values", | ||
"val", | ||
"iterate", | ||
"iterator", | ||
"rename", | ||
"modify", | ||
"deep", | ||
"recurse", | ||
"recursive" | ||
], | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"esnext": true | ||
} | ||
"name": "map-obj", | ||
"version": "3.0.0", | ||
"description": "Map object keys and values into a new object", | ||
"license": "MIT", | ||
"repository": "sindresorhus/map-obj", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"map", | ||
"obj", | ||
"object", | ||
"key", | ||
"keys", | ||
"value", | ||
"values", | ||
"val", | ||
"iterate", | ||
"iterator", | ||
"rename", | ||
"modify", | ||
"deep", | ||
"recurse", | ||
"recursive" | ||
], | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
} | ||
} |
@@ -9,3 +9,3 @@ # map-obj [![Build Status](https://travis-ci.org/sindresorhus/map-obj.svg?branch=master)](https://travis-ci.org/sindresorhus/map-obj) | ||
``` | ||
$ npm install --save map-obj | ||
$ npm install map-obj | ||
``` | ||
@@ -43,4 +43,8 @@ | ||
#### deep | ||
#### options | ||
Type: `Object` | ||
##### deep | ||
Type: `boolean`<br> | ||
@@ -51,3 +55,3 @@ Default: `false` | ||
#### target | ||
##### target | ||
@@ -63,3 +67,2 @@ Type: `Object`<br> | ||
- [filter-obj](https://github.com/sindresorhus/filter-obj) - Filter object keys and values into a new object | ||
- [object-assign](https://github.com/sindresorhus/object-assign) - Copy enumerable own properties from one or more source objects to a target object | ||
@@ -66,0 +69,0 @@ |
Sorry, the diff of this file is not supported yet
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
37
69
3828