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

cocotte-compare

v0.5.1

Published

deep equal

Downloads

6

Readme

cocotte-compare

はじめに

人間らしい比較を行う関数を提供します
javascriptの等価演算子では=====が存在しますが、どちらもテストに不向きな場面があります
人間らしい演算子を行うcocotte-compareを利用する事で、テストを簡単に行う事ができます
例えば、{a: 1} == {a: 1}としても {a: 1} === {a: 1} としても結果はfalseです
compare({a: 1}, {a: 1})trueを返すことができます

var compare = require('cocotte-compare');

var target1 = {
  a: 1,
  b: 2,
  c: [1, 2, 3]
};
var target2 = {
  b: 2,
  c: [1, 2, 3],
  a: 1
};

// 自己循環
target1.d = target1;
target2.d = target2;

// クロス循環
target1.e = target2;
target2.e = target1;

console.log(compare(target1, target2)); // true

関数定義

{Boolean} compare({Mixed} value, {Mixed} compareTo, {Number} level)

  • value: 比較する値
  • compareTo: 比較対象の値
  • level: ===の比較に切り替える階層(省略可能)

levelを設定すると、オブジェクトのプロパティや配列の要素は===で比較することができます
例えば、同じオブジェクトを順にもつ配列などを比較することができます

var x = {};
var y = {};
console.log(compare([x], [y]));    // true
console.log(compare([x], [y], 1)); // false

ルール

  • キャストは行いません。compare(1, '1')falseです
  • プリミティブは値を比較します
  • valueOfメソッドが自身を返さないオブジェクトはその値同士を比較し、プロパティの比較を行いません
  • オブジェクトの比較は、すべてのプロパティに対しルールを適用して一致する必要があります
  • プロパティの追加順序は無視します
  • 配列は、順序が一致し、それぞれの要素にルールを適用してすべてtrueである場合にtrueです
  • 自己循環参照にも対応しています
  • 0-0との比較はtrueです
  • newを使用して作成されたString,Number,Booleanとプリミティブの値との比較はfalseです
  • エラーオブジェクトは、プロトタイプとmessageプロパティの値が一致した場合にtrueです
  • 関数は参照先が同じである場合にのみtrueです
  • プロパティのenumerable,writable,configurableの設定が異なる場合もfalseです
  • プロパティのget,setはたとえコードが一緒でも関数が一致しない場合はfalseです
  • preventExtensions,seal,freezeされたオブジェクトとされていないオブジェクトとの比較はfalseです
  • level以上の階層の値は===で比較します