patmat-ts
TypeScript icon, indicating that this package has built-in type declarations

0.0.0 • Public • Published

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

Package Sidebar

Install

npm i patmat-ts

Weekly Downloads

2

Version

0.0.0

License

MIT

Unpacked Size

4.07 kB

Total Files

5

Last publish

Collaborators

  • me7uiz