-
-
Notifications
You must be signed in to change notification settings - Fork 42
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 | ||||||||||||||
import mapObject from 'map-obj'; | ||||||||||||||
Comment on lines
+29
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
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]}} | ||||||||||||||
*/ | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect indent. |
||||||||||||||
path: string[], | ||||||||||||||
) => [ | ||||||||||||||
targetKey: MappedObjectKeyType, | ||||||||||||||
targetValue: MappedObjectValueType, | ||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]}} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
``` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Readme and index.d.ts should be in sync. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I took this to mean you want the |
||||||
|
||||||
##### mapperOptions | ||||||
|
||||||
Type: `object` | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs |
||
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}); | ||
}); |
There was a problem hiding this comment.
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.