Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

comparejs

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

comparejs

Implementing the comparison operators eq, ne, gt, ge, lt and le in JavaScript for use with Node.js and the browser.

  • 0.0.11
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
26K
decreased by-2.8%
Maintainers
1
Weekly downloads
 
Created
Source

comparejs

comparejs implements JavaScript's comparison operators for Node.js and the browser the way you would expect them to be.

If you have any questions or feedback, feel free to contact me using @goloroden on Twitter.

Installing

Node.js

Basically, using comparejs in Node.js is easy. All you need to do is install it using npm by running the following command:

$ npm install comparejs

Then, you can reference it from your application by adding the following line:

var cmp = require('comparejs');

In the browser

Using comparejs in the browser is easy, too. All you need to do here is add a script reference within your web site. If you want to use the latest version hosted on GitHub, use:

<script type="text/javascript" src="https://raw.github.com/goloroden/comparejs/master/bin/compare.min.js"></script>

If instead you want to use a specific version hosted on GitHub, use:

<script type="text/javascript" src="https://raw.github.com/goloroden/comparejs/master/bin/compare-[x.y.z].min.js"></script>

Of course you can also download any version manually and add a local reference.

If you are running Visual Studio, instead of downloading manually you can also install comparejs by using the NuGet package. For that run the following command inside the NuGet console:

PM> Install-Package compare.js

Note: The NuGet package was created by Alexander Zeitler. Thanks for that :-)!

Features

  • Supports comparison of number, string, boolean, function, object, array and undefined.
  • Supports comparison of native data types and constructor-created data types, such as number and new Number().
  • Supports comparison of objects and arrays using deep-equal.
  • Supports comparison of objects and arrays with <, <=, > and >= by handling them as subsets.
  • Supports comparison of objects by structure.
  • Supports comparison with undefined correctly, as <= and >= are problematic in plain JavaScript.
  • Supports comparison in a perfectly type-safe way out-of-the-box.
  • Supports comparison by equality and identity, depending on what makes sense.
  • Developed using TDD and backed up by more than 930 unit tests.

Basic usage

Now you are able to use the various comparison operators. All you need to do is access the cmp object and use its functions:

OperatorAliasDescription
cmp.eq(first, second)cmp.equal(first, second)equal
cmp.ne(first, second)cmp.notEqual(first, second)not equal
cmp.gt(first, second)cmp.greaterThan(first, second)greater than
cmp.ge(first, second)cmp.greaterThanOrEqual(first, second)greater than or equal
cmp.lt(first, second)cmp.lessThan(first, second)less than
cmp.le(first, second)cmp.lessThanOrEqual(first, second)less than or equal
cmp.id(first, second)cmp.identical(first, second)identical

Please note that each comparison operator works on each combination of types and does what you would expect it to do.

Structure comparison operators

For objects, there are special operators that compare by structure. Among other things, they can be used to verify objects against interfaces:

OperatorAliasDescription
cmp.eqs(first, second)cmp.equalByStructure(first, second)equal by structure
cmp.nes(first, second)cmp.notEqualByStructure(first, second)not equal by structure
cmp.gts(first, second)cmp.greaterThanByStructure(first, second)greater than by structure
cmp.ges(first, second)cmp.greaterThanOrEqualByStructure(first, second)greater than or equal by structure
cmp.lts(first, second)cmp.lessThanByStructure(first, second)less than by structure
cmp.les(first, second)cmp.lessThanOrEqualByStructure(first, second)less than or equal by structure

Please note that these operators only work for objects. For any other type, they return false.

Running the tests

comparejs has been developed using TDD. To run the tests, go to the folder where you have installed comparejs to and run npm test. You need to have mocha installed.

$ npm test

That's it :-)!

License

The MIT License (MIT) Copyright (c) 2012 Golo Roden.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

FAQs

Package last updated on 29 Mar 2013

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