Blame view

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