Skip to content

Commit

Permalink
feat: remove wraping non-value pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Jan 17, 2019
1 parent 9888d75 commit 4ce6e63
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
]
},
"dependencies": {
"dot-wild": "^3.0.1"
"common-prefix": "^1.1.0",
"dot-wild": "^3.0.1",
"lodash": "^4.17.11"
},
"description": "Creates a cartesian product of all properties identified using value-pointers.",
"devDependencies": {
Expand Down
24 changes: 18 additions & 6 deletions src/utilities/unnest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

// eslint-disable-next-line import/no-namespace
import * as wild from 'dot-wild';
import findCommonPrefix from 'common-prefix';
import {
mapKeys
} from 'lodash';

// eslint-disable-next-line flowtype/no-weak-types
const getDeepestValuePointerKey = (flatInput: Object): string => {
Expand Down Expand Up @@ -33,9 +37,17 @@ const isDotKeyArray = (key: string): boolean => {

// eslint-disable-next-line flowtype/no-weak-types
const unnest = (tree: Object) => {
const flatInput = wild.flatten(tree);
let flatInput = wild.flatten(tree);

console.log('flatInput', flatInput);
const commonPrefix = findCommonPrefix(Object.keys(flatInput));

if (commonPrefix && commonPrefix.endsWith('.')) {
flatInput = mapKeys(flatInput, (value, key) => {
return key.slice(commonPrefix.length);
});
}

const normalisedTree = wild.expand(flatInput);

const keys = Object.keys(flatInput);

Expand Down Expand Up @@ -63,7 +75,7 @@ const unnest = (tree: Object) => {

if (isDotKeyArray(deepestValuePointerKey)) {
if (deepestValuePointerKey.startsWith('@')) {
return tree;
return normalisedTree;
}

const tokens = deepestValuePointerKey.split(/\.\d+\./);
Expand All @@ -74,13 +86,13 @@ const unnest = (tree: Object) => {

const first = tokens[0];

const descendents = wild.get(tree, first + '.*') || [];
const descendents = wild.get(normalisedTree, first + '.*') || [];

const results = [];

for (const descendent of descendents) {
const result = unnest({
...wild.remove(tree, first),
...wild.remove(normalisedTree, first),
...descendent
});

Expand All @@ -93,7 +105,7 @@ const unnest = (tree: Object) => {

return results;
} else {
return tree;
return normalisedTree;
}
};

Expand Down
4 changes: 1 addition & 3 deletions test/unnest/unnest.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ test('creates a cartesian product of pointer values (depth 2)', (t) => {
t.deepEqual(unnest(input), expected);
});

test.only('strips parent non-pointer values', (t) => {
test('strips parent non-pointer values', (t) => {
const input = {
member: {
'@date': 'foo',
Expand All @@ -130,8 +130,6 @@ test.only('strips parent non-pointer values', (t) => {
}
];

console.log(unnest(input));

t.deepEqual(unnest(input), expected);
});

Expand Down

0 comments on commit 4ce6e63

Please sign in to comment.