Git Product home page Git Product logo

javascript-object-paraphernalia's Introduction

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.

javascript-object-paraphernalia's People

Contributors

qrb avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.