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

sortedjson

v0.2.9

Published

JSON.stringify with keys sorted. Supports custom ordering, replacer and space.

Downloads

39

Readme

sortedjson

JSON.stringify with keys sorted. Supports custom ordering, replacer and space.

Some features are inspired by JSON.sortify:

  • Hoping that the platform's native JSON.stringify will write the keys in the order they were added to the object, instead of re-implementing our own stringify.
  • Using a backup of the original JSON.stringify so it can be overwritten with this function.

Despite taking these ideas, this library was written anew from scratch because I hope my version will be faster for default sort order with no checks for circular reference.

API

sortedJson(data[, replacer][, space])

Behaves like native JSON.stringify, just with the keys sorted in default Array.prototype.sort() order.

  • Non-standard space: Negative numbers mean to use n=-space space characters for indentation but removing the 2nd and 3rd character of the result if they happen to be a newline followed by a space character. In cases where data is an array or an object, removing that newline and space will put the first item or key on the same line as the opening bracket. Default: -2
    • Does not apply if the sortOpts.stfy option is used (see below).

sortedJson(data, [replacer,] space[, sortOpts])

Like above, except if¹ you provide a number or a string as space, you may optionally provide your own sortOpts, too, which can be either a function or an options object. A shallow copy of sortOpts will be passed as the how argument to sortObj(), possibly with keyPrefix and keySuffix modified so don't rely on them. Some additional options in sortOpts are supported by sortedJson:

  • stfy: a function to use instead of the original (probably native) JSON.stringify in order to encode the sorted object. If a replacer is given as well, the replacer will always be applied using the original JSON.stringify.
    • If you're going to use univeil's jsonify as stfy, you may instead use the pre-made combination safe-sortedjson.

(¹ Requires space because a function that has no space argument before it could be confused with a replacer function.)

sortedJson.preset([replacer,] space[, sortOpts])

Return a function that accepts one argument data, remembers all the other arguments listed above, calls sortedJson with data` and the above arguments, and returns the result.

sortedJson.sortObj(data[, sortOpts])

If data is an array or not an object, return it verbatim. If it is a non-array object, return a copy that has its keys sorted and all its non-array object values .sortObj()ed as well.

Usage

From test/usage.js:

var sortedJson = require('sortedjson'), pets = {
  dog: { sounds: [ 'woof' ],            colors: [ 'grey', 'white' ] },
  cat: { colors: [ 'white', 'orange' ], sounds: [ 'meow', 'purr' ]  },
  ant: { colors: [ 'red', 'black' ] },
};
console.log(sortedJson(pets));

Output: test/usage.json

Known issues

  • Needs more/better tests and docs.
  • Versions 0.1.x did not work around the array index priority problem described in deepsortobj.

Related projects

License

ISC