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

object-to-json-refs

v2.0.2

Published

Convert back and from object to JSON with references

Downloads

4

Readme

object-to-json-refs

Convert back and from object to JSON with references

Demo

Code:

const objectToJsonRefs = require('object-to-json-refs');

const obj = {
    actor: [
        { name: 'Robert Travolta' },
        { name: 'Jhon Schwarzenegger' },
        { name: 'Arnold De Niro' }
    ],
    fictionalCharacter: {
        male: [
            { name: 'Stan Mertens' },
            { name: 'Homer Smith' },
            { name: 'Finn Simpson' }
        ],
        female: [
            { name: 'Francine Bubblegum' },
            { name: 'Marge Ling' },
            { name: 'Princess Bouvier' }
        ]
    },
    myFavouriteCelebrities: [
    ],
    lastSeen: null
};
obj.myFavouriteCelebrities[0] = obj.actor[0];
obj.myFavouriteCelebrities[1] = obj.fictionalCharacter.male[2];
obj.lastSeen = obj.fictionalCharacter.female[0];

const result = objectToJsonRefs.ConvertToJsonRefsReady(obj);
console.log('forth:\n', JSON.stringify(result, null, 4));

const back = objectToJsonRefs.JsonRefReadyToObject(result);
console.log('back:\n', JSON.stringify(back, null, 4));

console.log('references still equal:\n', [
    back.myFavouriteCelebrities[0] === back.actor[0],
    back.myFavouriteCelebrities[1] === back.fictionalCharacter.male[2],
    back.lastSeen === back.fictionalCharacter.female[0]
]);

Output:


forth:
 {
    "actor": [
        {
            "name": "Robert Travolta"
        },
        {
            "name": "Jhon Schwarzenegger"
        },
        {
            "name": "Arnold De Niro"
        }
    ],
    "fictionalCharacter": {
        "male": [
            {
                "name": "Stan Mertens"
            },
            {
                "name": "Homer Smith"
            },
            {
                "$ref": "/myFavouriteCelebrities/1"
            }
        ],
        "female": [
            {
                "$ref": "/lastSeen"
            },
            {
                "name": "Marge Ling"
            },
            {
                "name": "Princess Bouvier"
            }
        ]
    },
    "myFavouriteCelebrities": [
        {
            "$ref": "/actor/0"
        },
        {
            "name": "Finn Simpson"
        }
    ],
    "lastSeen": {
        "name": "Francine Bubblegum"
    }
}
back:
 {
    "actor": [
        {
            "name": "Robert Travolta"
        },
        {
            "name": "Jhon Schwarzenegger"
        },
        {
            "name": "Arnold De Niro"
        }
    ],
    "fictionalCharacter": {
        "male": [
            {
                "name": "Stan Mertens"
            },
            {
                "name": "Homer Smith"
            },
            {
                "name": "Finn Simpson"
            }
        ],
        "female": [
            {
                "name": "Francine Bubblegum"
            },
            {
                "name": "Marge Ling"
            },
            {
                "name": "Princess Bouvier"
            }
        ]
    },
    "myFavouriteCelebrities": [
        {
            "name": "Robert Travolta"
        },
        {
            "name": "Finn Simpson"
        }
    ],
    "lastSeen": {
        "name": "Francine Bubblegum"
    }
}
references still equal:
 [ true, true, true ]

(or something similar depending on which tools serves as a viewer)

Changelog

Version 2.0.1

Removed needless RxJS dependency

Version 2.0.0

Support transformation both way:

  • from object to their JSON with ref representation
  • from JSON with ref representation to the corresponding object

Version 1.1.0

Currently only support convertion from an object to its JSON with references representation. (not the other way arround yet)

Version 1.0.0

Does nothing, it is just a "publish to npm" tutorial induced tryout.