forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnth.js
42 lines (32 loc) · 1.05 KB
/
nth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var assert = require('assert');
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
describe('nth', function() {
var list = ['foo', 'bar', 'baz', 'quux'];
it('accepts positive offsets', function() {
eq(R.nth(0, list), 'foo');
eq(R.nth(1, list), 'bar');
eq(R.nth(2, list), 'baz');
eq(R.nth(3, list), 'quux');
eq(R.nth(4, list), undefined);
eq(R.nth(0, 'abc'), 'a');
eq(R.nth(1, 'abc'), 'b');
eq(R.nth(2, 'abc'), 'c');
eq(R.nth(3, 'abc'), undefined);
});
it('accepts negative offsets', function() {
eq(R.nth(-1, list), 'quux');
eq(R.nth(-2, list), 'baz');
eq(R.nth(-3, list), 'bar');
eq(R.nth(-4, list), 'foo');
eq(R.nth(-5, list), undefined);
eq(R.nth(-1, 'abc'), 'c');
eq(R.nth(-2, 'abc'), 'b');
eq(R.nth(-3, 'abc'), 'a');
eq(R.nth(-4, 'abc'), undefined);
});
it('throws if applied to null or undefined', function() {
assert.throws(function() { R.nth(0, null); }, TypeError);
assert.throws(function() { R.nth(0, undefined); }, TypeError);
});
});