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

@rickzaki/json-stringify-sorted

v1.0.2

Published

a variation on JSON.stringify which allows for sorting keys

Downloads

8

Readme

Issues MIT License LinkedIn

About The Project

Objects by their nature are an unordered collection of properties. JS engines typically serialize objects in consistent ways. This is perfectly fine for system consumption. However; human readability can be greatly improved by forcing a particular order. The use case arose by the desire to save JSON to disk, review by humans and comparison tools.

Getting Started

Prerequisites

  • Node.js with npm

Installation

Using npm:

npm install @RickZaki/json-stringify-sorted

Usage

CommonJS

const jsonStringifySorted = require('json-stringify-sorted');

ECMAScript Modules

import * as jsonStringifySorted from 'json-stringify-sorted';

examples

Simple example

cont sampleObject = {d:[8,6,7,5,3,0,9], a: 1, c:true, b:"b"};
const sortedObject = jsonStringifySorted(sampleObject);
console.log(sortedObject); // {"a":1,"b":"b","c":true,"d":[8,6,7,5,3,0,9]}

AS you can see arrays remain unaltered.

Nested example

cont sampleObject = {d:{h:"h", e:"e"}, a: 1, c:true, b:"b"};
const sortedObject = jsonStringifySorted(sampleObject);
console.log(sortedObject); // {"a":1,"b":"b","c":true,"d":{"e":"e","h":"h"}}

Nested objects are also sorted

Key Order

The second optional paramter is used to determine the sort order of the keys.

cont sampleObject = {d:{h:"h", e:"e"}, a: 1, c:true, b:"b"};
const sortedObject = jsonStringifySorted(sampleObject, ['a', 'e', 'd', 'c']);
console.log(sortedObject); // {"a":1,"d":{"e":"e"},"c":true}

a few ccaveats with this use case:

  1. Keys not provided in the order array are omitted from the result
  2. The order for root and nested object keys are intertwined. Should a nested object have the same keys, the order will be identical. You cann specify a before b on the ancestor, but b before a on descendant.

Custom sort function

As the utility uses Array.sort, you can supply a custom sort compare function. Here's one that forces some keys in order, and the remaining in alphabetical order.

const compareFn = (a, b) =>{
    const keyOrder = ['c', 'b'];
    const aIndex = keyOrder.indexOf(a);
    const bIndex = keyOrder.indexOf(b);
    if (aIndex > -1 || bIndex > -1) {
        if (aIndex == -1) {
            return 1;
            } else if (bIndex == -1) {
            return -1;
            } else if (aIndex < bIndex) {
            return -1;
            } else {
            return 1;
            }
    }
    if (a < b) {
        return -1;
    } else {
        return 1;
    }
};
cont sampleObject = {d:{h:"h", e:"e"}, a: 1, c:true, b:"b"};
const sortedObject = jsonStringifySorted(sampleObject, compareFn);
console.log(sortedObject); // {"c":true,"b":"b","a":1,"d":{"e":"e","h":"h"}}

Formatting

the third optional parameter is an number of spaces to include in the output. Just like JSON.strigify

cont sampleObject = { a: 1, c:true, b:"b"};
const sortedObject = jsonStringifySorted(sampleObject, null, 2);
console.log(sortedObject); /*
{
  "a": 1,
  "b": "b",
  "c": true
}
*/

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Rick Zaki - GitHub-at-RickZaki.com

Project Link: https://github.com/RickZaki/json-stringify-sorted