Skip to content

Commit

Permalink
Allow specifying whether targetValue should be recursed (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richienb authored Jan 18, 2021
1 parent 290f1ed commit 73c6139
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 7 deletions.
15 changes: 14 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ declare namespace mapObject {
sourceKey: keyof SourceObjectType,
sourceValue: SourceObjectType[keyof SourceObjectType],
source: SourceObjectType
) => [MappedObjectKeyType, MappedObjectValueType];
) => [
targetKey: MappedObjectKeyType,
targetValue: MappedObjectValueType,
mapperOptions?: mapObject.MapperOptions
];

interface Options {
/**
Expand All @@ -32,6 +36,15 @@ declare namespace mapObject {
interface TargetOptions<TargetObjectType extends {[key: string]: any}> extends Options {
target: TargetObjectType;
}

interface MapperOptions {
/**
Whether `targetValue` should be recursed. Requires `deep: true`.
@default true
*/
shouldRecurse?: boolean;
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ const mapObject = (object, mapper, options, isSeen = new WeakMap()) => {
}

for (const [key, value] of Object.entries(object)) {
let [newKey, newValue] = mapper(key, value, object);
let [newKey, newValue, {shouldRecurse = true} = {}] = mapper(key, value, object);

if (options.deep && isObjectCustom(newValue)) {
if (options.deep && shouldRecurse && isObjectCustom(newValue)) {
newValue = Array.isArray(newValue) ?
mapArray(newValue) :
mapObject(newValue, mapper, options, isSeen);
Expand Down
2 changes: 2 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ const object3 = mapObject({foo: 'bar'}, (key, value) => [value, key], {
});
expectType<{[key: string]: unknown}>(object3);
expectType<'baz'>(object3.bar);

mapObject({foo: 'bar'}, (key, value) => [value, key, {shouldRecurse: false}]);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
],
"devDependencies": {
"ava": "^2.0.0",
"tsd": "^0.7.3",
"tsd": "^0.14.0",
"xo": "^0.24.0"
}
}
12 changes: 9 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,18 @@ Source object to copy properties from.

#### mapper

Type: `Function`
Type: `(sourceKey, sourceValue, source) => [targetKey, targetValue, mapperOptions?]`

Mapping function.

- It has signature `mapper(sourceKey, sourceValue, source)`.
- It must return a two item array: `[targetKey, targetValue]`.
##### mapperOptions

###### shouldRecurse

Type: `boolean`\
Default: `true`

Whether `targetValue` should be recursed. Requires `deep: true`.

#### options

Expand Down
41 changes: 41 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,47 @@ test('deep option', t => {
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: [
Expand Down

0 comments on commit 73c6139

Please sign in to comment.