Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'path' parameter to the mapper function #48

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,20 @@ export type Mapper<
> = (
sourceKey: keyof SourceObjectType,
sourceValue: SourceObjectType[keyof SourceObjectType],
source: SourceObjectType
source: SourceObjectType,
/**
When using `deep: true`, this is the sequence of keys to reach the current value from the `source`, otherwise it is an empty array.
For arrays, the key is the index of the element being mapped.
@example
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example needs to be wrapped in a codeblock.

import mapObject from 'map-obj';
Comment on lines +29 to +30
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@example
import mapObject from 'map-obj';
@example
import mapObject from 'map-obj';

const object = {foo: {bar: [2], baz: [1, 2, 3]}}
const mapper = (key, value, source, path) => path.join(".") === "foo.baz" ? [key, 3] : [key, value];
const result = mapObject(object, mapper, {deep: true});

console.log(result);
//=> {foo: {bar:[2], baz: [3, 3, 3]}}
*/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect indent.

path: string[],
) => [
targetKey: MappedObjectKeyType,
targetValue: MappedObjectValueType,
Expand Down
17 changes: 11 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const isObjectCustom = value =>

export const mapObjectSkip = Symbol('mapObjectSkip');

const _mapObject = (object, mapper, options, isSeen = new WeakMap()) => {
const _mapObject = (object, mapper, options, {isSeen = new WeakMap(), path = []} = {}) => {
options = {
deep: false,
target: {},
Expand All @@ -26,13 +26,18 @@ const _mapObject = (object, mapper, options, isSeen = new WeakMap()) => {
const {target} = options;
delete options.target;

const mapArray = array => array.map(element => isObjectCustom(element) ? _mapObject(element, mapper, options, isSeen) : element);
const mapArray = (array, arrayPath) => array.map((element, index) =>
isObjectCustom(element)
? _mapObject(element, mapper, options, {isSeen, path: [...arrayPath, index]})
: element,
);

if (Array.isArray(object)) {
return mapArray(object);
return mapArray(object, path);
}

for (const [key, value] of Object.entries(object)) {
const mapResult = mapper(key, value, object);
const mapResult = mapper(key, value, object, options.deep ? [...path, key] : []);

if (mapResult === mapObjectSkip) {
continue;
Expand All @@ -47,8 +52,8 @@ const _mapObject = (object, mapper, options, isSeen = new WeakMap()) => {

if (options.deep && shouldRecurse && isObjectCustom(newValue)) {
newValue = Array.isArray(newValue)
? mapArray(newValue)
: _mapObject(newValue, mapper, options, isSeen);
? mapArray(newValue, [...path, key])
: _mapObject(newValue, mapper, options, {isSeen, path: [...path, key]});
}

target[newKey] = newValue;
Expand Down
21 changes: 20 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,29 @@ The source object to copy properties from.

#### mapper

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

A mapping function.

##### path

Type: `string[]`

When using `deep: true`, this is the sequence of keys to reach the current value from the `source`, otherwise it is an empty array.

For arrays, the key is the index of the element being mapped.

```js
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if this simplified example is ok, in response to https://github.com/sindresorhus/map-obj/pull/43/files#r884235030

import mapObject from 'map-obj';

const object = {foo: {bar: [2], baz: [1, 2, 3]}}
const mapper = (key, value, source, path) => path.join(".") === "foo.baz" ? [key, 3] : [key, value];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single-quotes, as commented earlier.

const result = mapObject(object, mapper, {deep: true});

console.log(result);
//=> {foo: {bar:[2], baz: [3, 3, 3]}}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//=> {foo: {bar:[2], baz: [3, 3, 3]}}
//=> {foo: {bar: [2], baz: [3, 3, 3]}}

```
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readme and index.d.ts should be in sync.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took this to mean you want the path annotated in the definition file, let me know if I misunderstood.


##### mapperOptions

Type: `object`
Expand Down
73 changes: 73 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,76 @@ test('remove keys (#36)', t => {
const actual = mapObject(object, mapper, {deep: true});
t.deepEqual(actual, expected);
});

test('mapper `path` argument', t => {
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
t.plan(10);

const subject = {
one: 1,
nested: {
two: 2,
deep: {
three: 3,
},

array: [4, 5, 6],
},
};

mapObject(
subject,
(key, value, source, path) => {
t.true(Array.isArray(path));
t.is(path.length, 0);
return [key, value];
},
);

mapObject(
subject,
(key, value, source, path) => {
t.true(Array.isArray(path));
return [key, value];
},
{deep: true},
);
});

test('mapper argument `path` contains the sequence of keys to reach the current value from the source', t => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs t.plan too.

const subject = {
one: 1,
nested: {
two: 2,
deep: {
three: 3,
},
simpleArray: [4, 5, 6],
complexArray: [
{seven: 7},
{eight: 8},
],
},
};

const expectations = {
one: 1,
nested: subject.nested,
'nested.two': 2,
'nested.deep': subject.nested.deep,
'nested.deep.three': 3,
'nested.simpleArray': subject.nested.simpleArray,
'nested.complexArray': subject.nested.complexArray,
'nested.complexArray.0.seven': 7,
'nested.complexArray.1.eight': 8,
};

const mapper = (key, value, source, path) => {
t.true(Array.isArray(path));
t.is(path.at(-1), key);
const expectedValue = expectations[path.join('.')];
t.is(value, expectedValue);
return [key, value];
};

mapObject(subject, mapper, {deep: true});
});
Loading