Skip to content

Commit

Permalink
feat: add lengthEq and lengthNotEq (#552)
Browse files Browse the repository at this point in the history
Closes #444
  • Loading branch information
Undistraction authored and char0n committed May 13, 2018
1 parent 92ba7b5 commit f71ad1e
Show file tree
Hide file tree
Showing 14 changed files with 288 additions and 88 deletions.
12 changes: 12 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,18 @@ declare namespace RamdaAdjunct {
lengthGte<T>(valueLength: number, list: string | T[]): boolean;
lengthGte(valueLength: number): <T>(list: string | T[]) => boolean;

/**
* Returns `true` if the supplied list or string has a length equal to `valueLength`.
*/
lengthEq<T>(valueLength: number, list: string | T[]): boolean;
lengthEq(valueLength: number): <T>(list: string | T[]) => boolean;

/**
* Returns `true` if the supplied list or string has a length not equal to `valueLength`.
*/
lengthNotEq<T>(valueLength: number, list: string | T[]): boolean;
lengthNotEq(valueLength: number): <T>(list: string | T[]) => boolean;

/**
* Checks if input value is a `thenable`.
* `thenable` is an object or function that defines a `then` method.
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ export { default as lengthGt } from './lengthGt';
export { default as lengthLt } from './lengthLt';
export { default as lengthGte } from './lengthGte';
export { default as lengthLte } from './lengthLte';
export { default as lengthEq } from './lengthEq';
export { default as lengthNotEq } from './lengthNotEq';
// Object
export { default as paths } from './paths';
export { default as renameKeys } from './renameKeys';
Expand Down
24 changes: 24 additions & 0 deletions src/lengthEq.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { equals } from 'ramda';

import compareLength from './internal/compareLength';

/**
* Returns `true` if the supplied list or string has a length equal to `valueLength`.
*
* @func lengthEq
* @memberOf RA
* @since {@link https://char0n.github.io/ramda-adjunct/2.8.0|v2.8.0}
* @category List
* @sig Number -> [*] -> Boolean
* @param {number} valueLength The length of the list or string
* @param {Array|string} value The list or string
* @return {boolean}
* @see {@link RA.lengthNotEq|lengthNotEq}, {@link RA.lengthLt|lengthLt}, {@link RA.lengthGt|lengthGt}, {@link RA.lengthLte|lengthLte}, {@link RA.lengthGte|lengthGte},, {@link http://ramdajs.com/docs/#equals|equals}, {@link http://ramdajs.com/docs/#length|length}
* @example
*
* RA.lengthEq(3, [1,2,3]); //=> true
* RA.lengthEq(3, [1,2,3,4]); //=> false
*/
const lengthEq = compareLength(equals);

export default lengthEq;
6 changes: 3 additions & 3 deletions src/lengthGt.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gt, flip } from 'ramda';
import { flip, gt } from 'ramda';

import compareLength from './internal/compareLength';

Expand All @@ -11,9 +11,9 @@ import compareLength from './internal/compareLength';
* @category List
* @sig Number -> [*] -> Boolean
* @param {number} valueLength The length of the list or string
* @param {number} value The list or string
* @param {Array|string} value The list or string
* @return {boolean}
* @see {@link http://ramdajs.com/docs/#gt|gt}, {@link http://ramdajs.com/docs/#length|length}
* @see {@link RA.lengthEq|lengthEq}, {@link RA.lengthNotEq|lengthNotEq}, {@link RA.lengthLt|lengthLt}, {@link RA.lengthLte|lengthLte}, {@link RA.lengthGte|lengthGte}, {@link http://ramdajs.com/docs/#gt|gt}, {@link http://ramdajs.com/docs/#length|length}
* @example
*
* RA.lengthGt(3, [1,2,3,4]); //=> true
Expand Down
6 changes: 3 additions & 3 deletions src/lengthGte.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gte, flip } from 'ramda';
import { flip, gte } from 'ramda';

import compareLength from './internal/compareLength';

Expand All @@ -12,9 +12,9 @@ import compareLength from './internal/compareLength';
* @category List
* @sig Number -> [*] -> Boolean
* @param {number} valueLength The length of the list or string
* @param {number} value The list or string
* @param {Array|string} value The list or string
* @return {boolean}
* @see {@link http://ramdajs.com/docs/#gte|gte}, {@link http://ramdajs.com/docs/#length|length}
* @see {@link RA.lengthEq|lengthEq}, {@link RA.lengthNotEq|lengthNotEq}, {@link RA.lengthLt|lengthLt}, {@link RA.lengthGt|lengthGt}, {@link RA.lengthLte|lengthLte}, {@link http://ramdajs.com/docs/#gte|gte}, {@link http://ramdajs.com/docs/#length|length}
* @example
*
* RA.lengthGte(3, [1,2,3,4]); //=> true
Expand Down
6 changes: 3 additions & 3 deletions src/lengthLt.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { lt, flip } from 'ramda';
import { flip, lt } from 'ramda';

import compareLength from './internal/compareLength';

Expand All @@ -11,9 +11,9 @@ import compareLength from './internal/compareLength';
* @category List
* @sig Number -> [*] -> Boolean
* @param {number} valueLength The length of the list or string
* @param {number} value The list or string
* @param {Array|string} value The list or string
* @return {boolean}
* @see {@link http://ramdajs.com/docs/#lt|lt}, {@link http://ramdajs.com/docs/#length|length}
* @see {@link RA.lengthEq|lengthEq}, {@link RA.lengthNotEq|lengthNotEq}, {@link RA.lengthGt|lengthGt}, {@link RA.lengthLte|lengthLte}, {@link RA.lengthGte|lengthGte}, {@link http://ramdajs.com/docs/#lt|lt}, {@link http://ramdajs.com/docs/#length|length}
* @example
*
* RA.lengthLt(3, [1,2]); //=> true
Expand Down
6 changes: 3 additions & 3 deletions src/lengthLte.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { lte, flip } from 'ramda';
import { flip, lte } from 'ramda';

import compareLength from './internal/compareLength';

Expand All @@ -11,9 +11,9 @@ import compareLength from './internal/compareLength';
* @category List
* @sig Number -> [*] -> Boolean
* @param {number} valueLength The length of the list or string
* @param {number} value The list or string
* @param {Array|string} value The list or string
* @return {boolean}
* @see {@link http://ramdajs.com/docs/#lte|lte}, {@link http://ramdajs.com/docs/#length|length}
* @see {@link RA.lengthEq|lengthEq}, {@link RA.lengthNotEq|lengthNotEq}, {@link RA.lengthLt|lengthLt}, {@link RA.lengthGt|lengthGt}, {@link RA.lengthGte|lengthGte}, {@link http://ramdajs.com/docs/#lte|lte}, {@link http://ramdajs.com/docs/#length|length}
* @example
*
* RA.lengthLte(3, [1,2]); //=> true
Expand Down
24 changes: 24 additions & 0 deletions src/lengthNotEq.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { complement, equals } from 'ramda';

import compareLength from './internal/compareLength';

/**
* Returns `true` if the supplied list or string has a length not equal to `valueLength`.
*
* @func lengthNotEq
* @memberOf RA
* @since {@link https://char0n.github.io/ramda-adjunct/2.8.0|v2.8.0}
* @category List
* @sig Number -> [*] -> Boolean
* @param {number} valueLength The length of the list or string
* @param {Array|string} value The list or string
* @return {boolean}
* @see {@link RA.lengthEq|lengthEq}, {@link RA.lengthLt|lengthLt}, {@link RA.lengthGt|lengthGt}, {@link RA.lengthLte|lengthLte}, {@link RA.lengthGte|lengthGte}, {@link http://ramdajs.com/docs/#equals|equals}, {@link http://ramdajs.com/docs/#length|length}
* @example
*
* RA.lengthNotEq(3, [1,2,3,4]); //=> true
* RA.lengthNotEq(3, [1,2,3]); //=> false
*/
const lengthNotEq = compareLength(complement(equals));

export default lengthNotEq;
45 changes: 45 additions & 0 deletions test/lengthEq.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as RA from '../src/index';
import eq from './shared/eq';

describe('lengthEq', function() {
context(
`when the length of a list is equal to the supplied length`,
function() {
specify(`should return true`, function() {
eq(RA.lengthEq(3, [1, 2, 3]), true);
eq(RA.lengthEq(3, [1, 2, 3, 4]), false);
eq(RA.lengthEq(3, [1, 2]), false);
eq(RA.lengthEq(0, []), true);
});
}
);

context(
`when the length of a string is equal to the supplied length`,
function() {
specify(`should return true`, function() {
eq(RA.lengthEq(3, 'abc'), true);
eq(RA.lengthEq(3, 'abcd'), false);
eq(RA.lengthEq(3, 'ab'), false);
eq(RA.lengthEq(0, ''), true);
});
}
);

context(`when a value doesn't have a length property`, function() {
specify(`should return true`, function() {
eq(RA.lengthEq(1, NaN), false);
eq(RA.lengthEq(1, undefined), false);
eq(RA.lengthEq(1, null), false);
eq(RA.lengthEq(1, {}), false);
eq(RA.lengthEq(1, true), false);
eq(RA.lengthEq(1, false), false);
eq(RA.lengthEq(1, 5), false);
});
});

it(`should be curried`, function() {
eq(RA.lengthEq(2, [1, 2]), true);
eq(RA.lengthEq(2)([1, 2]), true);
});
});
48 changes: 30 additions & 18 deletions test/lengthGt.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,38 @@ import * as RA from '../src/index';
import eq from './shared/eq';

describe('lengthGt', function() {
it(`should return true if the length of a list is greater than supplied length`, function() {
eq(RA.lengthGt(3, [1, 2, 3, 4]), true);
eq(RA.lengthGt(3, [1, 2, 3]), false);
eq(RA.lengthGt(0, []), false);
});
context(
`when the length of a list is greater than the supplied length`,
function() {
specify(`should return true`, function() {
eq(RA.lengthGt(3, [1, 2, 3, 4]), true);
eq(RA.lengthGt(3, [1, 2, 3]), false);
eq(RA.lengthGt(0, []), false);
});
}
);

it(`should return true if the length of a string is greater than supplied length`, function() {
eq(RA.lengthGt(3, 'abcd'), true);
eq(RA.lengthGt(3, 'abc'), false);
eq(RA.lengthGt(0, ''), false);
});
context(
`when the length of a string is greater than the supplied length`,
function() {
specify(`should return true`, function() {
eq(RA.lengthGt(3, 'abcd'), true);
eq(RA.lengthGt(3, 'abc'), false);
eq(RA.lengthGt(0, ''), false);
});
}
);

it(`should return false for values without a length property`, function() {
eq(RA.lengthGt(1, NaN), false);
eq(RA.lengthGt(1, undefined), false);
eq(RA.lengthGt(1, null), false);
eq(RA.lengthGt(1, {}), false);
eq(RA.lengthGt(1, true), false);
eq(RA.lengthGt(1, false), false);
eq(RA.lengthGt(1, 5), false);
context(`when a value doesn't have a length property`, function() {
specify(`should return false`, function() {
eq(RA.lengthGt(1, NaN), false);
eq(RA.lengthGt(1, undefined), false);
eq(RA.lengthGt(1, null), false);
eq(RA.lengthGt(1, {}), false);
eq(RA.lengthGt(1, true), false);
eq(RA.lengthGt(1, false), false);
eq(RA.lengthGt(1, 5), false);
});
});

it(`should be curried`, function() {
Expand Down
52 changes: 32 additions & 20 deletions test/lengthGte.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,40 @@ import * as RA from '../src/index';
import eq from './shared/eq';

describe('lengthGte', function() {
it(`should return true if the length of a list is greater than or equal to the supplied length`, function() {
eq(RA.lengthGte(3, [1, 2, 3, 4]), true);
eq(RA.lengthGte(3, [1, 2, 3]), true);
eq(RA.lengthGte(3, [1, 2]), false);
eq(RA.lengthGte(0, []), true);
});
context(
`when the length of a list is greater than or equal to the supplied length`,
function() {
specify(`should return true`, function() {
eq(RA.lengthGte(3, [1, 2, 3, 4]), true);
eq(RA.lengthGte(3, [1, 2, 3]), true);
eq(RA.lengthGte(3, [1, 2]), false);
eq(RA.lengthGte(0, []), true);
});
}
);

it(`should return true if the length of a string is greater than or equal to the supplied length`, function() {
eq(RA.lengthGte(3, 'abcd'), true);
eq(RA.lengthGte(3, 'abc'), true);
eq(RA.lengthGte(3, 'ab'), false);
eq(RA.lengthGte(0, ''), true);
});
context(
`when the length of a string is greater than or equal to the supplied length`,
function() {
specify(`should return true`, function() {
eq(RA.lengthGte(3, 'abcd'), true);
eq(RA.lengthGte(3, 'abc'), true);
eq(RA.lengthGte(3, 'ab'), false);
eq(RA.lengthGte(0, ''), true);
});
}
);

it(`should return false for values without a length property`, function() {
eq(RA.lengthGte(1, NaN), false);
eq(RA.lengthGte(1, undefined), false);
eq(RA.lengthGte(1, null), false);
eq(RA.lengthGte(1, {}), false);
eq(RA.lengthGte(1, true), false);
eq(RA.lengthGte(1, false), false);
eq(RA.lengthGte(1, 5), false);
context(`when a value doesn't have a length property`, function() {
specify(`should return false`, function() {
eq(RA.lengthGte(1, NaN), false);
eq(RA.lengthGte(1, undefined), false);
eq(RA.lengthGte(1, null), false);
eq(RA.lengthGte(1, {}), false);
eq(RA.lengthGte(1, true), false);
eq(RA.lengthGte(1, false), false);
eq(RA.lengthGte(1, 5), false);
});
});

it(`should be curried`, function() {
Expand Down
48 changes: 30 additions & 18 deletions test/lengthLt.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,38 @@ import * as RA from '../src/index';
import eq from './shared/eq';

describe('lengthLt', function() {
it(`should return true if the length of a list is less than supplied length`, function() {
eq(RA.lengthLt(3, [1, 2]), true);
eq(RA.lengthLt(3, [1, 2, 3]), false);
eq(RA.lengthLt(0, []), false);
});
context(
`when the length of a list is less than the supplied length`,
function() {
specify(`should return true`, function() {
eq(RA.lengthLt(3, [1, 2]), true);
eq(RA.lengthLt(3, [1, 2, 3]), false);
eq(RA.lengthLt(0, []), false);
});
}
);

it(`should return true if the length of a string is less than supplied length`, function() {
eq(RA.lengthLt(3, 'ab'), true);
eq(RA.lengthLt(3, 'abc'), false);
eq(RA.lengthLt(0, ''), false);
});
context(
`when the length of a string is less than the supplied length`,
function() {
specify(`should return true`, function() {
eq(RA.lengthLt(3, 'ab'), true);
eq(RA.lengthLt(3, 'abc'), false);
eq(RA.lengthLt(0, ''), false);
});
}
);

it(`should return false for values without a length property`, function() {
eq(RA.lengthLt(1, NaN), false);
eq(RA.lengthLt(1, undefined), false);
eq(RA.lengthLt(1, null), false);
eq(RA.lengthLt(1, {}), false);
eq(RA.lengthLt(1, true), false);
eq(RA.lengthLt(1, false), false);
eq(RA.lengthLt(1, 5), false);
context(`when a value doesn't have a length property`, function() {
specify(`should return false`, function() {
eq(RA.lengthLt(1, NaN), false);
eq(RA.lengthLt(1, undefined), false);
eq(RA.lengthLt(1, null), false);
eq(RA.lengthLt(1, {}), false);
eq(RA.lengthLt(1, true), false);
eq(RA.lengthLt(1, false), false);
eq(RA.lengthLt(1, 5), false);
});
});

it(`should be curried`, function() {
Expand Down
Loading

0 comments on commit f71ad1e

Please sign in to comment.