GithubHelp home page GithubHelp logo

mockexp's Introduction

MockExp

Mockup value generator using RegExp pattern.

Installation

npm install mockexp

Import

ES Module

import MockExp from 'mockexp';

CommonJS

const MockExp = require('mockexp').default;

Browser

<script type="text/javascript" src="mockexp.min.js"></script>

Usage

let hi = new MockExp(/Hello{1,5} world!*/i);
hi.generate(); // -> "HElloOo woRLd!!!"
hi.generate(); // -> "HeLlO WORLD!"
hi.generate(); // -> "hellOoooO wOrLD"

// or by monkey patching RegExp
MockExp.install();
// then generating directly through RegExp
/Hello{1,5} world!*/i.generate(); // -> "heLLooOO WorLD!!!!!"

Supported Syntax

Standard

  • Literal characters
  • Wildcard: .
  • Disjunction: |
  • Charset: [...], [^...]
  • Quantifiers: ?, *, +, {n}, {n,}, {n,m}
  • Groups: (...), (?:...), (?<name>...)
  • Backreferences: \#, \k<name>
  • Assertions
    • Input boundaries: ^, $
    • Word boundaries: \b, \B
    • Lookahead: (?=...), (?!...)
    • Lookbehind: (?<=...), (?<!...)
  • Escape characters: \xHH, \uHHHH, \u{HHHHHH}, \cA, \###, \0, \t, \n, \v, \f, \r, [\b]
  • Escape classes: \d, \D, \s, \S, \w, \W
  • Unicode classes: \p{...}, \P{...}
  • Flags: i, g, m, s, u, y

Non-Standard

  • Expressions: \x{...}

Configuration

MockExp(pattern, flags?)

Accepts a string or RegExp pattern with optional RegExp flags. Only the flags i, s, u will affect the generated value.

MockExp.charset

Defines the default charset used for wildcards ., negated charsets [^...] and the classes \D, \W, \S, \P{...}. It must be a RegExp charset pattern [...].

If not set, the default charset is either [\u0020-\u007E] if u flag is not set, or [\u0020-\u0FFD] if u flag is set. Also, [\u000A\u000D\u2028\u2029] will be included if s flag is set.

However, if MockExp.charset is set to a custom charset then all the above will be ignored and the custom charset will be used instead.

MockExp.maxRepetition

Defines the maximum repetition limit for the quantifiers without an upper-limit: *, +, {n,}. The upper-limit will be a random number between the lower-limit and the lower-limit + MockExp.maxRepetition (inclusive). By default its value is 10.

MockExp.maxAttempts

Defines the maximum failed attempts to generate valid RegExp patterns before giving up with an Unsupported pattern exception. Certain RegExp patterns are impossible to generate (e.g. impossible assertion or limited default charset). By default its value is 1000.

MockExp.classes

Defines the unicode classes for \p{...} and \P{...}. Each defined class must be assigned a RegExp charset pattern [...]. It will only be respected if u flag is set and the class is defined in MockExp.classes.

MockExp.expressions

Defines custom expressions to be included in the pattern using the \x{...} non-standard syntax. Each defined expression must be assigned a value of either string, RegExp, MockExp, or function():string. Expressions can also reuse other predefined expressions. If the expression is not defined then the output will be x{...} as normally handled by RegExp.

MockExp.randomInt(min, max)

Generates random integers between min and max (inclusive) using Math.random. Override if you need to control the randomness behavior (e.g. for seeded or cryptographic randomness).

MockExp.install()

Monkey patch RegExp class with generate method. While monkey patching can provide ease of use, it should be noted that it is generally considered a bad practice.

Limitations

  • Experimental support for lookahead and lookbehind assertions.
    • Tries its best to generate a value that respects all lookahead and lookbehind assertions.
    • Throws an Unsupported pattern exception after it fails to generate the pattern (e.g. impossible pattern).
    • MockExp.maxAttempts controls how many attempts to try before it decides to give up on satisfying the assertions.
  • Limited support for unicode classes \p{...} and \P{...}.
    • Parses all \p{...} and \P{...} classes when u flag is set.
    • Generates based on defined Unicode classes in MockExp.classes.
    • Since the exact set of characters matched by unicode classes can change due to unicode standard changes, only manual definition of unicode classes is supported currently.
  • No support for the recent v mode flag.
    • No unicode classes intersection && or subtraction -- support.
    • No string literal in charset \q{...} support.
    • Extended unicode can be still generated in u mode.

Development

Uses eslint for linting, tsc for building, and jest for testing. All source files are under src, all test files are under tests, and all built files will go under lib.

# install dev-dependencies
npm install
# lint source
npm run lint
# build library
npm run build
# test source only
npm run test:src
# test source and library
npm run test
# clean build and test coverage
npm run clean

License

This work is licensed under the MIT license.

mockexp's People

Contributors

aaalsaleh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.