Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
37 lines
966 B
Plaintext
37 lines
966 B
Plaintext
import { invariant } from "./utils.js";
|
|
/**
|
|
* Partition a pattern into a list of literals and placeholders
|
|
* https://tc39.es/ecma402/#sec-partitionpattern
|
|
* @param pattern
|
|
*/
|
|
export function PartitionPattern(pattern) {
|
|
const result = [];
|
|
let beginIndex = pattern.indexOf("{");
|
|
let endIndex = 0;
|
|
let nextIndex = 0;
|
|
const length = pattern.length;
|
|
while (beginIndex < pattern.length && beginIndex > -1) {
|
|
endIndex = pattern.indexOf("}", beginIndex);
|
|
invariant(endIndex > beginIndex, `Invalid pattern ${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;
|
|
}
|