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

jminor

v1.0.1

Published

JSON Minifier

Downloads

5

Readme

JMinor

✂️ A configurable Two-Way JSON minifier to reduce your payload size.

jminor Travis

What is it good for?

When passing data back and forth between servers and clients its always a good idea to reduce your payload size to the minimum, that will save you money and time for your users.

JMinor will help you achive this task by reducing the size of your payload with a generated payload translation dictionary.

Turn (~136 Bytes):

{
    "my_very_long_key": 1,
    "deep_object": {
        "a_falsy_value": false,
        "filled_array": [1, 2, 3],
        "empty_array": [],
        "another_array": [{
            "some_number": 0
        }]
    }
}

Into (~25 Bytes):

{
    "a": 1,
    "b": {
        "d": [1, 2, 3]
    }
}

And then back into (~61 Bytes):

{
    "my_very_long_key": 1,
    "deep_object": {
        "filled_array": [1, 2, 3]
    }
}

Install

Install via npm with

$ npm install --save jminor

Or use the CDN:

<script src="https://unpkg.com/jminor/umd/jminor.js"></script>

Or the minified version:

<script src="https://unpkg.com/jminor/umd/jminor.min.js"></script>

You can use JMinor both in the Client and the Service (or any other ECMAScript runtime).

Usage

Dictionary

The first thing to do is to create a Dictionary. The dictionary will hold all the original payload keys mapped to their translations.

import { createDictionary } from "jminor";

const dictionary = createDictionary();

createDictionary() is a factory function that receive a key generator factory.
A key generator is a module that generates dictionary keys.

createDictionary(keyGeneratorFactory)

| Name | Type | Required | Default | Description | | - | - | - | - | - | | keyGeneratorFactory | () => IKeyGenerator | false | createDefaultKeyGenerator | See below |

Dictionary API

dictionary.replaceKeyGenerator(keyGenerator)

| Name | Type | Required | Default | Description | | - | - | - | - | - | | keyGenerator | IKeyGenerator | true | - | - |

  • replaces the current key generator.
    Note! that this will reset the dictionary.

dictionary.fromJSON(data)

| Name | Type | Required | Default | Description | | - | - | - | - | - | | data | JSON | true | - | - |

  • This method will digest the json object passed to it, and generates a uniqe key for each property for later translation

dictionary.extendWith(data)

| Name | Type | Required | Default | Description | | - | - | - | - | - | | data | JSON | true | - | - |

  • After dictionary is craeted and generated, you can pass another object to extend the dictionary that was created with the fromJSON method.

dictionary.export()

  • Returns the generated dictionary as a raw object.

dictionary.import(rawDictionary)

| Name | Type | Required | Default | Description | | - | - | - | - | - | | rawDictionary | JSON | true | - | exported raw dictionary |

  • If you have exported dictionary received from the export method, you can import it.

dictionary.ktoc(key)

| Name | Type | Required | Default | Description | | - | - | - | - | - | | key | String | true | - | - |

  • Translates original key to generated key, if it exist in the dictionary.

dictionary.ctok(ckey)

| Name | Type | Required | Default | Description | | - | - | - | - | - | | ckey | String | true | - | - |

  • Translates generated key to original key, if it exist in the dictionary.

Compress

After we have our dictionary with the generated keys, we can go and compress some data.

import { compress } from "jminor";

const compressed = compress(data, dictionary, config);

compress(data, dictionary, config)

| Name | Type | Required | Default | Description | | - | - | - | - | - | | data | JSON | true | - | A JSON with keys that presented in the dictionary | | dictionary | Dictionary | true | - | - | | config | ICompressConfig | false | See below | - |

Config and defaults

{
    // Translate object keys
    // Default: true
    translateKeys: true,

    null: {

        // Remove null values (null)
        // Default: false
        removeNull: false,

        // If removeNull is true, exclude this keys
        // Default: []
        exclude: []
    },

    boolean: {

        // Remove false values (false)
        // Default: false
        removeFalse: false,

        // If removeFalse is true, exclude this keys
        // Default: []
        exclude: []
    },

    string: {

        // Remove empty strings ("")
        // Default: false
        removeEmpty: false,

        // If removeEmpty is true, exclude this keys
        // Default: []
        exclude: []
    },

    number: {

        // Remove zero values (0)
        // Default: false
        removeZero: false,

        // If removeZero is true, exclude this keys
        // Default: []
        exclude: []
    },

    object: {

        // Remove empty objects ({})
        // Default: false
        removeEmpty: false,

        // If removeEmpty is true, exclude this keys
        // Default: []
        exclude: []
    },

    array: {

        // Remove empty arrays ([])
        // Default: false
        removeEmpty: false,

        // If removeEmpty is true, exclude this keys
        // Default: []
        exclude: []
    }
}

Note! All config keys are optionals

Decompress

After compressing some data we can decompres it.
Note! that some data may be truncated based on your compress config.

import { decompress } from "jminor";

const data = decompress(compressed, dictionary);

decompress(compressed, dictionary)

| Name | Type | Required | Default | Description | | - | - | - | - | - | | compressed | JSON | true | - | A JSON with keys that presented in the dictionary | | dictionary | Dictionary | true | - | - |

Generators

JMinor comes with two built in key generators:

  • DefaultKeyGenerator - generates keys in the form of aaa, aab, zxc etc.

  • NumericKeyGenerator - generates keys in the form of a numeric ascending series.

You can create your own key generator, if you will, you should implement the IKeyGenerator interface.

See the generators/ folder for source example

License

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