Skip to content

Commit

Permalink
fix(pickIndexes): omits values for non existing indexes
Browse files Browse the repository at this point in the history
Closes char0n#181
  • Loading branch information
char0n authored and srghma2 committed Jan 13, 2018
1 parent 2881bd6 commit 6e4b9b3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
10 changes: 8 additions & 2 deletions src/pickIndexes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { curryN, compose, values, pickAll } from 'ramda';
import { filter, addIndex, curry, contains } from 'ramda';


// helpers
const filterIndexed = addIndex(filter);
const containsIndex = curry((indexes, val, index) => contains(index, indexes));


/**
* Picks values from list by indexes.
Expand All @@ -16,6 +22,6 @@ import { curryN, compose, values, pickAll } from 'ramda';
*
* RA.pickIndexes([0, 2], ['a', 'b', 'c']); //=> ['a', 'c']
*/
const pickIndexes = curryN(2, compose(values, pickAll));
const pickIndexes = curry((indexes, list) => filterIndexed(containsIndex(indexes), list));

export default pickIndexes;
32 changes: 24 additions & 8 deletions test/pickIndexes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,39 @@ import eq from './shared/eq';


describe('pickIndexes', function() {
let list;

beforeEach(function() {
list = ['a', 'b', 'c'];
});

it('tests picking values from list by indexes', function() {
eq(RA.pickIndexes([0, 2], ['a', 'b', 'c']), ['a', 'c']);
eq(RA.pickIndexes([0, 2], list), ['a', 'c']);
});

it('tests currying', function() {
eq(RA.pickIndexes([0, 2])(['a', 'b', 'c']), ['a', 'c']);
eq(RA.pickIndexes([], []), []);
eq(RA.pickIndexes([])([]), []);
});

it('tests picking values from list by indexes out of range', function() {
eq(RA.pickIndexes([0, 5], ['a', 'b', 'c']), ['a', undefined]);

context("when indexes doesn't exist", function() {
specify('should skip these indexes', function() {
eq(RA.pickIndexes([-1, 0, 5], list), ['a']);
});
});

it('tests picking values from list by non array', function() {
chai.assert.throws(RA.pickIndexes.bind(null, undefined, ['a', 'b', 'c']), TypeError);
// todo fix function parens inconsistencies
context('when indexes is a non-array', function() {
it('should produce TypeError', function() {
chai.assert.throws(RA.pickIndexes.bind(null, undefined, list), TypeError);
});
});

it('tests picking values from non-array', function() {
chai.assert.throws(RA.pickIndexes.bind(null, [0, 2], undefined), TypeError);

context('when list is a non-array', function() {
it('should product TypeError', function() {
chai.assert.throws(RA.pickIndexes.bind(null, [0, 2], undefined), TypeError);
});
});
});

0 comments on commit 6e4b9b3

Please sign in to comment.