-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathusing-natives.js
More file actions
246 lines (178 loc) · 5.36 KB
/
using-natives.js
File metadata and controls
246 lines (178 loc) · 5.36 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
* test Constructor.extend() with natives
*
* YES! You can extend native constructors, provided you are aware of a couple things.
* In this set of tests, we'll use extend to subclass the Array constructor and provide workarounds for
* for constructor initialization and [[Class]] dependent methods (toString()).
*
* BUT ~ 2 tests provided by @Raynos2 show that modifying the length property directly has side-
* effects (like clobbering the array).
*
*/
var test = require('tape');
var Constructor = require('../../constructor.js').Constructor;
/*
* FIXTURE - fairly complete example of extending the native built-in Arrays.
*
* NOTE: the first argument here could be the Array constructor.
*/
var SubArray = Constructor.extend([], {
constructor: function () {
[].slice.call(this);
// stolen from kangax (http://bit.ly/W66vQW)
this.push.apply(this, arguments);
},
// borrowed from kangax (http://bit.ly/W66vQW)
last: function () {
return this[this.length - 1];
},
/**
* @method toString - must be defined explicitly
*
* kangax (http://bit.ly/W66vQW), David Herman in Effective JavaScript (item 40, pp. 106ff), and some
* learning tests tell us that the Array.prototype.toString() method fails on objects whose
* constructor is not really an Array - due to the internal use of [[Class]] by JavaScript engine -
* so we'll have to shim it, using an output string directly.
*
* NOTE - doesn't seem to work in IE 6, 7, or 8
*/
toString: function () {
var array = [];
for (var i = 0; i < this.length; i += 1) {
array.push(this[i]);
}
return array.join();
},
/**
* @method concat - must be defined explicitly
*
* Array.concat works on the Array object and returns a new Array - we have to do the same thing,
* which is to return a new SubArray. And that means we have to shim it.
*/
concat: function () {
var instance = new this.constructor();
for (var i = 0; i < this.length; i += 1) {
instance.push(this[i]);
}
for (i = 0; i < arguments.length; i += 1) {
instance.push(arguments[i]);
}
return instance;
} // watch out for trailing commas in IE 6-8!!!
});
test('SubArray - verify the toString() fix', function (t) {
var b = new SubArray('first', 'middle', 'last');
t.strictEqual(b.toString(), 'first,middle,last', 'toString() fix');
t.end();
});
test('SubArray - verify the concat fix', function (t) {
var b = new SubArray('first');
b = b.concat('last');
t.strictEqual(b.last(), 'last');
t.end();
});
test('SubArray - verify custom method - last', function (t) {
var b = new SubArray('first', 'middle', 'last');
t.strictEqual(b.last(), 'last');
t.end();
});
test('SubArray - index access', function (t) {
var b = new SubArray('first', 'middle', 'last');
t.strictEqual(b[0], 'first');
t.strictEqual(b[1], 'middle');
t.strictEqual(b[2], 'last');
t.end();
});
// failing tests offered by @Raynos2 ~ converted to "passing" by expecting exceptions
test('SubArray - length append', function (t) {
var message = 'length not modified';
var b = new SubArray(0, 1);
var len = b.length;
b[len] = len;
// fails
//t.strictEqual(b.length, len + 1);
try {
if (b.length === len) {
throw new Error(message);
}
t.fail('length unexpectedly modified');
} catch (e) {
t.strictEqual(e.message, message);
}
t.end();
});
// failing tests offered by @Raynos2 ~ converted to "passing" by expecting exceptions
test('SubArray - length reset', function (t) {
var b = new SubArray(0, 1);
b.length = 0;
try {
if ("0" in b) {
throw new Error('zero not removed');
}
t.fail('resetting length somehow removed zero');
} catch (e) {
t.pass(e.message);
}
try {
if (typeof b[0] !== 'undefined') {
throw new Error('zero not undefined');
}
t.fail('resetting length somehow undefined zero');
} catch (e) {
t.pass(e.message);
}
t.end();
});
test('SubArray - length after create', function (t) {
var b = new SubArray('first', 'middle', 'last');
t.strictEqual(b.length, 3);
t.end();
});
test('SubArray - reverse', function (t) {
var b = new SubArray('first', 'middle', 'last');
t.strictEqual(b.reverse().last(), 'first');
t.end();
});
test('SubArray - sort', function (t) {
var b = new SubArray('first', 'middle', 'last');
t.strictEqual(b.sort().last(), 'middle');
t.end();
});
test('SubArray - shift', function (t) {
var b = new SubArray('first', 'middle', 'last');
b.shift();
t.strictEqual(b[0], 'middle');
t.strictEqual(b.length, 2);
t.end();
});
test('SubArray - pop', function (t) {
var b = new SubArray('first', 'middle', 'last');
t.strictEqual(b.pop(), 'last');
t.end();
});
test('SubArray - push', function (t) {
var b = new SubArray('first');
b.push('last');
t.strictEqual(b.last(), 'last');
t.strictEqual(b.length, 2);
t.end();
});
test('SubArray - splice', function (t) {
var b = new SubArray('first');
b.splice(1, 1, 'last');
t.strictEqual(b.last(), 'last');
t.end();
});
test('SubArray - unshift', function (t) {
var b = new SubArray('first');
b.unshift('last');
t.strictEqual(b.last(), 'first');
t.end();
});
/* template */
/*
test('$1 should return $2', function (t) {
t.plan(1);
t.fail('next test to implement');
});
*/