From 32fedd9c3c8c9f49f167295d5527d64b369732ac Mon Sep 17 00:00:00 2001 From: Jesse McCarthy <git_commits@jessemccarthy.net> Date: Sun, 24 Nov 2019 12:34:27 -0500 Subject: [PATCH 1/3] Test `invertByArray` with path-like keys (Failing) --- test/objects.spec.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/objects.spec.js b/test/objects.spec.js index a10c54f5..2ada654c 100644 --- a/test/objects.spec.js +++ b/test/objects.spec.js @@ -536,13 +536,15 @@ describe('Object Functions', () => { it('invertByArray', () => { expect( F.invertByArray({ - a: ['x', 'y', 'z'], - b: ['x'], + a: ['v.w', 'x', '.y', 'z'], + b: ['v[w]', 'x'], }) ).to.deep.equal({ + 'v.w': ['a'], x: ['a', 'b'], - y: ['a'], + '.y': ['a'], z: ['a'], + 'v[w]': ['b'], }) }) it('stampKey', () => { From 321aa6929ccfbbc49ec7dffc80977af15b289318 Mon Sep 17 00:00:00 2001 From: Jesse McCarthy <git_commits@jessemccarthy.net> Date: Sun, 24 Nov 2019 12:43:52 -0500 Subject: [PATCH 2/3] Handle keys as literals in `invertByArray` Use `_.zipObject` instead of `zipObjectDeepWith` so path-like keys aren't interpreted as paths. --- src/object.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/object.js b/src/object.js index 127f0b3a..d6e3fc95 100644 --- a/src/object.js +++ b/src/object.js @@ -1,5 +1,5 @@ import _ from 'lodash/fp' -import { dotJoinWith, zipObjectDeepWith } from './array' +import { dotJoinWith } from './array' import { overNone } from './logic' import { isNotNil, isBlank } from './lang' import { @@ -171,7 +171,10 @@ let mergeArrays = (objValue, srcValue) => export let mergeAllArrays = _.mergeAllWith(mergeArrays) // { a: [x, y, z], b: [x] } -> { x: [a, b], y: [a], z: [a] } export let invertByArray = _.flow( - mapIndexed((arr, key) => zipObjectDeepWith(arr, () => [key])), + mapIndexed((arr, key) => + _.zipObject(arr, arr.map(() => [key])) + ), + mergeAllArrays ) From 7166d914941d67b47999f5bb97a32e69db08154b Mon Sep 17 00:00:00 2001 From: Jesse McCarthy <git_commits@jessemccarthy.net> Date: Sun, 24 Nov 2019 12:48:36 -0500 Subject: [PATCH 3/3] Declare `invertByArray` with const --- src/object.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/object.js b/src/object.js index d6e3fc95..fbb369ff 100644 --- a/src/object.js +++ b/src/object.js @@ -170,7 +170,7 @@ let mergeArrays = (objValue, srcValue) => // Straight from the lodash docs export let mergeAllArrays = _.mergeAllWith(mergeArrays) // { a: [x, y, z], b: [x] } -> { x: [a, b], y: [a], z: [a] } -export let invertByArray = _.flow( +export const invertByArray = _.flow( mapIndexed((arr, key) => _.zipObject(arr, arr.map(() => [key])) ),