Skip to content

Commit

Permalink
feat: add allIdenticalTo
Browse files Browse the repository at this point in the history
Ref #525
  • Loading branch information
char0n committed Oct 15, 2018
1 parent e3dbb67 commit 189f3f5
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/allIdenticalTo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { curry, identical, all } from 'ramda';

/**
* Returns true if all items in the list are equivalent to user provided value using `R.identical` for equality comparisons.
*
* @func allIdenticalTo
* @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.allIdentical|allIdentical}
* @example
*
* RA.allIdenticalTo(1, [ 1, 2, 3, 4 ]); //=> false
* RA.allIdenticalTo(1, [ 1, 1, 1, 1 ]); //=> true
* RA.allIdenticalTo([]); //=> true
* RA.allIdenticalTo({}, [ {}, {} ]); //=> false
*
*/
const allIdenticalTo = curry((val, list) => all(identical(val), list));

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

/**
* Returns true if all items in the list are equivalent to user provided value using `R.identical` for equality comparisons.
*/
allIdenticalTo<T>(val: T, list: T[]): boolean;
allIdenticalTo<T>(val: 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 @@ -119,6 +119,7 @@ export { default as lengthLte } from './lengthLte';
export { default as lengthEq } from './lengthEq';
export { default as lengthNotEq } from './lengthNotEq';
export { default as allEqual } from './allEqual';
export { default as allIdenticalTo } from './allIdenticalTo';
// Object
export { default as paths } from './paths';
export { default as renameKeys } from './renameKeys';
Expand Down
49 changes: 49 additions & 0 deletions test/allIdenticalTo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { assert } from 'chai';

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

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

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

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

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

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

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

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

0 comments on commit 189f3f5

Please sign in to comment.