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

params-parser

v0.0.3

Published

A parser for parsing a string into parameters object

Downloads

8

Readme

paramsParser

A parser for parsing a string into parameters object.

Parameters parser will sequentially use provided rules to match a input string, trying to extract parameters from it. If a rule fails to parse the input, parser will try next one, till all the rules are tried. The parser will return the result as soon as a succeeded parse occurs.

Usage

Once require the module, you could use it as a constructor. Pass your rules array into it, it will return a parser based on the rules.

var ParamsParser = require('paramsParser');
var rules = [rule1, rule2, rule3];
var parser = new ParamsParser(rules);
var params = parser.parse(inputString);

The parser will apply the rules one by one to the input string, till he make a success. If all the rules return non-true values, a NO_MATCH_RULE error will be thrown.

Rules

In paramsParse, you can use regular expression or a function to express your parsing rules.

regular express

If rule.reg is given, paramsParser will match the input string with it, and pass the result into getter. If the result returned from getter is true, we'll regard it as a succeeded match.

var rule = {

  // a regex object or a string, will use it with .match
  reg: /https*:\/\/([^\/]+)(.*)/, 

  // a getter function
  getter: function (result) {

    return {
      url: result[0],
      host: result[1],
      path: result[2]
    };

  } // a function

}

try {

  var parser = new ParamsParser([rule]);
  var params = parser.parse('http://www.fizz.com/buzz.html');
  console.log(params.url); // http://www.fizz.com/buzz.html
  console.log(params.host); // www.fizz.com
  console.log(params.path); // /buzz.html

} catch (err) {

  console.log(err); // PARAMS_PARSER * err -> NO_MATCH_RULE * input -> xxxx

}

getter function only

If rule.reg is not provided, paramsParser will pass the input string into the getter function, and regards a non-false return as a succeeded parse result.


var rule = {

  getter: function (inStr) {

    var url = require('url');
    var result = url.parse(inStr, true);
    
    return {
      url: result.href,
      path: result.path + '&numberOfTheBeast=666',
      fizz: 'buzz'
    };

  }

};
var parser = new ParamsParser([rule]);

try {

  var params = parser.parse('http://www.booo.com/search.html');
  console.log(params.url); // http://www.booo.com/search.html
  console.log(params.path); // search.html
  console.log(params.fizz); // buzz

} catch (err) {

  console.log(err);

}