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

javascript-object-paraphernalia

v1.0.11

Published

Javascript object utilities such as deep clone, merge and apply. Also includes 'is' a type checking method to type check Objects, Functions and Classes

Downloads

61

Readme

javascript-object-paraphernalia

Build Status

Javascript object utilities such as deep clone, merge and apply. Also includes 'is' a type checking method to type check Objects, Functions and Classes

Getting Started

1. Installation

npm install javascript-object-paraphernalia

2. Examples

Require javascript-object-paraphernalia

var obj = require('javascript-object-paraphernalia')

Define some objects for use with the following examples

var exampleFirst = {
    fruit: "Apple",
    animals: {
      firstAnimal: "Beaver"
    }
  },
  exampleSecond = {
    fruit: "Banana",
    animals: {
      secondAnimal: "Slow Loris",
      thirdAnimal: "Elephant"
    },
    gems: {
      firstGem: "Diamond"
    }
  };

Object clone example

Use obj.clone to clone the first example object

var exampleClone = obj.clone(exampleFirst)

Results:-

exampleClone = {
  fruit: "Apple",
  animals: {
    firstAnimal: "Beaver"
  }
}

Update the clone's inner properties

exampleClone.fruit = "Orange"
exampleClone.animals.cloneOnlyAnimal = "Rabbit"

Compare the clone with the first example object

exampleFirst = {
  fruit: "Apple",
  animals: {
    firstAnimal: "Beaver"
  }
}
exampleClone = {
  fruit: "Orange",
  animals: {
    cloneOnlyAnimal: "Rabbit",
    firstAnimal: "Beaver"
  }
}

Object merge example

Use obj.merge to merge the second example object with the first example object

obj.merge(exampleFirst, exampleSecond)

Results:-

{
  fruit: "Banana",
  animals: {
    firstAnimal: "Beaver",
    secondAnimal: "Slow Loris",
    thirdAnimal: "Elephant"
  },
  gems: {
    firstGem: "Diamond"
  }
}

Change the second example's thirdAnimal

exampleSecond.animals.thirdAnimal = "Slender Loris"

See that the first example's animal property has not mutated

// exampleFirst contents:- 
{
  fruit: "Banana",
  animals: {
    firstAnimal: "Beaver",
    secondAnimal: "Slow Loris",
    thirdAnimal: "Elephant"
  },
  gems: {
    firstGem: "Diamond"
  }
}

Now change the first Example's gems property

exampleFirst.gems.firstGem = "Ruby"

Because the gems property of the second Example was referenced by the first Example, the gems property is the same object on both first and second examples.

// exampleFirst contents:- 
{
  fruit: "Banana",
  animals: {
    firstAnimal: "Beaver",
    secondAnimal: "Slow Loris",
    thirdAnimal: "Slender Loris"
  },
  gems: {
    firstGem: "Ruby"
  }

Object apply example

Use obj.apply to apply the properties of the second example object on the first example object

exampleSecond.animals.firstAnimal = "Slow Loris"
obj.apply(exampleFirst, exampleSecond)

The contents of the first object are:-

{
  fruit: "Banana",
  animals: {
    firstAnimal: "Slow Loris"
  }
}

See that only the first example's properties have been updated and none of the additional properties present on the second object have been applied

Object is example

Declare some variables to work with in the following examples

function TestClass() {}
var object = {},
    testClass = new TestClass(),
    func = function() { return false },
    str = "I'm a string"

Use obj.is to test an assertion about an object's type

  obj.is(object, 'Object')        // === true
  obj.is(object, 'object')        // === true
  obj.is(func, 'Function')        // === true
  obj.is(func, 'function')        // === true
  obj.is(str, 'String')           // === true
  obj.is(str, 'string')           // === true

  // testClass returns true to either its Class or Object
  obj.is(testClass, 'TestClass')  // === true
  obj.is(testClass, 'Object')     // === true
    
  // testing for a class name is not case insensitive  
  obj.is(testClass, 'testclass')  // === false  

Copyright and license

Copyright (c) 2015, Any Code [email protected]

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.