New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

better-objects

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

better-objects

Clone and Equals methods for javascript objects and arrays

  • 1.0.2
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-77.78%
Maintainers
1
Weekly downloads
 
Created
Source

better-objects

Implementation for Object.clone, Object.equals

Adds the following methods to Object.prototype:

  • Object.prototype.clone()
  • clones an object into another object
  • deep clone, no matter how deep
  • doesn't clone functions - for performance reasons keeps a reference to the same function
  • if, for some reason, you need to clone the functions, you can uncomment lines 13/16
  • Object.prototype.equals()
  • checks if two objects are equal to each other
    • also works with arrays
    • returns true for similar objects or for references to the same object

You can choose not to add the methods to the Object prototype, it's really simple to change

usage:

install
$ npm install better-objects
and in the code (once)
require('better-objects');
//or just 
<script src="node_modules/better-objects/index.js"></script>
using clone:
var object = { ... };
var clone = object.clone();
// or with arrays
var arr = [...];
var clone = arr.clone();
using equals:
var objOne = {
  "string": "some value",
  "number": 344,
  "boolean": true,
  "object": {
      a: 10,
      b: 20
  },
  "array": [0, 1, 2, 3, 4, 5],
  "superArray": [
    "string",
    1,
    true,
    ["an", "array", "of", "strings"],
    {
        "str": "dfsdfh",
        "fn": somefunction,
        "array": [1, 3, 4, 5, 6]
    }
  ]
};

var objTwo = {
  "string": "some value",
  "number": 344,
  "boolean": true,
  "object": {
      a: 10,
      b: 20
  },
  "array": [0, 1, 2, 3, 4, 5],
  "superArray": [
    "string",
    1,
    true,
    ["an", "array", "of", "strings"],
    {
        "str": "dfsdfh",
        "fn": somefunction,
        "array": [1, 3, 4, 5, 6]
    }
  ]
};

var objThree = {
  something: 'different'
}

objOne.equals(objTwo); // returns true
objTwo.equals(objOne); // rerturns true
objOne.equals(objThree); // returns false
objThree.equals(objTwo); // returns false
note
  • .equals does not make a strict comparison for functions, it is implemented as such:
if( fn1.toString() === fn2.toString() ) { equals = true };

if you wish to have strict equality, just change it to:

if( fn1 === fn2 ) { equals = true };

running the tests

I'm using Testem + Mocha + Chai

$ npm install -g testem
$ npm install
# run the tests
$ testem

Keywords

FAQs

Package last updated on 09 Dec 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc