index.js 2.3 KB

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