| 1 | var assert = require('assert');
|
|---|
| 2 | var slice = require('../lib/slice');
|
|---|
| 3 |
|
|---|
| 4 | var data = ['a', 'b', 'c', 'd', 'e', 'f'];
|
|---|
| 5 |
|
|---|
| 6 | suite('slice', function() {
|
|---|
| 7 |
|
|---|
| 8 | test('no params yields copy', function() {
|
|---|
| 9 | assert.deepEqual(slice(data), data);
|
|---|
| 10 | });
|
|---|
| 11 |
|
|---|
| 12 | test('no end param defaults to end', function() {
|
|---|
| 13 | assert.deepEqual(slice(data, 2), data.slice(2));
|
|---|
| 14 | });
|
|---|
| 15 |
|
|---|
| 16 | test('zero end param yields empty', function() {
|
|---|
| 17 | assert.deepEqual(slice(data, 0, 0), []);
|
|---|
| 18 | });
|
|---|
| 19 |
|
|---|
| 20 | test('first element with explicit params', function() {
|
|---|
| 21 | assert.deepEqual(slice(data, 0, 1, 1), ['a']);
|
|---|
| 22 | });
|
|---|
| 23 |
|
|---|
| 24 | test('last element with explicit params', function() {
|
|---|
| 25 | assert.deepEqual(slice(data, -1, 6), ['f']);
|
|---|
| 26 | });
|
|---|
| 27 |
|
|---|
| 28 | test('empty extents and negative step reverses', function() {
|
|---|
| 29 | assert.deepEqual(slice(data, null, null, -1), ['f', 'e', 'd', 'c', 'b', 'a']);
|
|---|
| 30 | });
|
|---|
| 31 |
|
|---|
| 32 | test('negative step partial slice', function() {
|
|---|
| 33 | assert.deepEqual(slice(data, 4, 2, -1), ['e', 'd']);
|
|---|
| 34 | });
|
|---|
| 35 |
|
|---|
| 36 | test('negative step partial slice no start defaults to end', function() {
|
|---|
| 37 | assert.deepEqual(slice(data, null, 2, -1), ['f', 'e', 'd']);
|
|---|
| 38 | });
|
|---|
| 39 |
|
|---|
| 40 | test('extents clamped end', function() {
|
|---|
| 41 | assert.deepEqual(slice(data, null, 100), data);
|
|---|
| 42 | });
|
|---|
| 43 |
|
|---|
| 44 | test('extents clamped beginning', function() {
|
|---|
| 45 | assert.deepEqual(slice(data, -100, 100), data);
|
|---|
| 46 | });
|
|---|
| 47 |
|
|---|
| 48 | test('backwards extents yields empty', function() {
|
|---|
| 49 | assert.deepEqual(slice(data, 2, 1), []);
|
|---|
| 50 | });
|
|---|
| 51 |
|
|---|
| 52 | test('zero step gets shot down', function() {
|
|---|
| 53 | assert.throws(function() { slice(data, null, null, 0) });
|
|---|
| 54 | });
|
|---|
| 55 |
|
|---|
| 56 | });
|
|---|
| 57 |
|
|---|