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

key-val-parser

v3.0.0

Published

A parser that parses key value pairs in the form of name=john age=23 address="1234 Somewhere"

Downloads

3

Readme

CircleCI npm version License

key-val-parser

This is a parser written in Javascript that helps parse key value pairs of the format "key1=value1 key2=value2". The resultant value is a JSON object that contains all the keys and their associated values. Additionally values might be quoted to contain sentences or multiple words that are space separated.

Motivation

I was writing a Slack app and I had to receive arguments as a series of key value pairs. I shopped around and didn't find a package that suited my needs.

Installation

npm install key-val-parser

Usage

var Parser=require('key-val-parser');

var p=new Parser();

var parsed=p.parse("name=john age=23");

console.log("Number of keys parsed",parsed.length());
console.log("value of name=",parsed.name);
console.log("value of age=",parsed.age);

The above code will produce

Number of keys parsed 2
value of name= john
value of age= 23

Examples

Regular Parser
var Parser=require('key-val-parser').Parser;
var p=new Parser();

// Single key
> p.parse("username=john");
Parsed { username: 'john' }

// Multiple keys on the same line separated by whitespace
> p.parse("username=john age=23");
Parsed { username: 'john', age: '23' }

// Single key with space between key, assignment operator and value
> p.parse("username = john");
Parsed { username: 'john' }

// Multiple keys with spaces between keys, assignment operator and values
> p.parse("username = john age = 23");
Parsed { username: 'john', age: '23' }

// Single key with quoted value
> p.parse("username=\"John Doe\"");
Parsed { username: 'John Doe' }

// Multiple keys with quoted values
> p.parse("username=\"John Doe\" address=\"No 1 Janpath, New Delhi\"");
Parsed { username: 'John Doe', address: 'No 1 Janpath, New Delhi' }

// Multiple keys with both quoted and unquoted values
> p.parse("username=johndoe address=\"No 1 Janpath, New Delhi\"");
Parsed { username: 'johndoe', address: 'No 1 Janpath, New Delhi' }

These are broadly the cases that it will cover.

Strict Parser

There is also a Strict Parser available that allows you to specify valid keys. If the parser encounters an invalid key, it will raise an error. This is useful in cases where you want to whitelist certain options. For example, a config file.

var StrictParser=require('key-val-parser').StrictParser;
var sp=new StrictParser(["name","age"]);

> sp.parse("name=john age=23 color=blue") // Error: Invalid key

Note: The rest of the parsing stays the same as a regular parser, and it will handle all the cases involving leading spaces, quotes etc.

Error handling

Errors will be thrown if the parser encounters missing keys, values or assignment operators.

var Parser=require('key-val-parser');
var p=new Parser();

// Missing value
p.parse("username=");  // Error: missing value

// Missing key
p.parse("=john"); // Error: missing key

// Missing assignment operator
p.parse("username john"); // Error: missing assignment operator

// Incomplete key value pair
p.parse("key"); // Error: incomplete key value pair

Unsupported Cases

Keys with spaces

p.parse("user name=john");

Keys containing non-alphanumeric characters

p.parse("abc123#$$=john"); // It will simply ignore special characters

Quoted keys

p.parse("\"user name\"=john");

Quoted values containing nested quotes

p.parse("quotation=\"she said \"stop!\"\"");