Is this the correct way to reference `root` in new scopes?

I'm working through using the scope/module interface, to define new app-specific helper keywords to use. There are a couple of cases where I want to have a new hierarchy of keywords, but I don't want to duplicate the constraint from the "parent" in the child. For instance, if I have something like (as a contrived example):
import { type, scope } from "arktype";

const prefixedHex = scope({
root: "/^0x[0-9a-fA-F]+$/",
trim: () => prefixedHex.type("root").pipe((x) => x.substring(2)),
});

export const myScope = scope({
string: type.module({
prefixedHex: prefixedHex.export(),
}),
});
import { type, scope } from "arktype";

const prefixedHex = scope({
root: "/^0x[0-9a-fA-F]+$/",
trim: () => prefixedHex.type("root").pipe((x) => x.substring(2)),
});

export const myScope = scope({
string: type.module({
prefixedHex: prefixedHex.export(),
}),
});
Is prefixedHex.type("root") the correct/idiomatic way to refer to the regex above it or is there some simpler/cleaner to do it? Thanks.
2 Replies
ssalbdivad
ssalbdivad2w ago
This looks pretty reasonable to me, but another option would be tuple expressions. They allow you to use native scope syntax with expressions like pipe:
const prefixedHex = scope({
root: "/^0x[0-9a-fA-F]+$/",
trim: ["root", "=>", x => x.substring(2)]
})
const prefixedHex = scope({
root: "/^0x[0-9a-fA-F]+$/",
trim: ["root", "=>", x => x.substring(2)]
})
Every expression like pipe or narrow has an equivalent tuple expression you can see by selecting that syntax tab in the example on the docs (most of them are just the corresponding operator in a tuple like [A, "|", B] for a union or [A, "[]"] for an array)
alexander
alexanderOP2w ago
Ah, ok, that makes sense. Thank you.

Did you find this page helpful?