From e8daa71b464aa01f41c7b5b2859d3d9b97780dd8 Mon Sep 17 00:00:00 2001 From: Sergey Homa Date: Mon, 23 Oct 2017 22:41:54 +0300 Subject: [PATCH] feature: add omitIndexes (#180) Closes #174 --- src/index.d.ts | 6 ++++++ src/index.js | 3 +++ src/omitIndexes.js | 25 ++++++++++++++++++++++++ test/omitIndexes.js | 46 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 src/omitIndexes.js create mode 100644 test/omitIndexes.js diff --git a/src/index.d.ts b/src/index.d.ts index bf503cb617..4222ba24f0 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -557,6 +557,12 @@ declare namespace RamdaAdjunct { sliceTo(toIndex: number, list: string|Array): string|Array; sliceTo(toIndex: number): (list: string|Array) => string|Array; + /** + * Returns a partial copy of an array omitting the indexes specified. + */ + omitIndexes(indexes: number[], list: Array): Array; + omitIndexes(indexes: number[]): (list: Array) => Array; + /** * Identity type. */ diff --git a/src/index.js b/src/index.js index 70d3d447b8..73c02029b0 100644 --- a/src/index.js +++ b/src/index.js @@ -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'; @@ -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'; @@ -262,6 +264,7 @@ const RA = { reduceRightP, sliceFrom, sliceTo, + omitIndexes, // Object defaults: mergeRight, resetToDefault: mergeRight, diff --git a/src/omitIndexes.js b/src/omitIndexes.js new file mode 100644 index 0000000000..b0c1ade6d9 --- /dev/null +++ b/src/omitIndexes.js @@ -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; diff --git a/test/omitIndexes.js b/test/omitIndexes.js new file mode 100644 index 0000000000..54ad2ffab3 --- /dev/null +++ b/test/omitIndexes.js @@ -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); + }); +});