-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
80 lines (72 loc) · 2.31 KB
/
index.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var sliced = require('../')
var assert = require('assert')
describe('sliced', function(){
it('exports a function', function(){
assert.equal('function', typeof sliced);
})
describe('with 1 arg', function(){
it('returns an array of the arg', function(){
var o = [3, "4", {}];
var r = sliced(o);
assert.equal(3, r.length);
assert.equal(o[0], r[0]);
assert.equal(o[1], r[1]);
assert.equal(o[1], r[1]);
})
})
describe('with 2 args', function(){
it('returns an array of the arg starting at the 2nd arg', function(){
var o = [3, "4", 5, null];
var r = sliced(o, 2);
assert.equal(2, r.length);
assert.equal(o[2], r[0]);
assert.equal(o[3], r[1]);
})
})
describe('with 3 args', function(){
it('returns an array of the arg from the 2nd to the 3rd arg', function(){
var o = [3, "4", 5, null];
var r = sliced(o, 1, 2);
assert.equal(1, r.length);
assert.equal(o[1], r[0]);
})
})
describe('with negative start and no end', function(){
it('begins at an offset from the end and includes all following elements', function(){
var o = [3, "4", 5, null];
var r = sliced(o, -2);
assert.equal(2, r.length);
assert.equal(o[2], r[0]);
assert.equal(o[3], r[1]);
var r = sliced(o, -12);
assert.equal(4, r.length);
assert.equal(o[0], r[0]);
assert.equal(o[1], r[1]);
})
})
describe('with negative start and positive end', function(){
it('begins at an offset from the end and includes `end` elements', function(){
var o = [3, "4", {x:1}, null];
var r = sliced(o, -2, 1);
assert.equal(0, r.length);
var r = sliced(o, -2, 2);
assert.equal(0, r.length);
var r = sliced(o, -2, 3);
assert.equal(1, r.length);
assert.equal(o[2], r[0]);
})
})
describe('with negative start and negative end', function(){
it('begins at `start` offset from the end and includes all elements up to `end` offset from the end', function(){
var o = [3, "4", {x:1}, null];
var r = sliced(o, -3, -1);
assert.equal(2, r.length);
assert.equal(o[1], r[0]);
assert.equal(o[2], r[1]);
var r = sliced(o, -3, -3);
assert.equal(0, r.length);
var r = sliced(o, -3, -4);
assert.equal(0, r.length);
})
})
})