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

@swjc1g11/objecttopaths

v0.0.1

Published

Flattens an object to an object where each value is represented by its path in the object. Allows the object to be reassembled with depth from the flattened version of itself.

Downloads

2

Readme

Object to Paths

This repository is a work in progress to create a small JavaScript library whose primary purpose is to deal with the flattening and reassembly of an object from the flattened data for the purposes of a personal project I am working on.

It can be installed as follows:

npm install @swjc1g11/objecttopaths

Once installed, you only have to import the library to begin using it:

const objecttopaths = require('@swjc1g11/objecttopaths')

It takes an object like this:

const original = {
    title: "This is a title",
    description: "This is a description",
    nestedObject: {
        title: "This is a nested title",
        description: "This is a nested description"
    }
}

Turns it into this:

const paths = {
    "title": "This is a title",
    "description": "This is a description",
    "nestedObject%~%title": "This is a nested title",
    "nestedObject%~%description": "This is an ested description" 
}

And then turns that back into this when or if desired:

const reassembled = {
    title: "This is a title",
    description: "This is a description",
    nestedObject: {
        title: "This is a nested title",
        description: "This is a nested description"
    }
}

Methods

objecttopaths.objectToPaths

This function takes an object and returns a flattened version of this object.

In this flattened version of the object, each value is represented by a key formed of its path in the original object, as in the above examples.

objecttopaths.mergePathsWithObject

This method takes the original object and a set of paths obtained from objectToPaths. It then returns a version of the original object whereby any changes made to the paths are applied to the corresponding value in the original object.

This allows for use cases whereby it is easier to build an application that edits the paths than the object, among other potential use cases.

objecttopaths.buildObjectFromPaths

This method takes an object of paths and takes its best shot at guessing what the original object looked at, returning what it things the original object might have been.

It suffers from three primary weaknesses at this time:

  1. You must choose whether solely numeric keys in an object at any nested level represent indexes in an array or keys in an object. More on this below.
  2. If your keys use the chosen separator in their content, the object will be built incorrectly. More on this below, too.
  3. Arrays containing mixed data types may not work as expected or cause an error.

To allow users to make a choice on whether purely numeric indexes should be built as part of an array or as an object, you can use a settings object as below:

let flattened = objecttopaths.objectToPaths(objectWithNumericKeys)
let reassembled = objecttopaths.buildObjectFromPaths(flattened, {
    'numericKeysRepresentArrayItems': false
})

objecttopaths.getPathSeparator

To view the current separator used to separate the keys that build the path of a value in an original object, it is possible to use the function. The default value is '%~%'.

objecttopaths.setPathSeparator

In the unlikely event that your existing object keys needs the default value of '%~%', you are able to choose an alternate value with this function. It will stay in effect until the objecttopaths object is destroyed and created.

This will allow you to avoid any conflicts with your own keys. Alternatively, it can be used to set a common delimiter such as '.' that could allow you to re-interpret existing data sources. This could have a multitude of applications that would allow you to rebuild objects from existing data sources that use a path delimiter, or otherwise.