mirror of
https://github.com/fluencelabs/js-libp2p-utils
synced 2025-04-25 09:12:32 +00:00
feat: arrayEquals for non primitive types with equals function
This commit is contained in:
parent
0e7fc344ec
commit
80668fff1a
14
src/array-equals.js
Normal file
14
src/array-equals.js
Normal file
@ -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
test/array-equals.spec.js
Normal file
76
test/array-equals.spec.js
Normal file
@ -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/8001'),
|
||||
multiaddr('/dns4/test.libp2p.io')
|
||||
]
|
||||
|
||||
expect(arrayEquals(a, b)).to.eql(false)
|
||||
})
|
||||
})
|
Loading…
x
Reference in New Issue
Block a user