Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

map-obj

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

map-obj - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

48

index.js
'use strict';
module.exports = function (obj, cb) {
var ret = {};
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var res = cb(key, obj[key], obj);
ret[res[0]] = res[1];
// customized for this use-case
const isObject = x =>
typeof x === 'object' &&
x !== null &&
!(x instanceof RegExp) &&
!(x instanceof Error) &&
!(x instanceof Date);
module.exports = function mapObj(obj, fn, opts, seen) {
opts = Object.assign({
deep: false,
target: {}
}, opts);
seen = seen || new WeakMap();
if (seen.has(obj)) {
return seen.get(obj);
}
return ret;
seen.set(obj, opts.target);
const target = opts.target;
delete opts.target;
for (const key of Object.keys(obj)) {
const val = obj[key];
const res = fn(key, val, obj);
let newVal = res[1];
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);
}
}
target[res[0]] = newVal;
}
return target;
};

19

package.json
{
"name": "map-obj",
"version": "1.0.1",
"version": "2.0.0",
"description": "Map object keys and values into a new object",

@@ -13,6 +13,6 @@ "license": "MIT",

"engines": {
"node": ">=0.10.0"
"node": ">=4"
},
"scripts": {
"test": "node test.js"
"test": "xo && ava"
},

@@ -32,7 +32,16 @@ "files": [

"iterate",
"iterator"
"iterator",
"rename",
"modify",
"deep",
"recurse",
"recursive"
],
"devDependencies": {
"ava": "0.0.4"
"ava": "*",
"xo": "*"
},
"xo": {
"esnext": true
}
}

@@ -16,9 +16,5 @@ # map-obj [![Build Status](https://travis-ci.org/sindresorhus/map-obj.svg?branch=master)](https://travis-ci.org/sindresorhus/map-obj)

```js
var mapObj = require('map-obj');
const mapObj = require('map-obj');
var newObject = mapObj({foo: 'bar'}, function (key, value, object) {
// first element is the new key and second is the new value
// here we reverse the order
return [value, key];
});
const newObject = mapObj({foo: 'bar'}, (key, value) => [value, key]);
//=> {bar: 'foo'}

@@ -28,4 +24,44 @@ ```

## API
### mapObj(source, mapper, [options])
#### source
Type: `Object`
Source object to copy properties from.
#### mapper
Type: `Function`
Mapping function.
- It has signature `mapper(sourceKey, sourceValue, source)`.
- It must return a two item array: `[targetKey, targetValue]`.
#### deep
Type: `boolean`<br>
Default: `false`
Recurse nested objects and objects in arrays.
#### target
Type: `Object`<br>
Default: `{}`
Target object to map properties on to.
## Related
- [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
## License
MIT © [Sindre Sorhus](http://sindresorhus.com)
MIT © [Sindre Sorhus](https://sindresorhus.com)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc