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

json-loose

v1.2.3

Published

Transforms loosely structured plain object strings into valid JSON strings

Downloads

1,497

Readme

json-loose

A utility to handle and transform loosely structured data into valid JSON strings. It offers an intuitive solution to ensure data consistency and compatibility, providing a safer alternative to using the Function constructor to obtain an object from a string.

Install

You can install this module using npm or yarn, it's only 2.93 kB │ gzip: 1.32 kB:

npm i json-loose
# or
yarn add json-loose

Alternatively, you can also include this module directly in your HTML file from CDN:

| Type | URL | | :--- | :---------------------------------------------------------- | | ESM | https://cdn.jsdelivr.net/npm/json-loose/+esm | | CJS | https://cdn.jsdelivr.net/npm/json-loose/dist/index.cjs | | UMD | https://cdn.jsdelivr.net/npm/json-loose/dist/index.umd.js |

Usage

The jsonLoose function takes an invalid JSON string as input and returns a JSON-like string representation of the transformed data.

import jsonLoose from 'json-loose'

const invalidJSON = `
{
  name: 'Bambang Ekalaya',
  username: "@palgunadi",
  age: 30,
  isStudent: true
}
`
const data = jsonLoose(invalidJSON)
// now you can `JSON.parse(data)`

console.log(data)

Yields:

{
  "name": "Bambang Ekalaya",
  "username": "@palgunadi",
  "age": 30,
  "isStudent": true
}

You can also specify an optional context object to transform Identifier values:

import jsonLoose from 'json-loose'

const invalidJSON = `
[
  "foo",
  true,
  [1, 2, wife],
  {
    [n]: 'Bambang Ekalaya',
    username: "@palgunadi",
    age: 30,
    address: [{city:city}, [country]],
    skills: skills,
    isStudent: true,
    relation: {
      wife: wife,
      guru: guru,
      [bar]: "qux"
    },
  },
],
`

const context = {
  n: 'name',
  skills: 'Archery',
  city: 'Nishada',
  country: 'Aravalli',
  wife: 'Anggraini',
  guru: 'Drona'
}

const data = jsonLoose(invalidJSON, context)
// now you can `JSON.parse(data)`

console.log(data)

Yields:

[
  "foo",
  true,
  [1, 2, "Anggraini"],
  {
    "name": "Bambang Ekalaya",
    "username": "@palgunadi",
    "age": 30,
    "address": [{ "city": "Nishada" }, ["Aravalli"]],
    "skills": "Archery",
    "isStudent": true,
    "relation": { "wife": "Anggraini", "guru": "Drona", "bar": "qux" }
  }
]

Related

  • attributes-parser – A utility for parsing and tokenizing attributes string into meaningful tokens and key-value pairs.
  • js-tokens – A JavaScript tokenizer that never fails.

Benchmarks

  name                hz     min      max    mean     p75     p99    p995    p999      rme  samples
· json-loose  109,620.27  0.0077   0.7080  0.0091  0.0083  0.0168  0.0363  0.2052   ±1.19%    54811   fastest
· js-tokens    54,156.77  0.0116  24.3425  0.0185  0.0122  0.0247  0.0643  1.1352  ±15.68%    27267

Contributing

We 💛  issues.

When committing, please conform to the semantic-release commit standards. Please install commitizen and the adapter globally, if you have not already.

npm i -g commitizen cz-conventional-changelog

Now you can use git cz or just cz instead of git commit when committing. You can also use git-cz, which is an alias for cz.

git add . && git cz

License

GitHub

A project by Stilearning © 2023.