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

test-object-literal-parse-module

v0.0.0

Published

Splits an object literal string into a set of top-level key-value pairs

Downloads

4

Readme

Javascript Object Literal Parser

Splits an object literal string into a set of top-level key-value pairs.

By Michael Best

License: MIT (http://www.opensource.org/licenses/mit-license.php)

Exports a single function, parseObjectLiteral(str) that accepts an object literal string (with or without enclosing braces) and returns an array of key-value string pairs. The key are strings, but unquoted keys are accepted as long as they don't contain these special characters: ,:()[]{}'"/. The value is any valid Javascript expression; the parser will return it as a string (without evaluating it).

Notes: This parser is more lenient than a standard JavaScript parser, accepting unquoted keys with special characters and accepting keys without a value. It also silently ignores errors such as a missing key (the value will be skipped). If a value expression contains unquoted spaces, they may be stripped in the result.

Acknowledgments: This parser's functionality is based on the parser included in Knockout 2.x by Steven Sanderson. The examples below are also based on the specs in Knockout. This parser's implementation is inspired by json-sans-eval by Mike Samuel. Compared to the Knockout 2.x parser, this one is smaller and about three times as fast. A version of this parser is now included in Knockout as of version 3.0.

Examples

  • Simple object literals

    parseObjectLiteral("a: 1, b: 2, \"quotedKey\": 3, 'aposQuotedKey': 4");
    [ ["a","1"], ["b","2"], ["quotedKey","3"], ["aposQuotedKey","4"] ]
  • With enclosing braces

    parseObjectLiteral("{a: 1}");
    [ ["a","1"] ]
  • String values

    parseObjectLiteral(
          "a: \"comma, colon: brace{ bracket[ apos' escapedQuot\\\" end\","
        + "b: 'escapedApos\\' brace} bracket] quot\"'");
    [
        ["a","\"comma, colon: brace{ bracket[ apos' escapedQuot\\\" end\""],
        ["b","'escapedApos\\' brace} bracket] quot\"'"]
    ]
  • Values with objects, arrays, function literals, and regular expressions

    parseObjectLiteral(
          "myObject:{someChild:{}, someChildArray:[1,2,3], \"quotedChildProp\":'string value'},\n"
        + "someFn:function(a,b,c){var regex=/}/g;var str='/})({';return{};},"
        + "myArray:[{}, function(){}, \"my'Str\", 'my\"Str']"
        );
    [
        ["myObject","{someChild:{},someChildArray:[1,2,3],\"quotedChildProp\":'string value'}"],
        ["someFn","function(a,b,c){var regex=/}/g;var str='/})({';return{};}"],
        ["myArray","[{},function(){},\"my'Str\",'my\"Str']"]
    ]
  • Keys with special characters

    parseObjectLiteral("a.b: 1, b+c: 2, c=d: 3, d_e: 4");
    [ ["a.b","1"], ["b+c","2"], ["c=d","3"], ["d_e","4"] ]
  • Keys without values, values without keys, etc.

    parseObjectLiteral("keyonly, 'key:quoted', key:, {nokey: 4}");
    [ ["keyonly",undefined], ["key:quoted",undefined], ["key",undefined], [undefined,"{nokey:4}"] ]