Skip to content

Commit

Permalink
fixup! Preserve commas that are not part of a brace expansion
Browse files Browse the repository at this point in the history
Roll unbalanced and nested brace assertion into mini-parser iteration
  • Loading branch information
nickiaconis committed Jan 4, 2017
1 parent 62e8d44 commit f55afed
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions packages/ember-metal/lib/expand_properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,8 @@ export default function expandProperties(pattern, callback) {
'Brace expanded properties cannot contain spaces, e.g. "user.{firstName, lastName}" should be "user.{firstName,lastName}"',
pattern.indexOf(' ') === -1
);
assert(
`Brace expanded properties have to be balanced and cannot be nested, pattern: ${pattern}`,
((str) => {
let inBrace = 0;
let char;
for (let i = 0; i < str.length; i++) {
char = str.charAt(i);

if (char === '{') {
inBrace++;
} else if (char === '}') {
inBrace--;
}

if (inBrace > 1 || inBrace < 0) {
return false;
}
}

return true;
})(pattern));

let unbalancedNestedError = `Brace expanded properties have to be balanced and cannot be nested, pattern: ${pattern}`;
let properties = [pattern];

// Iterating backward over the pattern makes dealing with indices easier.
Expand All @@ -77,6 +57,8 @@ export default function expandProperties(pattern, callback) {
if (!inside) {
bookmark = i - 1;
inside = true;
} else {
assert(unbalancedNestedError, false);
}
break;
// Opening curly brace will be the last character of the brace expansion we encounter.
Expand All @@ -96,10 +78,15 @@ export default function expandProperties(pattern, callback) {
}
}
inside = false;
} else {
assert(unbalancedNestedError, false);
}
break;
}
}
if (inside) {
assert(unbalancedNestedError, false);
}

for (let i = 0; i < properties.length; i++) {
callback(properties[i].replace(END_WITH_EACH_REGEX, '.[]'));
Expand Down

0 comments on commit f55afed

Please sign in to comment.