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

object2

v1.1.3

Published

Get,Put,Delete from an object easily.

Downloads

15

Readme

object2 - Inspired from Selectn

Get,Put,Delete from an object easily.

npm i object2 --save
npm stats

npm NPM downloads

Overview

Get-allows you to refactor this:
thor && thor.weapon && thor.weapon.stormbreaker 
into:
object2.get('weapon.stormbreaker', thor)
Put-allows you to refactor this:
if(!thor.info ) {
thor.info={}
}

if(!thor.info.name){
thor.info.name={}
}
thor.info.name.actorname = "Chris Hemsworth"
into:
object2.put("thor.info.name.actorname",avengers,"Chris Hemsworth")
Delete-allows you to refactor this:
if(thor && thor.weapon && thor.weapon.stormbreaker){
    delete thor.weapon.stormbreaker
}
into:
object2.delete('weapon.stormbreaker', thor)
  

Usage example(s)

get

var object2 = require('object2')
var avengers={
    thor:{
        info:{
            name:{
                castname:"Thor",
                actorname:"Chris Hemsworth",
                
            }
        },
        weapon:{
            stormbreaker:{
                activate:function(status){
                    console.log("Yeah!! its "+(status?"activated":"deactivated"));
                }

            }
        }
    }
}


object2.get("thor.info.name.actorname",avengers);
=>Chris Hemsworth

object2.get(["thor","info","name","actorname"],avengers);
=>Chris Hemsworth

object2.get("thor.weapon.stormbreaker.activate",avengers)(true);
=>"Yeah!! its activated"

object2.get("ironman.info.name.actoname",avengers);
=>undefined

put

var object2 = require('object2')
var avengers = {
    ironman:{
        info:{
            name:"Tony Stark"
        }
    }
}

** Note: This will change the original object **

object.put("ironman.info.name.actorname",avengers,"Robert Downey")
=> {
ironman:{
        info:{
            name:{
                actorname:"Robert Downey"
            }
        }
    }
}

object.put("ironman.info.name.actorname",avengers,function(val){ return val+" Jr"})
=> {
ironman:{
        info:{
            name:{
                actorname:"Robert Downey Jr"
            }
        }
    }
}

object.put(["ironman","info","name","castname"],avengers,"Tony Stark")
=> {
ironman:{
        info:{
            name:{
                actorname:"Robert Downey Jr",
                castname:"Tony Stark"
            }
        }
    }
}

/*
console.log(avengers);
=> {
ironman:{
        info:{
            name:{
                actorname:"Robert Downey Jr",
                castname:"Tony Stark"
            }
        }
    }
}
*/

delete

var object2 = require('object2')
var avengers={
    thor:{
        info:{
            name:{
                castname:"Thor",
                actorname:"Chris Hemsworth",
                
            }
        },
        weapon:{
            stormbreaker:{
                activate:function(status){
                    console.log("Yeah!! its "+(status?"activated":"deactivated"));
                }

            }
        }
    }
}

/*
If the path exists in the given object,then it will delete the key,which is the path, from the object.
If the path doesn't exists,then it won't do any thing.
*/

** Note: This will change the original object **

object2.delete("thor.info.name.actorname",avengers);

object2.delete(["thor","weapon"],avengers);

object2.delete("ironman.info.name.actorname",avengers); //this won't do anything because of the path doesn't exist in the given object.

/*
console.log(avengers);
=> {
thor:{
        info:{
            name:{
                castname:"Thor"
            }
        }
    }
}
*/

Licenses

LICENSE