Skip to content

Commit

Permalink
feature: add omitIndexes (char0n#180)
Browse files Browse the repository at this point in the history
Closes char0n#174
  • Loading branch information
srghma2 committed Jan 13, 2018
1 parent 5829627 commit e8daa71
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,12 @@ declare namespace RamdaAdjunct {
sliceTo<T>(toIndex: number, list: string|Array<T>): string|Array<T>;
sliceTo(toIndex: number): <T>(list: string|Array<T>) => string|Array<T>;

/**
* Returns a partial copy of an array omitting the indexes specified.
*/
omitIndexes<T>(indexes: number[], list: Array<T>): Array<T>;
omitIndexes(indexes: number[]): <T>(list: Array<T>) => Array<T>;

/**
* Identity type.
*/
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import reduceP from './reduceP';
import reduceRightP from './reduceRightP';
import sliceFrom from './sliceFrom';
import sliceTo from './sliceTo';
import omitIndexes from './omitIndexes';
// Object
import paths from './paths';
import renameKeys from './renameKeys';
Expand Down Expand Up @@ -162,6 +163,7 @@ export { default as reduceP } from './reduceP';
export { default as reduceRightP } from './reduceRightP';
export { default as sliceFrom } from './sliceFrom';
export { default as sliceTo } from './sliceTo';
export { default as omitIndexes } from './omitIndexes';
// Object
export { default as paths } from './paths';
export { default as renameKeys } from './renameKeys';
Expand Down Expand Up @@ -262,6 +264,7 @@ const RA = {
reduceRightP,
sliceFrom,
sliceTo,
omitIndexes,
// Object
defaults: mergeRight,
resetToDefault: mergeRight,
Expand Down
25 changes: 25 additions & 0 deletions src/omitIndexes.js
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;
46 changes: 46 additions & 0 deletions test/omitIndexes.js
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);
});
});

0 comments on commit e8daa71

Please sign in to comment.