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

lame-json

v1.5.1

Published

Module that attempts to parse string values of an object into JSON.

Downloads

11

Readme

lame-json

NPM Version Build Status Coverage Status Dependency Status devDependency Status

Module that attempts to parse string values of an object into JSON.

Getting Started

Install the module with: npm install lame-json

Documentation

Sometimes JSON comes back with values as a string.

{
    "foo": "true",
    "bar": "123",
    "baz": "{\"hello\":\"world\"}",
    "qux": "[ 1, 2, 3 ]"
}

In some cases, these values are meant to be parsed into primitives or native JS objects:

{
    foo: true,
    bar: 123,
    baz: {
        "hello": "world"
    },
    qux: [ 1, 2, 3 ]
}

This module helps you do that.

Examples

Usage is very straight forward:

var lameJson = require('lame-json');
var data = {
    "foo": "true",
    "bar": "123",
    "baz": "{\"hello\":\"world\"}"
}

var newData = lameJson.parseJson(data);

/*
=> {
    foo: true,
    bar: 123,
    baz: {
        "hello": "world"
    }
}
*/

If a value cannot be parsed out of the string, the original value is returned:


var newData = lameJson.parseJson({
    "foo": "[ 1, 2, 3 }"
});

// => { "foo": "[ 1, 2, 3 }" }

If you want to parse a specific string value:

var newData = lameJson.parse('[1, 2, 3]');

// => [1, 2, 3]

If you want to parse many times using the same options, you can use the factory method returned by the module. The factory method accepts a set of options, which are then used to create a parser object that always uses those options:

var parser = lameJson({
    boolean: false
});

parser.parse('[1, 2, 3]');
// => [1, 2, 3]

parser.parse('false');
// => 'false'

parser.parseJson({
    "foo": "true",
    "bar": "123",
});
/*
=> {
    foo: 'true',
    bar: 123,
}
*/

API

lameJson(options)

Creates a lameJson parser object that alwayhs uses the passed in options. If you only need to use the functions once or twice, you can use the static functions documented below.

  • options.boolean {Boolean} - If true, parses booleans. Defaults to true.
  • options.float {Boolean} - If true, parses ints and floats. Defaults to true. This only works when the full string is used during parsing (e.g 123foo would be ignored)
  • options.exponential {Boolean} - If true, parses exponential numbers such as 2e2. Defaults to false. This only works when the full string is used during parsing (e.g 2e2foo would be ignored)
  • options.array {Boolean} - If true, attempts parse arrays. Defaults to true.
  • options.object {Boolean} - If true, attempts to parse objects. Defaults to true.

Returns: {Object} a parser object with two methods, parseJson and parse

lameJson.parseJson(obj [, options])

Try to parse string values out of JSON values. You can use this in place of JSON.parse(). Options are the same as above.

Returns: {Object} a new object with possibly parsed values.

lameJson.parse(str [, options])

Try to parse a possible value out of a string. This is the underlying implementation used by parseJson(). Options are the same as above.

Returns: {Object} a new object with possibly parsed values.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality.

To start contributing, install the git pre-push hooks:

make githooks

Before committing, lint and test your code:

make prepush

License

Copyright (c) 2018 Alex Liu.

Licensed under the MIT license.