forked from char0n/ramda-adjunct
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add omitIndexes (char0n#180)
Closes char0n#174
- Loading branch information
Showing
4 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { contains, curry, addIndex, reject } from 'ramda'; | ||
|
||
// helpers | ||
const rejectIndexed = addIndex(reject); | ||
const containsIndex = curry((indexes, val, index) => contains(index, indexes)); | ||
|
||
/** | ||
* Returns a partial copy of an array omitting the indexes specified. | ||
* | ||
* @func omitIndexes | ||
* @memberOf RA | ||
* @since {@link https://char0n.github.io/ramda-adjunct/1.19.0|v1.19.0} | ||
* @category List | ||
* @sig [i] -> [a] -> [a] | ||
* @see {@link http://ramdajs.com/docs/#omit|omit} | ||
* @param {!Array} indexes The array of indexes to omit from the new array | ||
* @param {!Array} list The array to copy from | ||
* @return {!Array} The new array with indexes not on it | ||
* @example | ||
* | ||
* RA.omitIndexes([-1, 1, 3], ['a', 'b', 'c', 'd']); //=> ['a', 'c'] | ||
*/ | ||
const omitIndexes = curry((indexes, list) => rejectIndexed(containsIndex(indexes), list)); | ||
|
||
export default omitIndexes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import RA from '../src/index'; | ||
import eq from './shared/eq'; | ||
|
||
|
||
describe('omitIndexes', function() { | ||
it('tests currying', function () { | ||
const indexes = [1, 3]; | ||
const arr = ['a', 'b', 'c', 'd']; | ||
const expected = ['a', 'c']; | ||
|
||
eq(RA.omitIndexes(indexes, arr), expected); | ||
eq(RA.omitIndexes(indexes)(arr), expected); | ||
}); | ||
|
||
it('tests non existing indexes are omitted', function() { | ||
const indexes = [-1, 1, 3]; | ||
const arr = ['a', 'b', 'c', 'd']; | ||
const expected = ['a', 'c']; | ||
|
||
eq(RA.omitIndexes(indexes, arr), expected); | ||
}); | ||
|
||
it('tests when empty indexes', function() { | ||
const indexes = []; | ||
const arr = ['a', 'b', 'c', 'd']; | ||
const expected = arr; | ||
|
||
eq(RA.omitIndexes(indexes, arr), expected); | ||
}); | ||
|
||
it('tests when empty list', function() { | ||
const indexes = [1, 3]; | ||
const arr = []; | ||
const expected = arr; | ||
|
||
eq(RA.omitIndexes(indexes, arr), expected); | ||
}); | ||
|
||
it('tests when empty indexes and list', function() { | ||
const indexes = []; | ||
const arr = []; | ||
const expected = arr; | ||
|
||
eq(RA.omitIndexes(indexes, arr), expected); | ||
}); | ||
}); |