|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +describe('Filter: slice', function() { |
| 4 | + var items; |
| 5 | + var str |
| 6 | + var slice; |
| 7 | + |
| 8 | + beforeEach(inject(function($filter) { |
| 9 | + items = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; |
| 10 | + str = "stuvwxyz"; |
| 11 | + slice = $filter('slice'); |
| 12 | + })); |
| 13 | + |
| 14 | + it('should return all items after index X when X is positive', function() { |
| 15 | + expect(slice(items, 4)).toEqual(['e', 'f', 'g', 'h']); |
| 16 | + expect(slice(items, '4')).toEqual(['e', 'f', 'g', 'h']); |
| 17 | + |
| 18 | + expect(slice(str, 4)).toEqual("wxyz"); |
| 19 | + expect(slice(str, '4')).toEqual("wxyz"); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should return the last X items when X is negative', function() { |
| 23 | + expect(slice(items, -3)).toEqual(['f', 'g', 'h']); |
| 24 | + expect(slice(items, '-3')).toEqual(['f', 'g', 'h']); |
| 25 | + |
| 26 | + expect(slice(str, -3)).toEqual("xyz"); |
| 27 | + expect(slice(str, '-3')).toEqual("xyz"); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should return an extracted array between index X and X', function () { |
| 31 | + expect(slice(items, 3, 6)).toEqual(['d', 'e', 'f']); |
| 32 | + expect(slice(items, '3', '6')).toEqual(['d', 'e', 'f']); |
| 33 | + |
| 34 | + expect(slice(str, 3, 6)).toEqual("vwx"); |
| 35 | + expect(slice(str, '3', '6')).toEqual("vwx"); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should return a string or array the same as slice()', function () { |
| 39 | + expect(slice(items, -2, -4)).toEqual([]); |
| 40 | + expect(slice(items, -4, -2)).toEqual(['e', 'f']); |
| 41 | + expect(slice(items, 2, -2)).toEqual(['c', 'd', 'e', 'f']); |
| 42 | + expect(slice(items, 2, -8)).toEqual([]); |
| 43 | + |
| 44 | + expect(slice(str, -2, -4)).toEqual(""); |
| 45 | + expect(slice(str, -4, -2)).toEqual("wx"); |
| 46 | + expect(slice(str, 2, -2)).toEqual("uvwx"); |
| 47 | + expect(slice(str, -2, -8)).toEqual(""); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should return the source array when X cannot be parsed', function() { |
| 51 | + expect(slice(items, 'bogus')).toEqual(items); |
| 52 | + expect(slice(items, 'null')).toEqual(items); |
| 53 | + expect(slice(items, 'undefined')).toEqual(items); |
| 54 | + expect(slice(items, null)).toEqual(items); |
| 55 | + expect(slice(items, undefined)).toEqual(items); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should return the source string when X cannot be parsed', function() { |
| 59 | + expect(slice(str, 'bogus')).toEqual(str); |
| 60 | + expect(slice(str, 'null')).toEqual(str); |
| 61 | + expect(slice(str, 'undefined')).toEqual(str); |
| 62 | + expect(slice(str, null)).toEqual(str); |
| 63 | + expect(slice(str, undefined)).toEqual(str); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should return input if not String or Array', function() { |
| 67 | + expect(slice(1,1)).toEqual(1); |
| 68 | + expect(slice(null, 1)).toEqual(null); |
| 69 | + expect(slice(undefined, 1)).toEqual(undefined); |
| 70 | + expect(slice({}, 1)).toEqual({}); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should return an empty array or string if X is positive and exceeds input length', function () { |
| 74 | + expect(slice(items, 9)).toEqual([]); |
| 75 | + expect(slice(items, '9')).toEqual([]); |
| 76 | + |
| 77 | + expect(slice(str, 9)).toEqual(""); |
| 78 | + expect(slice(str, '9')).toEqual(""); |
| 79 | + |
| 80 | + expect(slice(items, 9)).not.toBe(items); |
| 81 | + expect(slice(str, 9)).not.toBe(str); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should return the source array or string if X is negative and exceeds input length', function() { |
| 85 | + expect(slice(items, -9)).toEqual(items); |
| 86 | + expect(slice(items, '-9')).toEqual(items); |
| 87 | + |
| 88 | + expect(slice(str, -9)).toEqual(str); |
| 89 | + expect(slice(str, '-9')).toEqual(str); |
| 90 | + }); |
| 91 | + |
| 92 | +}); |
0 commit comments