Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

object-literal-parse

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object-literal-parse

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

  • 2.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
150
increased by33.93%
Maintainers
1
Weekly downloads
 
Created
Source

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 the 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}"] ]
    

Keywords

FAQs

Package last updated on 19 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc