Skip to content

Commit

Permalink
feat: add allEqualTo
Browse files Browse the repository at this point in the history
Closes #525
  • Loading branch information
char0n committed Oct 22, 2018
1 parent fb61085 commit 3ce96d3
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/allEqualTo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { all, equals, curry } from 'ramda';

/**
* Returns true if all items in the list are equivalent to user provided value using `R.equals` for equality comparisons.
*
* @func allEqualTo
* @memberOf RA
* @since {@link https://char0n.github.io/ramda-adjunct/2.11.0|v2.11.0}
* @category List
* @sig a -> [b] -> Boolean
* @param {*} val User provided value to check the `list` against
* @param {Array} list The list of values
* @return {boolean}
* @see {@link RA.allEqual|allEqual}, {@link https://ramdajs.com/docs/#equals|equals}
* @example
*
* RA.allEqualTo(1, [ 1, 2, 3, 4 ]); //=> false
* RA.allEqualTo(1, [ 1, 1, 1, 1 ]); //=> true
* RA.allEqualTo(1, []); //=> true
*
*/
const allEqualTo = curry((val, list) => all(equals(val), list));

export default allEqualTo;
6 changes: 6 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,12 @@ declare namespace RamdaAdjunct {
allIdenticalTo<T>(val: T, list: T[]): boolean;
allIdenticalTo<T>(val: T): (list: T[]) => boolean;

/*
* Returns true if all items in the list are equivalent to user provided value using `R.equals` for equality comparisons.
*/
allEqualTo<T>(val: T, list: T[]): boolean;
allEqualTo<T>(val: T): <T>(list: T[]) => boolean;

/**
* Checks if input value is a `thenable`.
* `thenable` is an object or function that defines a `then` method.
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export { default as allEqual } from './allEqual';
export { default as repeatStr } from './repeatStr';
export { default as allIdentical } from './allIdentical';
export { default as allIdenticalTo } from './allIdenticalTo';
export { default as allEqualTo } from './allEqualTo';
// Object
export { default as paths } from './paths';
export { default as renameKeys } from './renameKeys';
Expand Down
58 changes: 58 additions & 0 deletions test/allEqualTo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { assert } from 'chai';
import * as R from 'ramda';

import * as RA from '../src';

// https://github.com/ramda/ramda/pull/1992
const hasFunctionReferenceEqualityBug = (() => {
const f = () => {};
return R.uniq([f, f, f]).length !== 1;
})();

describe('allEqualTo', function() {
context('given all items are equal', function() {
specify('returns true', function() {
assert.isTrue(RA.allEqualTo(4, [4, 4, 4, 4]));
});
});

context('given all items are deeply equal', function() {
specify('should return true', function() {
assert.isTrue(
RA.allEqualTo({ key: 'foo' }, [{ key: 'foo' }, { key: 'foo' }])
);
});
});

context('given items are not equal', function() {
specify('should return false', function() {
assert.isFalse(RA.allEqualTo(1, [1, 1, 2, 1, 1]));
});
});

context('given items have different type', function() {
specify('should return false', function() {
assert.isFalse(RA.allEqualTo(1, [1, 1, '1', 1]));
});
});

if (!hasFunctionReferenceEqualityBug) {
context('given items are reference to function', function() {
specify('should return true', function() {
const f = () => {};
assert.isTrue(RA.allEqualTo(f, [f, f, f]));
});
});
}

context('given empty list provided', function() {
specify('should return true', function() {
assert.isTrue(RA.allEqualTo(1, []));
});
});

it('should curry', function() {
assert.isTrue(RA.allEqualTo(1, [1, 1]));
assert.isTrue(RA.allEqualTo(1)([1, 1]));
});
});

0 comments on commit 3ce96d3

Please sign in to comment.