npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mockexp

v0.1.0

Published

Mockup value generator using RegExp pattern

Downloads

7

Readme

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.charset

Define the default charset used for ., negated charset [^...] and the classes \D, \W, \S. 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 this custom charset will be used instead.

MockExp.maxRepetition

Define 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 + MocExp.maxRepetition (inclusive). By default its value is 10.

MockExp.maxAttempts

Define the maximum failed attempts to generate a 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

Define 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

Define custom expressions to include 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 re-use other predefined expressions. If the expression is not defined then the output will be x{...} as normally handled by RegExp.

MockExp.install()

Used to monkey patch RegExp with generate method if needed. 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 respecting 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.
    • Generate 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.