36 lines
1.1 KiB
Plaintext
Executable File
36 lines
1.1 KiB
Plaintext
Executable File
import { invariant } from './utils';
|
|
/**
|
|
* https://tc39.es/ecma402/#sec-partitionpattern
|
|
* @param pattern
|
|
*/
|
|
export function PartitionPattern(pattern) {
|
|
var result = [];
|
|
var beginIndex = pattern.indexOf('{');
|
|
var endIndex = 0;
|
|
var nextIndex = 0;
|
|
var length = pattern.length;
|
|
while (beginIndex < pattern.length && beginIndex > -1) {
|
|
endIndex = pattern.indexOf('}', beginIndex);
|
|
invariant(endIndex > beginIndex, "Invalid pattern ".concat(pattern));
|
|
if (beginIndex > nextIndex) {
|
|
result.push({
|
|
type: 'literal',
|
|
value: pattern.substring(nextIndex, beginIndex),
|
|
});
|
|
}
|
|
result.push({
|
|
type: pattern.substring(beginIndex + 1, endIndex),
|
|
value: undefined,
|
|
});
|
|
nextIndex = endIndex + 1;
|
|
beginIndex = pattern.indexOf('{', nextIndex);
|
|
}
|
|
if (nextIndex < length) {
|
|
result.push({
|
|
type: 'literal',
|
|
value: pattern.substring(nextIndex, length),
|
|
});
|
|
}
|
|
return result;
|
|
}
|