GithubHelp home page GithubHelp logo

url-value-parser's Introduction

build status Coverage Status

UrlValueParser

A helper ES6 class letting you extract values from URL paths, leaving the other parts untouched.

It uses an internal class ValueDetector determining what is a value and what is not. By default the following path chunks are considered values:

  • decimal numbers
  • strings in UUID format
  • hex numbers consisting of 7 or more characters and consistent lower or upper case
  • long base64 encoded strings
  • JSON Web Tokens (JWT)

You can customize all of the logic by providing options, overriding methods or providing your own value detector. See the source - it's easy, i promise.

Usage

const UrlValueParser = require('url-value-parser');
const parser = new UrlValueParser(/* {options} */);

parser.parsePathValues('/some/path/154/userId/ABC363AFE2');
/*
 here the values would be 154 and ABC363AFE2
 thus it returns:

  {
    chunks: ['some', 'path', '154', 'userId', 'ABC363AFE2'],
    valueIndexes: [2, 5]
  }
*/

parser.replacePathValues('/some/path/154/userId/ABC363AFE2', '#id');
// returns: /some/path/#id/userId/#id

Options

  • replaceMasks - use custom masks instead of built-in
  • extraMasks - add your custom masks additionally to the built-in ones
  • minHexLength - when using built-in masks, count only long enough HEX values, DEFAULT: 7
  • minBase64Length - when using built-in masks, count only long enough base64 values, DEFAULT: 66

If strings are provided in an array to replaceMasks and extraMasks, then they're automatically converted into RegExp

Example:

const parser = new UrlValueParser({
  minHexLength: 4,
  extraMasks: [
    /^z_.*$/,
    '^[0-9]+\\.[0-9]+$'
  ]
});

License

MIT No Attribution License

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.