Skip to content

Commit

Permalink
add tests for head, last, and tail, fix string handling for head and …
Browse files Browse the repository at this point in the history
…last
  • Loading branch information
evan-king committed Mar 15, 2018
1 parent 0b79944 commit 67d85e7
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/portable-fp.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@
filter: (fn, list) => _prop('filter', list, Function, _filter).call(list, fn, list),
find: (fn, list) => _prop(_findKey(fn, list), list),
findIndex: (fn, list) => _is(Array, list) ? list.findIndex(fn) : _findKey(fn, list),
head: list => list[1],
head: list => _is(String, list) ? list[0] || '' : list[0],
identity: x => x,
init: _invoke(2, 'slice', [0, -1], [].slice, 'init'),
invoker: _invoke,
is: _is,
keys: arg => _keys(arg),
keysIn: arg => _keys(arg, false),
last: list => list[list.length - 1],
last: list => _is(String, list) ? list[list.length - 1] || '' : list[list.length - 1],
length: arg => _prop('length', arg, Number, NaN),
map: (fn, list) => _map(_arity(1, fn), list, _isObject(list) ? {} : []),
mapObjIndexed: (fn, list) => _map(fn, list),
Expand Down
3 changes: 0 additions & 3 deletions test/API.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,12 @@ const args = [null, undefined, false, true, '', 'a', '0xFF', 5, String, /x/, x =

with(R) {

// head
// is
// last
// match
// pick
// prop
// range
// reverse
// tail


}
40 changes: 40 additions & 0 deletions test/head.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const
{ head } = require('portable-fp'),
{ sparseList } = require('./util'),
{ expect } = require('chai');

describe('head :: [a] → a', function() {

it('returns the first element in a list', function() {
expect(head([])).eql(undefined);
expect(head(['a'])).eql('a');
expect(head(sparseList)).eql(sparseList[0]);
});

it('returns the first character of a string', function() {
expect(head('')).eql('');
expect(head('a')).eql('a');
expect(head('ab')).eql('a');
});

it('returns the first value of an arguments object', function() {
function fn() {return arguments};
expect(head(fn())).eql(undefined);
expect(head(fn(1))).eql(1);
expect(head(fn(1, 2))).eql(1);
});

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

it('returns empty array on invalid defined input', function() {
const args = [true, false, {}, head, x => x, /x/, String];
args.map(arg => expect(head(arg)).eql(undefined));
});

it('has arity of 1', () => expect(head).lengthOf(1));

});
5 changes: 2 additions & 3 deletions test/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ describe('init :: [a] → [a]', function() {
});

// Known divergence from Ramda behavior (these tests would fail):
// - expect(init(String)).eql('');
// - expect(init(String)).eql('0'); // []
// Instead, portable-fp is consistent with head and last

it('returns empty array on (some) invalid defined input', function() {
const args = [NaN, true, false, {}, init, x => x, /x/];
Expand All @@ -45,6 +46,4 @@ describe('init :: [a] → [a]', function() {

it('has arity of 1', () => expect(init).lengthOf(1));

it('is curried', testCurrying(init, ['blah'], 'bla'));

});
41 changes: 41 additions & 0 deletions test/last.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const
{ last } = require('portable-fp'),
{ sparseList } = require('./util'),
{ expect } = require('chai');

describe('last :: [a] → a', function() {

it('returns the last element in a list', function() {
expect(last([])).eql(undefined);
expect(last(['a'])).eql('a');
expect(last(sparseList)).eql(sparseList[sparseList.length-1]);
});

it('returns the last character of a string', function() {
expect(last('')).eql('');
expect(last('a')).eql('a');
expect(last('ab')).eql('b');
});

it('returns the last value of an arguments object', function() {
function fn() {return arguments};
expect(last(fn())).eql(undefined);
expect(last(fn(1))).eql(1);
expect(last(fn(1, 2))).eql(2);
expect(last(fn(1, 2, undefined))).eql(undefined);
});

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

it('returns empty array on invalid defined input', function() {
const args = [true, false, {}, last, x => x, /x/, String];
args.map(arg => expect(last(arg)).eql(undefined));
});

it('has arity of 1', () => expect(last).lengthOf(1));

});
49 changes: 49 additions & 0 deletions test/tail.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const
{ tail } = require('portable-fp'),
{ testCurrying } = require('./util'),
{ expect } = require('chai');

describe('tail :: [a] → [a]', function() {

it('returns all but the first element of a list', function() {
const arg = [432, undefined, null, 2.5, 'blah', 0, -5];
expect(tail([])).eql([]);
expect(tail(['a'])).eql([]);
expect(tail(['a', 'b'])).eql(['b']);
expect(tail(arg)).eql(arg.slice(1));
});

it('returns all but the first character of a string', function() {
const arg = 'blah blah';
expect(tail('')).eql('');
expect(tail('a')).eql('');
expect(tail('ab')).eql('b');
expect(tail(arg)).eql(arg.slice(1));
});

it('returns all but the first value of an arguments object', function() {
function fn() {return arguments};
expect(tail(fn())).eql([]);
expect(tail(fn(1))).eql([]);
expect(tail(fn(1, 2))).eql([2]);
expect(tail(fn(1, 2, 3))).eql([2, 3]);
});

// Known divergence from Ramda behavior (test would fail):
// - expect(tail(String)).eql(''); // []
// Instead, portable-fp is consistent with head and last

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

it('returns empty array on (some) invalid defined input', function() {
const args = [NaN, true, false, {}, tail, x => x, /x/];
args.map(arg => expect(tail(arg)).an('array').eql([]));
});

it('has arity of 1', () => expect(tail).lengthOf(1));

});

0 comments on commit 67d85e7

Please sign in to comment.