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-forge

v1.0.0

Published

A simple library that performs deep key remapping / special object compression when given a root object or array.

Downloads

9

Readme

json-forge

This README outlines the details of collaborating on this library.

This library serves as client-side json 'forge' to remap a where object to waterline syntax, and as a server-side type interpreter to handle special types that cannot be represented purely in JSON. A good example would be Mongo Object ID's or Date Objects. A forge instance will rebuild and return a new object without disrupting the original object.

Prerequisites

You will need the following things properly installed on your computer.

Installation

  • git clone <repository-url> this repository
  • cd json-forge
  • npm install

Running / Development

Testing

  • npm run test

Code Formatting

  • npm run format

Linting

  • npm run lint
  • npm run lint:tests

Building

  • npm run build

!!!WARNING!!!

Ensure that you do not send objects with circular references to JSONForge! You will crash your JS VM!

Usage

This library exports a single class called JSONForge.

When creating a new forge instance a single input object options is expected.

This options object can have the following definition keys:

map, prune, compressors.

All are expected to be objects/maps.

map is expected to be a map of literals, where the left-hand value of the map will be substituted for the right-hand value of the map at every level of the input object.

prune is expected to be a map of truthy values, where all branches that match the left-hand value of the prune map will be discarded at every level of the input object.

compressors is expected to be a map of synchronous functions that returns a replacement object for the current object level if there is a left-hand key within the object level that matches the compressor key. Compressors can therefore be used to "mutate" an object level, or "compress" an object level down to a special type like a Date object or a Mongo ID.

Creating a basic forge instance would looke like:

import JSONForge from 'json-forge';

const forge = new JSONForge({
    compressors: {
        $date(v, k) {
            return new Date(v);
        }
    }
});

Invoking a forge is as easy as:

const newObject = forge.process({
    "publishedAt": {
        "<=": {
            "$date": "2018-03-08T09:05:24.447Z"
        }
    }
});

The above forge would compress $date objects like:

{
    "publishedAt": {
        "<=": {
            "$date": "2018-03-08T09:05:24.447Z"
        }
    }
}

to:

{
    "publishedAt": {
        "<=":  new Date("2018-03-08T09:05:24.447Z")
    }
}

For simple remapping, a forge like the following can be useful:

const forge = new JSONForge({
    map: {
        $and: 'and',
        $or: 'or',
        like: 'contains'
    }
});

This forge would convert the following object:

{
    $and: [
        {
            title: {
                like: 'This'
            }
        },
        {
            $or: [
                {
                    description: {
                        contains: 'Text'
                    }
                },
                {
                    classification: {
                        contains: 'U'
                    }
                }
            ]
        }
    ]
}

to:

{
    and: [
        {
            title: {
                contains: 'This'
            }
        },
        {
            or: [
                {
                    description: {
                        contains: 'Text'
                    }
                },
                {
                    classification: {
                        contains: 'U'
                    }
                }
            ]
        }
    ]
}

Additionally, forges can also prune values out of an object tree.

The following forge would prune all branches starting with key _title_:

const forge = new JSONForge({
    prune: {
        _title_: true
    }
});

Which would convert the following object:

{
    _title_: 'A Good Day to Die',
    topics: ['a', 'b', 'c']
}

to:

{
    topics: ['a', 'b', 'c']
}

Putting it all together, the following forge:

import JSONForge from 'json-forge';

const forge = new JSONForge({
    map: {
        $and: 'and',
        $or: 'or',
        like: 'contains',
        DATE: '$date'
    },
    prune: {
        _title_: true
    },
    compressors: {
        $date(v, k) {
            return new Date(v);
        },
        $objectId(v, k) {
            if (Array.isArray(v)) {
                return v.map(e => {
                    return e; //here you would handle wrapping many objectIds (if needed)
                });
            }
            return v; //here you would wrap a single element
        }
    }
});

Would convert this object:

{
    "_title_": "A Good Day to Die",
    "topics": [
        "a",
        "b",
        "c"
    ],
    "updatedAt": {
        "<=": {
            "DATE": "2018-03-08T09:05:24.447Z"
        }
    },
    "$and": [
        {
            "title": {
                "like": "This"
            }
        },
        {
            "$or": [
                {
                    "description": {
                        "contains": "Text"
                    }
                },
                {
                    "classification": {
                        "contains": "U"
                    }
                }
            ]
        }
    ],
    "publishedAt": {
        "<=": {
            "$date": "2018-03-08T09:05:24.447Z"
        }
    }
}

to:

{
    "topics": [
        "a",
        "b",
        "c"
    ],
    "updatedAt": {
        "<=": new Date("2018-03-08T09:05:24.447Z")
    },
    "and": [
        {
            "title": {
                "contains": "This"
            }
        },
        {
            "or": [
                {
                    "description": {
                        "contains": "Text"
                    }
                },
                {
                    "classification": {
                        "contains": "U"
                    }
                }
            ]
        }
    ],
    "publishedAt": {
        "<=": new Date("2018-03-08T09:05:24.447Z")
    }
}

You may have noticed, that re-mapped keys can flow seamlessly into compressors!

Forge On!