Skip to content

Commit

Permalink
feat: add lengthLt, lengthGt, lengthLte, lengthGte
Browse files Browse the repository at this point in the history
  • Loading branch information
Undistraction authored and char0n committed May 13, 2018
1 parent cc12528 commit 47d4560
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,6 @@ declare namespace RamdaAdjunct {
*/
sliceFrom<T>(fromIndex: number, list: string | T[]): string | T[];
sliceFrom(fromIndex: number): <T>(list: string | T[]) => string | T[];
sliceFrom<T>(fromIndex: number, list: string | T[]): string | T[];

/**
* Returns the elements of the given list or string (or object with a slice method)
Expand All @@ -809,6 +808,32 @@ declare namespace RamdaAdjunct {
omitIndexes<T>(indexes: number[], list: T[]): T[];
omitIndexes(indexes: number[]): <T>(list: T[]) => T[];

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

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

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

/**
* Returns `true` if the supplied list or string has a length greater than or equal to
* `valueLength`.
*/
lengthGte<T>(valueLength: number, list: string | T[]): boolean;
lengthGte(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
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ export { default as compact } from './compact';
export { default as appendFlipped } from './appendFlipped';
export { default as contained } from './contained';
export { default as move } from './move';
export { default as lengthGt } from './lengthGt';
export { default as lengthLt } from './lengthLt';
export { default as lengthGte } from './lengthGte';
export { default as lengthLte } from './lengthLte';
// Object
export { default as paths } from './paths';
export { default as renameKeys } from './renameKeys';
Expand Down
7 changes: 7 additions & 0 deletions src/internal/compareLength.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { curry, compose, length } from 'ramda';

const compareLength = curry((comparator, value, list) =>
compose(comparator(value), length)(list)
);

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

import compareLength from './internal/compareLength';

/**
* Returns `true` if the supplied list or string has a length greater than `valueLength`.
*
* @func lengthGt
* @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 {number} value The list or string
* @return {boolean}
* @see {@link http://ramdajs.com/docs/#gt|gt}, {@link http://ramdajs.com/docs/#length|length}
* @example
*
* RA.lengthGt(3, [1,2,3,4]); //=> true
* RA.lengthGt(3, [1,2,3]); //=> false
*/
const lengthGt = compareLength(flip(gt));

export default lengthGt;
26 changes: 26 additions & 0 deletions src/lengthGte.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { gte, flip } from 'ramda';

import compareLength from './internal/compareLength';

/**
* Returns `true` if the supplied list or string has a length greater than or equal to
* `valueLength`.
*
* @func lengthGte
* @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 {number} value The list or string
* @return {boolean}
* @see {@link http://ramdajs.com/docs/#gte|gte}, {@link http://ramdajs.com/docs/#length|length}
* @example
*
* RA.lengthGte(3, [1,2,3,4]); //=> true
* RA.lengthGte(3, [1,2,3]); //=> true
* RA.lengthGte(3, [1,2,3]); //=> false
*/
const lengthGte = compareLength(flip(gte));

export default lengthGte;
24 changes: 24 additions & 0 deletions src/lengthLt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { lt, flip } from 'ramda';

import compareLength from './internal/compareLength';

/**
* Returns `true` if the supplied list or string has a length less than `valueLength`.
*
* @func lengthLt
* @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 {number} value The list or string
* @return {boolean}
* @see {@link http://ramdajs.com/docs/#lt|lt}, {@link http://ramdajs.com/docs/#length|length}
* @example
*
* RA.lengthLt(3, [1,2]); //=> true
* RA.lengthLt(3, [1,2,3]); //=> false
*/
const lengthLt = compareLength(flip(lt));

export default lengthLt;
25 changes: 25 additions & 0 deletions src/lengthLte.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { lte, flip } from 'ramda';

import compareLength from './internal/compareLength';

/**
* Returns `true` if the supplied list or string has a length less than or equal to `valueLength`.
*
* @func lengthLte
* @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 {number} value The list or string
* @return {boolean}
* @see {@link http://ramdajs.com/docs/#lte|lte}, {@link http://ramdajs.com/docs/#length|length}
* @example
*
* RA.lengthLte(3, [1,2]); //=> true
* RA.lengthLte(3, [1,2,3]); //=> true
* RA.lengthLte(3, [1,2,3,4]); //=> false
*/
const lengthLte = compareLength(flip(lte));

export default lengthLte;
31 changes: 31 additions & 0 deletions test/lengthGt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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);
});

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);
});

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);
});

it(`should be curried`, function() {
eq(RA.lengthGt(1, [1, 2]), true);
eq(RA.lengthGt(1)([1, 2]), true);
});
});
33 changes: 33 additions & 0 deletions test/lengthGte.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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);
});

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);
});

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);
});

it(`should be curried`, function() {
eq(RA.lengthGte(1, [1]), true);
eq(RA.lengthGte(1)([1]), true);
});
});
31 changes: 31 additions & 0 deletions test/lengthLt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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);
});

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);
});

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);
});

it(`should be curried`, function() {
eq(RA.lengthLt(2, [1]), true);
eq(RA.lengthLt(2)([1]), true);
});
});
33 changes: 33 additions & 0 deletions test/lengthLte.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as RA from '../src/index';
import eq from './shared/eq';

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

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

it(`should return false for values without a length property`, function() {
eq(RA.lengthLte(1, NaN), false);
eq(RA.lengthLte(1, undefined), false);
eq(RA.lengthLte(1, null), false);
eq(RA.lengthLte(1, {}), false);
eq(RA.lengthLte(1, true), false);
eq(RA.lengthLte(1, false), false);
eq(RA.lengthLte(1, 5), false);
});

it(`should be curried`, function() {
eq(RA.lengthLte(1, [1]), true);
eq(RA.lengthLte(1)([1]), true);
});
});

0 comments on commit 47d4560

Please sign in to comment.