Outlook_Addin_LLM/node_modules/@fluentui/merge-styles/lib/tokenizeWithParentheses.js

46 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

/**
* Split a string into tokens separated by whitespace, except all text within parentheses
* is treated as a single token (whitespace is ignored within parentheses).
*
* Unlike String.split(' '), multiple consecutive space characters are collapsed and
* removed from the returned array (including leading and trailing spaces).
*
* For example:
* `tokenizeWithParentheses("3px calc(var(--x) / 2) 9px 0 ")`
* => `["3px", "calc(var(--x) / 2)", "9px", "0"]`
*
* @returns The array of tokens. Returns an empty array if the string was empty or contained only whitespace.
*/
export function tokenizeWithParentheses(value) {
var parts = [];
var partStart = 0;
var parens = 0;
for (var i = 0; i < value.length; i++) {
switch (value[i]) {
case '(':
parens++;
break;
case ')':
if (parens) {
parens--;
}
break;
case '\t':
case ' ':
if (!parens) {
// Add the new part if it's not an empty string
if (i > partStart) {
parts.push(value.substring(partStart, i));
}
partStart = i + 1;
}
break;
}
}
// Add the last part
if (partStart < value.length) {
parts.push(value.substring(partStart));
}
return parts;
}
//# sourceMappingURL=tokenizeWithParentheses.js.map