Skip to main content
Module

x/rambda/equals.spec.js

Faster and smaller alternative to Ramda
Go to Latest
File
import { equals } from './equals'
test('compare functions', () => { function foo(){} function bar(){} const baz = () => {}
const expectTrue = equals(foo, foo) const expectFalseFirst = equals(foo, bar) const expectFalseSecond = equals(foo, baz)
expect(expectTrue).toBeTrue() expect(expectFalseFirst).toBeFalse() expect(expectFalseSecond).toBeFalse()})
test('with array of objects', () => { const list1 = [ { a : 1 }, [ { b : 2 } ] ] const list2 = [ { a : 1 }, [ { b : 2 } ] ] const list3 = [ { a : 1 }, [ { b : 3 } ] ]
expect(equals(list1, list2)).toBeTrue() expect(equals(list1, list3)).toBeFalse()})
test('with regex', () => { expect(equals(/s/, /s/)).toEqual(true) expect(equals(/s/, /d/)).toEqual(false) expect(equals(/a/gi, /a/gi)).toEqual(true) expect(equals(/a/gim, /a/gim)).toEqual(true) expect(equals(/a/gi, /a/i)).toEqual(false)})
test('not a number', () => { expect(equals([ NaN ], [ NaN ])).toBeTrue()})
test('new number', () => { expect(equals(new Number(0), new Number(0))).toEqual(true) expect(equals(new Number(0), new Number(1))).toEqual(false) expect(equals(new Number(1), new Number(0))).toEqual(false)})
test('new string', () => { expect(equals(new String(''), new String(''))).toEqual(true) expect(equals(new String(''), new String('x'))).toEqual(false) expect(equals(new String('x'), new String(''))).toEqual(false) expect(equals(new String('foo'), new String('foo'))).toEqual(true) expect(equals(new String('foo'), new String('bar'))).toEqual(false) expect(equals(new String('bar'), new String('foo'))).toEqual(false)})
test('new Boolean', () => { expect(equals(new Boolean(true), new Boolean(true))).toEqual(true) expect(equals(new Boolean(false), new Boolean(false))).toEqual(true) expect(equals(new Boolean(true), new Boolean(false))).toEqual(false) expect(equals(new Boolean(false), new Boolean(true))).toEqual(false)})
test('new Error', () => { expect(equals(new Error('XXX'), {})).toEqual(false) expect(equals(new Error('XXX'), new TypeError('XXX'))).toEqual(false) expect(equals(new Error('XXX'), new Error('YYY'))).toEqual(false) expect(equals(new Error('XXX'), new Error('XXX'))).toEqual(true) expect(equals(new Error('XXX'), new TypeError('YYY'))).toEqual(false)})
test('with dates', () => { expect(equals(new Date(0), new Date(0))).toEqual(true) expect(equals(new Date(1), new Date(1))).toEqual(true) expect(equals(new Date(0), new Date(1))).toEqual(false) expect(equals(new Date(1), new Date(0))).toEqual(false) expect(equals(new Date(0), {})).toEqual(false) expect(equals({}, new Date(0))).toEqual(false)})
test('ramda spec', () => { expect(equals({}, {})).toEqual(true)
expect(equals({ a : 1, b : 2, }, { a : 1, b : 2, })).toEqual(true)
expect(equals({ a : 2, b : 3, }, { b : 3, a : 2, })).toEqual(true)
expect(equals({ a : 2, b : 3, }, { a : 3, b : 3, })).toEqual(false)
expect(equals({ a : 2, b : 3, c : 1, }, { a : 2, b : 3, })).toEqual(false)})
test('works with boolean tuple', () => { expect(equals([ true, false ], [ true, false ])).toBeTrue() expect(equals([ true, false ], [ true, true ])).toBeFalse()})
test('works with equal objects within array', () => { const objFirst = { a : { b : 1, c : 2, d : [ 1 ], }, } const objSecond = { a : { b : 1, c : 2, d : [ 1 ], }, }
const x = [ 1, 2, objFirst, null, '', [] ] const y = [ 1, 2, objSecond, null, '', [] ] expect(equals(x, y)).toBeTrue()})
test('works with different objects within array', () => { const objFirst = { a : { b : 1 } } const objSecond = { a : { b : 2 } }
const x = [ 1, 2, objFirst, null, '', [] ] const y = [ 1, 2, objSecond, null, '', [] ] expect(equals(x, y)).toBeFalse()})
test('works with undefined as second argument', () => { expect(equals(1, undefined)).toBeFalse()
expect(equals(undefined, undefined)).toBeTrue()})
test('various examples', () => { expect(equals([ 1, 2, 3 ])([ 1, 2, 3 ])).toBeTrue()
expect(equals([ 1, 2, 3 ], [ 1, 2 ])).toBeFalse()
expect(equals(1, 1)).toBeTrue()
expect(equals(1, '1')).toBeFalse()
expect(equals({}, {})).toBeTrue()
expect(equals({ a : 1, b : 2, }, { b : 2, a : 1, })).toBeTrue()
expect(equals({ a : 1, b : 2, }, { a : 1, b : 1, })).toBeFalse()
expect(equals({ a : 1, b : false, }, { a : 1, b : 1, })).toBeFalse()
expect(equals({ a : 1, b : 2, }, { b : 2, a : 1, c : 3, })).toBeFalse()
expect(equals({ x : { a : 1, b : 2, }, }, { x : { b : 2, a : 1, c : 3, }, })).toBeFalse()
expect(equals({ a : 1, b : 2, }, { b : 3, a : 1, })).toBeFalse()
expect(equals({ a : { b : { c : 1 } } }, { a : { b : { c : 1 } } })).toBeTrue()
expect(equals({ a : { b : { c : 1 } } }, { a : { b : { c : 2 } } })).toBeFalse()
expect(equals({ a : {} }, { a : {} })).toBeTrue()
expect(equals('', '')).toBeTrue()
expect(equals('foo', 'foo')).toBeTrue()
expect(equals('foo', 'bar')).toBeFalse()
expect(equals(0, false)).toBeFalse()
expect(equals(/\s/g, null)).toBeFalse()
expect(equals(null, null)).toBeTrue()
expect(equals(false)(null)).toBeFalse()})
test('with custom functions', () => { function foo(){ return 1 } foo.prototype.toString = () => '' const result = equals(foo, foo)
expect(result).toBeTrue()})
test('with classes', () => { class Foo{} const foo = new Foo() const result = equals(foo, foo)
expect(result).toBeTrue()})
test('with negative zero', () => { expect(equals(-0, -0)).toBeTrue() expect(equals(-0, 0)).toBeFalse() expect(equals(0, 0)).toBeTrue() expect(equals(-0, 1)).toBeFalse()})