pax_global_header00006660000000000000000000000064141216065030014510gustar00rootroot0000000000000052 comment=1e5c466ee0f293e91465ecd53d74ecdeb90cb4e4 map-obj-4.3.0/000077500000000000000000000000001412160650300130415ustar00rootroot00000000000000map-obj-4.3.0/.editorconfig000066400000000000000000000002571412160650300155220ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 map-obj-4.3.0/.gitattributes000066400000000000000000000000231412160650300157270ustar00rootroot00000000000000* text=auto eol=lf map-obj-4.3.0/.github/000077500000000000000000000000001412160650300144015ustar00rootroot00000000000000map-obj-4.3.0/.github/funding.yml000066400000000000000000000001611412160650300165540ustar00rootroot00000000000000github: sindresorhus open_collective: sindresorhus tidelift: npm/map-obj custom: https://sindresorhus.com/donate map-obj-4.3.0/.github/security.md000066400000000000000000000002631412160650300165730ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. map-obj-4.3.0/.github/workflows/000077500000000000000000000000001412160650300164365ustar00rootroot00000000000000map-obj-4.3.0/.github/workflows/main.yml000066400000000000000000000006641412160650300201130ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 14 - 12 - 10 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test map-obj-4.3.0/.gitignore000066400000000000000000000000271412160650300150300ustar00rootroot00000000000000node_modules yarn.lock map-obj-4.3.0/.npmrc000066400000000000000000000000231412160650300141540ustar00rootroot00000000000000package-lock=false map-obj-4.3.0/index.d.ts000066400000000000000000000074321412160650300147500ustar00rootroot00000000000000// Unique symbol cannot be declared in a namespace directly, so we declare it top-level // See: https://github.com/sindresorhus/map-obj/pull/38#discussion_r702396878 declare const skipSymbol: unique symbol; declare namespace mapObject { type Mapper< SourceObjectType extends {[key: string]: any}, MappedObjectKeyType extends string, MappedObjectValueType > = ( sourceKey: keyof SourceObjectType, sourceValue: SourceObjectType[keyof SourceObjectType], source: SourceObjectType ) => [ targetKey: MappedObjectKeyType, targetValue: MappedObjectValueType, mapperOptions?: mapObject.MapperOptions ] | typeof mapObject.mapObjectSkip; interface Options { /** Recurse nested objects and objects in arrays. @default false */ deep?: boolean; /** Target object to map properties on to. @default {} */ target?: {[key: string]: any}; } interface DeepOptions extends Options { deep: true; } interface TargetOptions extends Options { target: TargetObjectType; } interface MapperOptions { /** Whether `targetValue` should be recursed. Requires `deep: true`. @default true */ readonly shouldRecurse?: boolean; } /** Return this value from a `mapper` function to remove a key from an object. @example ``` const mapObject = require('map-obj'); const object = {one: 1, two: 2} const mapper = (key, value) => value === 1 ? [key, value] : mapObject.mapObjectSkip const result = mapObject(object, mapper); console.log(result); //=> {one: 1} ``` */ const mapObjectSkip: typeof skipSymbol } /** Map object keys and values into a new object. @param source - Source object to copy properties from. @param mapper - Mapping function. @example ``` import mapObject = require('map-obj'); const newObject = mapObject({foo: 'bar'}, (key, value) => [value, key]); //=> {bar: 'foo'} const newObject = mapObject({FOO: true, bAr: {bAz: true}}, (key, value) => [key.toLowerCase(), value]); //=> {foo: true, bar: {bAz: true}} const newObject = mapObject({FOO: true, bAr: {bAz: true}}, (key, value) => [key.toLowerCase(), value], {deep: true}); //=> {foo: true, bar: {baz: true}} const newObject = mapObject({one: 1, two: 2}, (key, value) => value === 1 ? [key, value] : mapObject.mapObjectSkip); //=> {one: 1} ``` */ declare function mapObject< SourceObjectType extends object, TargetObjectType extends {[key: string]: any}, MappedObjectKeyType extends string, MappedObjectValueType >( source: SourceObjectType, mapper: mapObject.Mapper< SourceObjectType, MappedObjectKeyType, MappedObjectValueType >, options: mapObject.DeepOptions & mapObject.TargetOptions ): TargetObjectType & {[key: string]: unknown}; declare function mapObject< SourceObjectType extends object, MappedObjectKeyType extends string, MappedObjectValueType >( source: SourceObjectType, mapper: mapObject.Mapper< SourceObjectType, MappedObjectKeyType, MappedObjectValueType >, options: mapObject.DeepOptions ): {[key: string]: unknown}; declare function mapObject< SourceObjectType extends {[key: string]: any}, TargetObjectType extends {[key: string]: any}, MappedObjectKeyType extends string, MappedObjectValueType >( source: SourceObjectType, mapper: mapObject.Mapper< SourceObjectType, MappedObjectKeyType, MappedObjectValueType >, options: mapObject.TargetOptions ): TargetObjectType & {[K in MappedObjectKeyType]: MappedObjectValueType}; declare function mapObject< SourceObjectType extends {[key: string]: any}, MappedObjectKeyType extends string, MappedObjectValueType >( source: SourceObjectType, mapper: mapObject.Mapper< SourceObjectType, MappedObjectKeyType, MappedObjectValueType >, options?: mapObject.Options ): {[K in MappedObjectKeyType]: MappedObjectValueType}; export = mapObject; map-obj-4.3.0/index.js000066400000000000000000000030431412160650300145060ustar00rootroot00000000000000'use strict'; const isObject = value => typeof value === 'object' && value !== null; const mapObjectSkip = Symbol('skip'); // Customized for this use-case const isObjectCustom = value => isObject(value) && !(value instanceof RegExp) && !(value instanceof Error) && !(value instanceof Date); const mapObject = (object, mapper, options, isSeen = new WeakMap()) => { options = { deep: false, target: {}, ...options }; if (isSeen.has(object)) { return isSeen.get(object); } isSeen.set(object, options.target); const {target} = options; delete options.target; const mapArray = array => array.map(element => isObjectCustom(element) ? mapObject(element, mapper, options, isSeen) : element); if (Array.isArray(object)) { return mapArray(object); } for (const [key, value] of Object.entries(object)) { const mapResult = mapper(key, value, object); if (mapResult === mapObjectSkip) { continue; } let [newKey, newValue, {shouldRecurse = true} = {}] = mapResult; // Drop `__proto__` keys. if (newKey === '__proto__') { continue; } if (options.deep && shouldRecurse && isObjectCustom(newValue)) { newValue = Array.isArray(newValue) ? mapArray(newValue) : mapObject(newValue, mapper, options, isSeen); } target[newKey] = newValue; } return target; }; module.exports = (object, mapper, options) => { if (!isObject(object)) { throw new TypeError(`Expected an object, got \`${object}\` (${typeof object})`); } return mapObject(object, mapper, options); }; module.exports.mapObjectSkip = mapObjectSkip; map-obj-4.3.0/index.test-d.ts000066400000000000000000000023051412160650300157170ustar00rootroot00000000000000import {expectType, expectAssignable} from 'tsd'; import mapObject = require('./index.js'); const options: mapObject.Options = {}; const newObject = mapObject({foo: 'bar'}, (key, value) => [value, key]); expectType<{[key: string]: 'foo'}>(newObject); expectType<'foo'>(newObject.bar); const object = mapObject({foo: 'bar'}, (key, value) => [value, key], { target: {baz: 'baz'} }); expectType<{baz: string} & {[x: string]: 'foo'}>(object); expectType<'foo'>(object.bar); expectType(object.baz); const object1 = mapObject({foo: 'bar'}, (key, value) => [value, key], { target: {baz: 'baz'}, deep: false }); expectType<{baz: string} & {[x: string]: 'foo'}>(object1); expectType<'foo'>(object1.bar); expectType(object1.baz); const object2 = mapObject({foo: 'bar'}, (key, value) => [value, key], { deep: true }); expectType<{[key: string]: unknown}>(object2); const object3 = mapObject({foo: 'bar'}, (key, value) => [value, key], { deep: true, target: {bar: 'baz' as const} }); expectAssignable<{[key: string]: unknown}>(object3); expectType<'baz'>(object3.bar); mapObject({foo: 'bar'}, (key, value) => [value, key, {shouldRecurse: false}]); mapObject({foo: 'bar'}, () => mapObject.mapObjectSkip); map-obj-4.3.0/license000066400000000000000000000021351412160650300144070ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (https://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-4.3.0/package.json000066400000000000000000000013161412160650300153300ustar00rootroot00000000000000{ "name": "map-obj", "version": "4.3.0", "description": "Map object keys and values into a new object", "license": "MIT", "repository": "sindresorhus/map-obj", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "engines": { "node": ">=8" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "map", "object", "key", "keys", "value", "values", "iterate", "iterator", "rename", "modify", "deep", "recurse", "recursive" ], "devDependencies": { "ava": "^2.0.0", "tsd": "^0.14.0", "xo": "^0.24.0" } } map-obj-4.3.0/readme.md000066400000000000000000000042541412160650300146250ustar00rootroot00000000000000# map-obj > Map object keys and values into a new object ## Install ```sh npm install map-obj ``` ## Usage ```js const mapObject = require('map-obj'); const newObject = mapObject({foo: 'bar'}, (key, value) => [value, key]); //=> {bar: 'foo'} const newObject = mapObject({FOO: true, bAr: {bAz: true}}, (key, value) => [key.toLowerCase(), value]); //=> {foo: true, bar: {bAz: true}} const newObject = mapObject({FOO: true, bAr: {bAz: true}}, (key, value) => [key.toLowerCase(), value], {deep: true}); //=> {foo: true, bar: {baz: true}} const newObject = mapObject({one: 1, two: 2}, (key, value) => value === 1 ? [key, value] : mapObject.mapObjectSkip); //=> {one: 1} ``` ## API ### mapObject(source, mapper, options?) #### source Type: `object` Source object to copy properties from. #### mapper Type: `(sourceKey, sourceValue, source) => [targetKey, targetValue, mapperOptions?] | mapObject.mapObjectSkip` Mapping function. ##### mapperOptions Type: `object` ###### shouldRecurse Type: `boolean`\ Default: `true` Whether `targetValue` should be recursed. Requires `deep: true`. #### options Type: `object` ##### deep Type: `boolean`\ Default: `false` Recurse nested objects and objects in arrays. ##### target Type: `object`\ Default: `{}` Target object to map properties on to. ### mapObject.mapObjectSkip Return this value from a `mapper` function to exclude the key from the new object. ```js const mapObject = require('map-obj'); const object = {one: 1, two: 2} const mapper = (key, value) => value === 1 ? [key, value] : mapObject.mapObjectSkip const result = mapObject(object, mapper); console.log(result); //=> {one: 1} ``` ## Related - [filter-obj](https://github.com/sindresorhus/filter-obj) - Filter object keys and values into a new object ---
Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
map-obj-4.3.0/test.js000066400000000000000000000061231412160650300143600ustar00rootroot00000000000000import test from 'ava'; import mapObject from '.'; test('main', t => { t.is(mapObject({foo: 'bar'}, key => [key, 'unicorn']).foo, 'unicorn'); t.is(mapObject({foo: 'bar'}, (key, value) => ['unicorn', value]).unicorn, 'bar'); t.is(mapObject({foo: 'bar'}, (key, value) => [value, key]).bar, 'foo'); }); test('target option', t => { const target = {}; t.is(mapObject({foo: 'bar'}, (key, value) => [value, key], {target}), target); t.is(target.bar, 'foo'); }); test('deep option', t => { const object = { one: 1, object: { two: 2, three: 3 }, array: [ { four: 4 }, 5 ] }; const expected = { one: 2, object: { two: 4, three: 6 }, array: [ { four: 8 }, 5 ] }; const mapper = (key, value) => [key, typeof value === 'number' ? value * 2 : value]; const actual = mapObject(object, mapper, {deep: true}); t.deepEqual(actual, expected); }); test('shouldRecurse mapper option', t => { const object = { one: 1, object: { two: 2, three: 3 }, array: [ { four: 4 }, 5 ] }; const expected = { one: 2, object: { two: 2, three: 3 }, array: [ { four: 8 }, 5 ] }; const mapper = (key, value) => { if (key === 'object') { return [key, value, {shouldRecurse: false}]; } return [key, typeof value === 'number' ? value * 2 : value]; }; const actual = mapObject(object, mapper, {deep: true}); t.deepEqual(actual, expected); }); test('nested arrays', t => { const object = { array: [ [ 0, 1, 2, { a: 3 } ] ] }; const expected = { array: [ [ 0, 1, 2, { a: 6 } ] ] }; const mapper = (key, value) => [key, typeof value === 'number' ? value * 2 : value]; const actual = mapObject(object, mapper, {deep: true}); t.deepEqual(actual, expected); }); test('handles circular references', t => { const object = { one: 1, array: [ 2 ] }; object.circular = object; object.array2 = object.array; object.array.push(object); const mapper = (key, value) => [key.toUpperCase(), value]; const actual = mapObject(object, mapper, {deep: true}); const expected = { ONE: 1, ARRAY: [ 2 ] }; expected.CIRCULAR = expected; expected.ARRAY2 = expected.ARRAY; expected.ARRAY.push(expected); t.deepEqual(actual, expected); }); test('validates input', t => { t.throws(() => { mapObject(1, () => {}); }, TypeError); }); test('__proto__ keys are safely dropped', t => { const input = {['__proto__']: {one: 1}}; const output = mapObject(input, (key, value) => [key, value]); t.deepEqual(output, {}); // AVA's equality checking isn't quite strict enough to catch the difference // between plain objects as prototypes and Object.prototype, so we also check // the prototype by identity t.is(Object.getPrototypeOf(output), Object.prototype); }); test('remove keys (#36)', t => { const object = { one: 1, two: 2 }; const expected = { one: 1 }; const mapper = (key, value) => value === 1 ? [key, value] : mapObject.mapObjectSkip; const actual = mapObject(object, mapper, {deep: true}); t.deepEqual(actual, expected); });