1 | require './_prepare'
|
---|
2 |
|
---|
3 | array = mod 'array'
|
---|
4 |
|
---|
5 | test 'from', ->
|
---|
6 |
|
---|
7 | array.from([1]).should.be.an.instanceOf Array
|
---|
8 | array.from([1])[0].should.equal 1
|
---|
9 |
|
---|
10 | # test 'clone', ->
|
---|
11 |
|
---|
12 | # a = [0, 1, 2]
|
---|
13 |
|
---|
14 | # b = array.clone a
|
---|
15 |
|
---|
16 | # b[0].should.equal 0
|
---|
17 | # b[1].should.equal 1
|
---|
18 |
|
---|
19 | # b[0] = 3
|
---|
20 |
|
---|
21 | # a[0].should.equal 0
|
---|
22 |
|
---|
23 | test 'pluck', ->
|
---|
24 |
|
---|
25 | a = [0, 1, 2, 3]
|
---|
26 |
|
---|
27 | after = array.pluck a, 1
|
---|
28 |
|
---|
29 | after.length.should.equal 3
|
---|
30 |
|
---|
31 | after[0].should.equal 0
|
---|
32 | after[1].should.equal 2
|
---|
33 | after[2].should.equal 3
|
---|
34 | after.should.equal a
|
---|
35 |
|
---|
36 | test 'pluckMultiple', ->
|
---|
37 |
|
---|
38 | a = [0, 1, 2, 3, 4, 5, 6]
|
---|
39 |
|
---|
40 | array.pluckMultiple a, [0, 4, 2, 6]
|
---|
41 |
|
---|
42 | a.length.should.equal 3
|
---|
43 | a[0].should.equal 1
|
---|
44 | a[1].should.equal 3
|
---|
45 | a[2].should.equal 5
|
---|
46 |
|
---|
47 | test 'pluckItem', ->
|
---|
48 |
|
---|
49 | a = [0, 1, 2, 3, 2, 4, 2]
|
---|
50 |
|
---|
51 | array.pluckItem a, 2
|
---|
52 |
|
---|
53 | a[0].should.equal 0
|
---|
54 | a[1].should.equal 1
|
---|
55 | a[2].should.equal 3
|
---|
56 | a[3].should.equal 4
|
---|
57 |
|
---|
58 | array.pluckItem([1], 2).length.should.equal 1
|
---|
59 |
|
---|
60 |
|
---|
61 | test 'pluckOneItem', ->
|
---|
62 |
|
---|
63 | a = [0, 1, 2, 3, 2, 4, 2]
|
---|
64 |
|
---|
65 | array.pluckOneItem a, 2
|
---|
66 |
|
---|
67 | a[0].should.equal 0
|
---|
68 | a[1].should.equal 1
|
---|
69 | a[2].should.equal 3
|
---|
70 | a[3].should.equal 2
|
---|
71 | a[4].should.equal 4
|
---|
72 | a[5].should.equal 2
|
---|
73 |
|
---|
74 | a = [1, 2]
|
---|
75 |
|
---|
76 | array.pluckOneItem a, 1
|
---|
77 |
|
---|
78 | a.length.should.equal 1
|
---|
79 | a[0].should.equal 2
|
---|
80 |
|
---|
81 | array.pluckOneItem([], 1).length.should.equal 0
|
---|
82 |
|
---|
83 | array.pluckOneItem([1], 2).length.should.equal 1
|
---|
84 |
|
---|
85 | test 'plcukByCallback', ->
|
---|
86 |
|
---|
87 | a = [0, 1, 2, 3]
|
---|
88 |
|
---|
89 | array.pluckByCallback a, (val, i) ->
|
---|
90 |
|
---|
91 | return yes if val is 2
|
---|
92 |
|
---|
93 | return no
|
---|
94 |
|
---|
95 | a[0].should.equal 0
|
---|
96 | a[1].should.equal 1
|
---|
97 | a[2].should.equal 3
|
---|
98 |
|
---|
99 | test 'injectByCallback', ->
|
---|
100 |
|
---|
101 | shouldInject = (valA, valB, toInject) ->
|
---|
102 |
|
---|
103 | unless valA?
|
---|
104 |
|
---|
105 | return yes if toInject <= valB
|
---|
106 |
|
---|
107 | return no
|
---|
108 |
|
---|
109 | unless valB?
|
---|
110 |
|
---|
111 | return yes if valA <= toInject
|
---|
112 |
|
---|
113 | return no
|
---|
114 |
|
---|
115 | return yes if valA <= toInject <= valB
|
---|
116 |
|
---|
117 | return no
|
---|
118 |
|
---|
119 | a = [0.5, 1, 2.5, 2.5, 2.75, 2.75, 3]
|
---|
120 |
|
---|
121 | array.injectByCallback a, 0, shouldInject
|
---|
122 |
|
---|
123 | a[0].should.equal 0
|
---|
124 | a[1].should.equal 0.5
|
---|
125 | a[7].should.equal 3
|
---|
126 |
|
---|
127 | a = [0.5, 1, 2.5, 2.5, 2.75, 2.75, 3]
|
---|
128 |
|
---|
129 | array.injectByCallback a, 2.7, shouldInject
|
---|
130 |
|
---|
131 | a[0].should.equal 0.5
|
---|
132 | a[4].should.equal 2.7
|
---|
133 | a[5].should.equal 2.75
|
---|
134 | a[7].should.equal 3
|
---|
135 |
|
---|
136 | a = [0.5, 1, 2.5, 2.5, 2.75, 2.75, 3]
|
---|
137 |
|
---|
138 | array.injectByCallback a, 3.2, shouldInject
|
---|
139 |
|
---|
140 | a[0].should.equal 0.5
|
---|
141 | a[4].should.equal 2.75
|
---|
142 | a[6].should.equal 3
|
---|
143 | a[7].should.equal 3.2 |
---|