Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

feat: array equals for non primitive types with equal function #8

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@
},
"homepage": "https://github.com/libp2p/js-libp2p-utils#readme",
"devDependencies": {
"aegir": "^22.0.0",
"aegir": "^25.0.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"it-pair": "^1.0.0",
"it-pipe": "^1.1.0",
"streaming-iterables": "^4.1.2"
"streaming-iterables": "^5.0.2"
},
"dependencies": {
"abortable-iterator": "^3.0.0",
"debug": "^4.1.1",
"err-code": "^2.0.0",
"err-code": "^2.0.3",
"ip-address": "^6.1.0",
"multiaddr": "^7.3.0"
"multiaddr": "^7.5.0"
},
"contributors": [
"Vasco Santos <vasco.santos@moxy.studio>"
Expand Down
14 changes: 14 additions & 0 deletions src/array-equals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

/**
* Verify if two arrays of non primitive types with the "equals" function are equal.
* Compatible with multiaddr, peer-id and Buffer.
* @param {Array<*>} a
* @param {Array<*>} b
* @returns {boolean}
*/
function arrayEquals (a, b) {
return a.length === b.length && b.sort() && a.sort().every((item, index) => b[index].equals(item))
}

module.exports = arrayEquals
76 changes: 76 additions & 0 deletions test/array-equals.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)

const multiaddr = require('multiaddr')

const arrayEquals = require('../src/array-equals')

describe('non primitive array equals', () => {
it('returns true if two arrays of multiaddrs are equal', () => {
const a = [
multiaddr('/ip4/127.0.0.1/tcp/8000'),
multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
multiaddr('/dns4/test.libp2p.io')
]

const b = [
multiaddr('/ip4/127.0.0.1/tcp/8000'),
multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
multiaddr('/dns4/test.libp2p.io')
]

expect(arrayEquals(a, b)).to.eql(true)
})

it('returns true if two arrays of multiaddrs have the same content but different orders', () => {
const a = [
multiaddr('/ip4/127.0.0.1/tcp/8000'),
multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
multiaddr('/dns4/test.libp2p.io')
]

const b = [
multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
multiaddr('/ip4/127.0.0.1/tcp/8000'),
multiaddr('/dns4/test.libp2p.io')
]

expect(arrayEquals(a, b)).to.eql(true)
})

it('returns false if two arrays of multiaddrs are different', () => {
const a = [
multiaddr('/ip4/127.0.0.1/tcp/8000'),
multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
multiaddr('/dns4/test.libp2p.io')
]

const b = [
multiaddr('/ip4/127.0.0.1/tcp/8001'),
multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
multiaddr('/dns4/test.libp2p.io')
]

expect(arrayEquals(a, b)).to.eql(false)
})

it('returns false if two arrays of multiaddrs are partially equal, but different lengths', () => {
const a = [
multiaddr('/ip4/127.0.0.1/tcp/8000'),
multiaddr('/ip4/127.0.0.1/tcp/3000/ws'),
multiaddr('/dns4/test.libp2p.io')
]

const b = [
multiaddr('/ip4/127.0.0.1/tcp/8000'),
multiaddr('/dns4/test.libp2p.io')
]

expect(arrayEquals(a, b)).to.eql(false)
})
})