Skip to content

Commit

Permalink
get the collapse and recurse done earlier.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelly Selden committed Aug 24, 2017
1 parent 7754d2f commit f34b023
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions addon/collapse-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@ import {
ARRAY_LENGTH
} from './-constants';

function collapseAndPruneDuplicates(expandedProperties) {
return expandedProperties.map(collapseKey).reduce((properties, collapsedProperties) => {
let uniqueProperties = collapsedProperties.filter(collapsedProperty => {
return properties.indexOf(collapsedProperty) === -1;
});
return properties.concat(uniqueProperties);
}, []);
}

export default function collapseKey(property) {
if (typeof property !== 'string') {
return [property];
}

let expandedProperties = expandProperty(property);
if (expandedProperties.length > 1) {
return collapseAndPruneDuplicates(expandedProperties);
}

let arrayIndex = property.indexOf(ARRAY_EACH);
if (arrayIndex === -1) {
arrayIndex = property.indexOf(ARRAY_LENGTH);
Expand All @@ -19,18 +33,7 @@ export default function collapseKey(property) {
// and will convert to `this`
return [''];
} else if (arrayIndex > 0) {
if (property.indexOf('{') === -1) {
return [property.slice(0, arrayIndex - 1)];
} else {
let propertyList = [];
expandProperty(property).forEach(property => {
let [collapsedProperty] = collapseKey(property);
if (propertyList.indexOf(collapsedProperty) === -1) {
propertyList.push(collapsedProperty);
}
});
return propertyList;
}
return [property.slice(0, arrayIndex - 1)];
}

return expandProperty(property);
Expand Down

0 comments on commit f34b023

Please sign in to comment.