0.0.0 • Published 1 year ago

patmat-ts v0.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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();