2 var sliced = require('../')
3 var assert = require('assert')
5 describe('sliced', function(){
6 it('exports a function', function(){
7 assert.equal('function', typeof sliced);
9 describe('with 1 arg', function(){
10 it('returns an array of the arg', function(){
13 assert.equal(3, r.length);
14 assert.equal(o[0], r[0]);
15 assert.equal(o[1], r[1]);
16 assert.equal(o[1], r[1]);
19 describe('with 2 args', function(){
20 it('returns an array of the arg starting at the 2nd arg', function(){
21 var o = [3, "4", 5, null];
23 assert.equal(2, r.length);
24 assert.equal(o[2], r[0]);
25 assert.equal(o[3], r[1]);
28 describe('with 3 args', function(){
29 it('returns an array of the arg from the 2nd to the 3rd arg', function(){
30 var o = [3, "4", 5, null];
31 var r = sliced(o, 1, 2);
32 assert.equal(1, r.length);
33 assert.equal(o[1], r[0]);
36 describe('with negative start and no end', function(){
37 it('begins at an offset from the end and includes all following elements', function(){
38 var o = [3, "4", 5, null];
39 var r = sliced(o, -2);
40 assert.equal(2, r.length);
41 assert.equal(o[2], r[0]);
42 assert.equal(o[3], r[1]);
44 var r = sliced(o, -12);
45 assert.equal(4, r.length);
46 assert.equal(o[0], r[0]);
47 assert.equal(o[1], r[1]);
50 describe('with negative start and positive end', function(){
51 it('begins at an offset from the end and includes `end` elements', function(){
52 var o = [3, "4", {x:1}, null];
54 var r = sliced(o, -2, 1);
55 assert.equal(0, r.length);
57 var r = sliced(o, -2, 2);
58 assert.equal(0, r.length);
60 var r = sliced(o, -2, 3);
61 assert.equal(1, r.length);
62 assert.equal(o[2], r[0]);
65 describe('with negative start and negative end', function(){
66 it('begins at `start` offset from the end and includes all elements up to `end` offset from the end', function(){
67 var o = [3, "4", {x:1}, null];
68 var r = sliced(o, -3, -1);
69 assert.equal(2, r.length);
70 assert.equal(o[1], r[0]);
71 assert.equal(o[2], r[1]);
73 var r = sliced(o, -3, -3);
74 assert.equal(0, r.length);
76 var r = sliced(o, -3, -4);
77 assert.equal(0, r.length);