deep-eql

中文文档
5.0.2发布于 2024/06/03MITES Module零依赖近 30 天下载 2.3亿

Improved deep equality testing for Node.js and the browser.

原始使用文档
文档目录5 节
  1. What is Deep-Eql?
  2. Installation
  3. Node.js
  4. Usage
  5. Rules

deep-eql

Improved deep equality testing for node and the browser.

build:? coverage:? dependencies:? devDependencies:?
Join the Slack chat Join the Gitter chat

What is Deep-Eql?

Deep Eql is a module which you can use to determine if two objects are "deeply" equal - that is, rather than having referential equality (a === b), this module checks an object's keys recursively, until it finds primitives to check for referential equality. For more on equality in JavaScript, read the comparison operators article on mdn.

As an example, take the following:

1 === 1 // These are primitives, they hold the same reference - they are strictly equal
1 == '1' // These are two different primitives, through type coercion they hold the same value - they are loosely equal
{ a: 1 } !== { a: 1 } // These are two different objects, they hold different references and so are not strictly equal - even though they hold the same values inside
{ a: 1 } != { a: 1 } // They have the same type, meaning loose equality performs the same check as strict equality - they are still not equal.

var deepEql = require("deep-eql");
deepEql({ a: 1 }, { a: 1 }) === true // deepEql can determine that they share the same keys and those keys share the same values, therefore they are deeply equal!

Installation

Node.js

deep-eql is available on npm.

$ npm install deep-eql

Usage

The primary export of deep-eql is function that can be given two objects to compare. It will always return a boolean which can be used to determine if two objects are deeply equal.

Rules

  • Strict equality for non-traversable nodes according to Object.is:
    • eql(NaN, NaN).should.be.true;
    • eql(-0, +0).should.be.false;
  • All own and inherited enumerable properties are considered:
    • eql(Object.create({ foo: { a: 1 } }), Object.create({ foo: { a: 1 } })).should.be.true;
    • eql(Object.create({ foo: { a: 1 } }), Object.create({ foo: { a: 2 } })).should.be.false;
  • When comparing Error objects, only name, message, and code properties are considered, regardless of enumerability:
    • eql(Error('foo'), Error('foo')).should.be.true;
    • eql(Error('foo'), Error('bar')).should.be.false;
    • eql(Error('foo'), TypeError('foo')).should.be.false;
    • eql(Object.assign(Error('foo'), { code: 42 }), Object.assign(Error('foo'), { code: 42 })).should.be.true;
    • eql(Object.assign(Error('foo'), { code: 42 }), Object.assign(Error('foo'), { code: 13 })).should.be.false;
    • eql(Object.assign(Error('foo'), { otherProp: 42 }), Object.assign(Error('foo'), { otherProp: 13 })).should.be.true;
  • Arguments are not Arrays:
    • eql([], arguments).should.be.false;
    • eql([], Array.prototype.slice.call(arguments)).should.be.true;

版本与发布记录

deep-eql 共发布 23 个版本,首次发布于 2013/09/18;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.1.4legacy2024/06/052 年前
5.0.2latest2024/06/032 年前
5.0.12023/10/253 年前
5.0.02023/10/183 年前
4.1.32022/12/053 年前
4.1.22022/11/073 年前

关于 deep-eql 的常见问题

deep-eql 是什么?

Improved deep equality testing for Node.js and the browser.

如何安装 deep-eql?

在项目目录运行 npm install deep-eql,或使用 pnpm add / yarn add / bun add。

deep-eql 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/deep-eql 是否存在。

deep-eql 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

deep-eql 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=6。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

deep-eql 可以商用吗?

deep-eql 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

deep-eql 的下载量有多少?

根据 npm 官方下载统计,deep-eql 在 2026/10/05 – 2026/11/03 这 30 天内被下载 226,535,116 次,日均约 7,551,171 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

deep-eql 还在维护吗?

最新版本 5.0.2 发布于 2024/06/03(2 年前);至今共发布 23 个版本,其中过去 12 个月发布了 0 个;首次发布于 2013/09/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。