Replace StringRange w/ an actual StrRVal + more command implementations
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import {Command, ParseContext} from "./command.js";
|
||||
import {Command, hashStrRVal, ParseContext, unwrapDestructured, wrapDestructured, wrapString} from "./command.js";
|
||||
import {LexInput} from "../lexer.js";
|
||||
import {StrVM} from "../vm.js";
|
||||
import {Awaitable} from "../../util/types.js";
|
||||
|
||||
export class Unique extends Command<{}> {
|
||||
isParseCandidate(token: LexInput): boolean {
|
||||
@@ -13,4 +15,22 @@ export class Unique extends Command<{}> {
|
||||
getDisplayName(): string {
|
||||
return 'unique'
|
||||
}
|
||||
|
||||
execute(vm: StrVM): Awaitable<StrVM> {
|
||||
return vm.tapInPlace(ctx =>
|
||||
ctx.replaceSubject(sub => {
|
||||
const seen: Record<string, boolean> = {}
|
||||
return wrapDestructured(
|
||||
unwrapDestructured(sub)
|
||||
.filter(part => {
|
||||
const hash = hashStrRVal(wrapString(part.value))
|
||||
if ( seen[hash] ) {
|
||||
return false
|
||||
}
|
||||
|
||||
seen[hash] = true
|
||||
return true
|
||||
}))
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user