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

string-object-to-object

v1.0.3

Published

Parse object in string format to a javascript object

Downloads

3

Readme

Parses an object in string format to a javascript object

Parses values of type boolean, strings, numbers, undefined, null, array and object. Regex to do at a later time.

Comments and whitespace in the string are ignored.

Two apis

a) stringToObject / stringToAny

stringToObject is to be used if you expect the string to only be an object. It will throw an error with message 'String is not an object' if not an object.

const example = stringToObject(`{
      double: "hello",
      single:'world' ,
      multiline : 'm\
ultiline',
      concatenation:'hello' + 'world',
      isTrue:true,
      isFalse:false,
      isUndefined:undefined,
      isNull:null,
      binary:0b1010,
      positiveBinary:+0b1010,
      negativeBinary:-0b1010,
      binaryWithSeparators:0b1_0_1_0,
      octalLegacy:0644,
      octal:0o7, // with signs and separators
      hex:0xAbCD, // with signs and separators
      integer:1234567890, // with signs and separators
      float1:0.0123456789 /* with signs */,
      float2:.0123456789, // with signs
      float3:2e2,
      float4:2_0_1.0_1e3_2,
      binaryBigInt:0b1010n, // and hex, octal and integer
      arrayEmpty:[],
      array:[1,,2],
      nestedArray:[
        [1,2],
        [
          'one',
          'two',
        ]
      ],
      nestedObject:{nested1:'one'}
    }`);

stringToAny will work with any allowed type represented as a string. It will throw error with message 'Malformed' if malformed.

const example = stringToAny(`[1,'Two',true,{p:'v'}]`)';

b) StringToObjectParser

This works a character at a time returning a ProcessResult.
If malformed will return ProcessResult.Break When completed call complete. Do not pass the first {

enum ProcessResult{Continue,Completed,Break} 
const stringToObjectParser = new StringToObjectParser();
stringToObjectParser.process('h'); // ProcessResult.Continue
stringToObjectParser.process('i'); // ProcessResult.Continue
stringToObjectParser.process(':'); // ProcessResult.Continue
stringToObjectParser.process('"'); // ProcessResult.Continue
stringToObjectParser.process('w'); // ProcessResult.Continue
stringToObjectParser.process('o'); // ProcessResult.Continue
stringToObjectParser.process('r'); // ProcessResult.Continue
stringToObjectParser.process('l'); // ProcessResult.Continue
stringToObjectParser.process('d'); // ProcessResult.Continue
stringToObjectParser.process('"'); // ProcessResult.Continue
stringToObjectParser.process('}'); // ProcessResult.Completed
expect(stringToObjectParser.getCompleted()).toEqual({hi:'world'});