pax_global_header00006660000000000000000000000064127671500560014523gustar00rootroot0000000000000052 comment=40ce3a8b7d419249a6c0d83031b5d3214b9bf8ed map-obj-2.0.0/000077500000000000000000000000001276715005600130475ustar00rootroot00000000000000map-obj-2.0.0/.editorconfig000066400000000000000000000002761276715005600155310ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [{package.json,*.yml}] indent_style = space indent_size = 2 map-obj-2.0.0/.gitattributes000066400000000000000000000000351276715005600157400ustar00rootroot00000000000000* text=auto *.js text eol=lf map-obj-2.0.0/.gitignore000066400000000000000000000000151276715005600150330ustar00rootroot00000000000000node_modules map-obj-2.0.0/.travis.yml000066400000000000000000000000671276715005600151630ustar00rootroot00000000000000sudo: false language: node_js node_js: - '6' - '4' map-obj-2.0.0/index.js000066400000000000000000000015251276715005600145170ustar00rootroot00000000000000'use strict'; // 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); } 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; }; map-obj-2.0.0/license000066400000000000000000000021371276715005600144170ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) Sindre Sorhus (sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. map-obj-2.0.0/package.json000066400000000000000000000013131276715005600153330ustar00rootroot00000000000000{ "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 } } map-obj-2.0.0/readme.md000066400000000000000000000022451276715005600146310ustar00rootroot00000000000000# map-obj [![Build Status](https://travis-ci.org/sindresorhus/map-obj.svg?branch=master)](https://travis-ci.org/sindresorhus/map-obj) > Map object keys and values into a new object ## Install ``` $ npm install --save map-obj ``` ## Usage ```js const mapObj = require('map-obj'); const newObject = mapObj({foo: 'bar'}, (key, value) => [value, key]); //=> {bar: 'foo'} ``` ## 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`
Default: `false` Recurse nested objects and objects in arrays. #### target Type: `Object`
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](https://sindresorhus.com) map-obj-2.0.0/test.js000066400000000000000000000021621276715005600143650ustar00rootroot00000000000000import test from 'ava'; import m from './'; test('main', t => { t.is(m({foo: 'bar'}, key => [key, 'unicorn']).foo, 'unicorn'); t.is(m({foo: 'bar'}, (key, val) => ['unicorn', val]).unicorn, 'bar'); t.is(m({foo: 'bar'}, (key, val) => [val, key]).bar, 'foo'); }); test('target option', t => { const target = {}; t.is(m({foo: 'bar'}, (key, val) => [val, key], {target}), target); t.is(target.bar, 'foo'); }); test('deep option', t => { const obj = {one: 1, obj: {two: 2, three: 3}, arr: [{four: 4}, 5]}; const expected = {one: 2, obj: {two: 4, three: 6}, arr: [{four: 8}, 5]}; const fn = (key, val) => [key, typeof val === 'number' ? val * 2 : val]; const actual = m(obj, fn, {deep: true}); t.deepEqual(actual, expected); }); test('handles circular references', t => { const obj = {one: 1, arr: [2]}; obj.circular = obj; obj.arr2 = obj.arr; obj.arr.push(obj); const fn = (key, val) => [key.toUpperCase(), val]; const actual = m(obj, fn, {deep: true}); const expected = {ONE: 1, ARR: [2]}; expected.CIRCULAR = expected; expected.ARR2 = expected.ARR; expected.ARR.push(expected); t.deepEqual(actual, expected); });