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

parse-string-data

v1.0.0

Published

Parse data (object literal / array) from a text string and return a data structure

Downloads

4

Readme

parse-string-data

Parse data (object literal / array) from a text string and return a data structure

This is particularly useful for extracting data from a text string, that doesnt conform to the JSON spec and will fail when using JSON.parse. e.g. data stored as a javascript object, without quotes surrounding the key:value pairs

The NPM distribution is shipped with the following files:

  • index.js: Babel transpiled version for general use
  • index.es6.js - Original ES6 version for platforms that support ES6

Installing

Run the following from your projects' root folder to install and save as a dependency in your package.json

npm install parse-string-data --save

Syntax

parseStringData(str[,initialValue])

Parameters

str (string) A string containing your data

initialValue (Object|Array) An initial value for your data to be assigned to.

( You must be sure of what data you are retrieving to use this parameter. e.g. if your data is an object, ensure you are passing through an object. Ditto for Array.)

Usage

Firstly, lets create a string containing some data, and require the module

var parseStringData = require('parse-string-data');
var str = 'Lorem ipsum dolor sit amet, consectetur adiplising elit. { foo: true, bar: false, buzz: bla }';

Call the module with the string as it's first argument.

var parsedData = parseStringData(str);

The data in the above string will be extracted and returned in a data structure. In this case, an Object.

{
 foo: true,
 bar: false,
 buzz: 'bla'
}

Recursion

When parsing the results, parse-string-data recursively checks the extracted value for data, allowing the data to be many levels deep

var str = 'Lorem ipsum dolor sit amet, consectetur adiplising elit. { foo: true, bar: false, buzz: [0, 10, asdf], beer: { type: paleale, size: pint, favourite: true, sizes: [pot, schooner, pint]  }  }';

Parsing the above string will result in the following:

{
  foo: true,
  bar: false,
  buzz: [
    0: 0, 
    1: 10, 
    2: 'asdf'
  ],
  beer: { 
    type: paleale,
    favourite: true,
    size: pint, 
    sizes: [
      0: 'pot', 
      1: 'schooner', 
      2: 'pint'
    ]
  }
}

Using an Initial Value

When invoking the function, an Initial Value can be used via it's 2nd argument. This is useful for merging with existing data or overwriting defaults.

var data = {
  lorem: 'ipsum',
  dolor: 'sit amet'
};
var merged = parseStringData(str, data);

The above returns the following

{
  lorem: 'ipsum',
  dolor: 'sit amet',
  foo: true,
  bar: false,
  buzz: 'bla'
}

Extending your projects' defaults

var defaults = {
  foo: true,
  bar: true,
  buzz: 'asdf'
}

var settings = parseStringData(str, defaults);

Settings now results in the following:

{
 foo: true,
 bar: false,
 buzz: 'bla'
}

Data Formatting

During parsing, the data values are run through a formatter to avoid returning values as strings. e.g.

  • 'true' and 'false' are formatted as Boolean
  • '0', '10', '-1' are formatted as a Number
  • 'null' is formatted as a null Object.

Additional formatting can be submitted by a pull-request.

Contributing

Please read CONTRIBUTING.md for details on the process for submitting pull requests.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

License

This project is licensed under the MIT License - see the LICENSE.md file for details