Skip to content

Commit

Permalink
test and bugfix match
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-king committed Mar 18, 2018
1 parent 6087306 commit cb41cce
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/portable-fp.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
length: arg => _prop('length', arg, Number, NaN),
map: (fn, list) => _map(_arity(1, fn), list, _isObject(list) ? {} : []),
mapObjIndexed: (fn, list) => _map(fn, list),
match: _invoke(0, 'match'),
match: (rgx, str) => _invoke(1, 'match', [], ''.match)(rgx, str) || [],
nAry: (n, fn) => _arity(n, fn),
partial: _prt,
pick: (keys, obj) => keys.reduce((accum, k) => (accum[k] = obj[k], accum), {}),
Expand Down
30 changes: 30 additions & 0 deletions test/match.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const
{ match } = require('portable-fp'),
{ testCurrying } = require('./util'),
{ expect } = require('chai');

describe('match :: RegExp → String → [String | undefined]', function() {

it('returns the list of regular expression matches in a string', function() {
expect(match('', 'aa3a')).eql(['']);
expect(match('a', 'aa3a')).eql(['a']);
expect(match(/a/g, 'aa3a')).eql(['a', 'a', 'a']);
expect(match('ab', 'aa3a')).eql([]);
});

it('throws error when given null or undefined', function() {
const run = arg => () => match('', arg);
expect(run(null)).throw(TypeError);
expect(run(undefined)).throw(TypeError);
});

it('returns empty array on invalid input string', function() {
[true, false, {}, [], Infinity, NaN, 4]
.map(a => expect(match(/x/, a)).eql([]));
});

it('has arity of 2', () => expect(match).lengthOf(2));

it('is curried', testCurrying(match, [/x/, 4], []));

});

0 comments on commit cb41cce

Please sign in to comment.