Introduce TermOperator helper and refactor execute()s to use replaceSubjectMatchingTerm
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import {Command, ParseContext, unwrapString, wrapDestructured} from "./command.js";
|
||||
import {Command, ParseContext} from "./command.js";
|
||||
import {LexInput} from "../lexer.js";
|
||||
import {StrVM} from "../vm.js";
|
||||
import {Awaitable} from "../../util/types.js";
|
||||
@@ -17,17 +17,16 @@ export class Words extends Command<{}> {
|
||||
}
|
||||
|
||||
execute(vm: StrVM): Awaitable<StrVM> {
|
||||
return vm.tapInPlace(ctx =>
|
||||
ctx.replaceSubject(sub => {
|
||||
const val = unwrapString(sub)
|
||||
const separators = [...val.matchAll(/\s+/sg)]
|
||||
const parts = val.split(/\s+/sg)
|
||||
return vm.replaceContextMatchingTerm({
|
||||
destructure: sub => {
|
||||
const separators = [...sub.matchAll(/\s+/sg)]
|
||||
const parts = sub.split(/\s+/sg)
|
||||
|
||||
return wrapDestructured(
|
||||
parts.map((part, idx) => ({
|
||||
prefix: idx ? separators[idx - 1][0] : undefined,
|
||||
value: part,
|
||||
})))
|
||||
}))
|
||||
return parts.map((part, idx) => ({
|
||||
prefix: idx ? separators[idx - 1][0] : undefined,
|
||||
value: part,
|
||||
}))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user