patmat-ts
v0.0.0
Published
A humanable pattern match library for JavaScript and TypeScript
Downloads
14
Maintainers
Readme
DSL (Domain-Specific Language) based
const pattern = pat`
start
word
then
digit repeat(3)
end
`;
const regex = pattern.toRegex(); // Resultado: /^\w\d{3}$/
Nested Object based
const pattern = pat({
start: true,
sequence: [
{ type: 'word' },
{ type: 'digit', repeat: 3 },
],
end: true,
})
const regex = pattern.toRegex(); // Resultado: /^\w\d{3}$/
Class based
class Pat extends RegexReadable {
constructor() {
super();
this.pattern = '';
}
start() {
this.pattern += '^';
return this;
}
word() {
this.pattern += '\\w';
return this;
}
digit(repeat = 1) {
this.pattern += `\\d{${repeat}}`;
return this;
}
end() {
this.pattern += '$';
return this;
}
toRegex() {
return new RegExp(this.pattern);
}
}
const pattern = new Pat()
.start()
.word()
.digit(3)
.end()
.toRegex();