Code Monkey home page Code Monkey logo

regexparam's Introduction

regexparam Build Status

A tiny (299B) utility that converts route patterns into RegExp. Limited alternative to path-to-regexp ๐Ÿ™‡

With regexparam, you may turn a pathing string (eg, /users/:id) into a regular expression.

An object with shape of { keys, pattern } is returned, where pattern is the RegExp and keys is an array of your parameter name(s) in the order that they appeared.

Unlike path-to-regexp, this module does not create a keys dictionary, nor mutate an existing variable. Also, this only ships a parser, which only accept strings. Simiarly, and most importantly, regexparam only handles basic pathing operators:

  • Static (/foo, /foo/bar)
  • Parameter (/:title, /books/:title, /books/:genre/:title)
  • Optional Parameters (/:title?, /books/:title?, /books/:genre/:title?)
  • Wildcards (*, /books/*, /books/:genre/*)

Lastly, please note that while this route-parser is not slow, you should use matchit or trouter if performance is of critical importance. This is epsecially true for backend/server scenarios!

This module exposes two module definitions:

  • ES Module: dist/regexparam.es.js
  • CommonJS: dist/regexparam.js

Install

$ npm install --save regexparam

Usage

const regexparam = require('regexparam');

let foo = regexparam('users/*');
// foo.keys => ['wild']
// foo.pattern => /^\/users\/(.*)(?:\/)?\/?$/i

let bar = regexparam('/books/:genre/:title?')
// bar.keys => ['genre', 'title']
// bar.pattern => /^\/books\/([^\/]+?)(?:\/([^\/]+?))?(?:\/)?\/?$/i

bar.pattern.test('/books/horror'); //=> true
bar.pattern.test('/books/horror/goosebumps'); //=> true

// Example param-assignment
function exec(path, result) {
  let i=0, out={};
  let matches = result.pattern.exec(path);
  while (i < result.keys.length) {
    out[ result.keys[i] ] = matches[++i] || null;
  }
  return out;
}

exec('/books/horror', bar);
//=> { genre:'horror', title:null }

exec('/books/horror/goosebumps', bar);
//=> { genre:'horror', title:'goosebumps' }

Important: When matching/testing against a generated RegExp, your path must begin with a leading slash ("/")!

API

regexparam(str)

Returns: Object

str

Type: String

The route/pathing string to convert.

Note: It does not matter if your str begins with a / -- it will be added if missing.

License

MIT ยฉ Luke Edwards

regexparam's People

Contributors

lukeed avatar

Stargazers

Roman avatar

Watchers

James Cloos 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.