const pattern = pat({
start: true,
sequence: [
{ type: 'word' },
{ type: 'digit', repeat: 3 },
],
end: true,
})
const regex = pattern.toRegex(); // Resultado: /^\w\d{3}$/
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();