提交 | 用户 | 时间
|
1ac2bc
|
1 |
// Copyright 2009-2012 by contributors, MIT License |
懒 |
2 |
// vim: ts=4 sts=4 sw=4 expandtab |
|
3 |
|
|
4 |
// Module systems magic dance |
|
5 |
(function (definition) { |
|
6 |
// RequireJS |
|
7 |
if (typeof define == "function") { |
|
8 |
define(definition); |
|
9 |
// YUI3 |
|
10 |
} else if (typeof YUI == "function") { |
|
11 |
YUI.add("es5", definition); |
|
12 |
// CommonJS and <script> |
|
13 |
} else { |
|
14 |
definition(); |
|
15 |
} |
|
16 |
})(function () { |
|
17 |
|
|
18 |
/** |
|
19 |
* Brings an environment as close to ECMAScript 5 compliance |
|
20 |
* as is possible with the facilities of erstwhile engines. |
|
21 |
* |
|
22 |
* Annotated ES5: http://es5.github.com/ (specific links below) |
|
23 |
* ES5 Spec: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf |
|
24 |
* Required reading: http://javascriptweblog.wordpress.com/2011/12/05/extending-javascript-natives/ |
|
25 |
*/ |
|
26 |
|
|
27 |
// |
|
28 |
// Function |
|
29 |
// ======== |
|
30 |
// |
|
31 |
|
|
32 |
// ES-5 15.3.4.5 |
|
33 |
// http://es5.github.com/#x15.3.4.5 |
|
34 |
|
|
35 |
function Empty() {} |
|
36 |
|
|
37 |
if (!Function.prototype.bind) { |
|
38 |
Function.prototype.bind = function bind(that) { // .length is 1 |
|
39 |
// 1. Let Target be the this value. |
|
40 |
var target = this; |
|
41 |
// 2. If IsCallable(Target) is false, throw a TypeError exception. |
|
42 |
if (typeof target != "function") { |
|
43 |
throw new TypeError("Function.prototype.bind called on incompatible " + target); |
|
44 |
} |
|
45 |
// 3. Let A be a new (possibly empty) internal list of all of the |
|
46 |
// argument values provided after thisArg (arg1, arg2 etc), in order. |
|
47 |
// XXX slicedArgs will stand in for "A" if used |
|
48 |
var args = _Array_slice_.call(arguments, 1); // for normal call |
|
49 |
// 4. Let F be a new native ECMAScript object. |
|
50 |
// 11. Set the [[Prototype]] internal property of F to the standard |
|
51 |
// built-in Function prototype object as specified in 15.3.3.1. |
|
52 |
// 12. Set the [[Call]] internal property of F as described in |
|
53 |
// 15.3.4.5.1. |
|
54 |
// 13. Set the [[Construct]] internal property of F as described in |
|
55 |
// 15.3.4.5.2. |
|
56 |
// 14. Set the [[HasInstance]] internal property of F as described in |
|
57 |
// 15.3.4.5.3. |
|
58 |
var bound = function () { |
|
59 |
|
|
60 |
if (this instanceof bound) { |
|
61 |
// 15.3.4.5.2 [[Construct]] |
|
62 |
// When the [[Construct]] internal method of a function object, |
|
63 |
// F that was created using the bind function is called with a |
|
64 |
// list of arguments ExtraArgs, the following steps are taken: |
|
65 |
// 1. Let target be the value of F's [[TargetFunction]] |
|
66 |
// internal property. |
|
67 |
// 2. If target has no [[Construct]] internal method, a |
|
68 |
// TypeError exception is thrown. |
|
69 |
// 3. Let boundArgs be the value of F's [[BoundArgs]] internal |
|
70 |
// property. |
|
71 |
// 4. Let args be a new list containing the same values as the |
|
72 |
// list boundArgs in the same order followed by the same |
|
73 |
// values as the list ExtraArgs in the same order. |
|
74 |
// 5. Return the result of calling the [[Construct]] internal |
|
75 |
// method of target providing args as the arguments. |
|
76 |
|
|
77 |
var result = target.apply( |
|
78 |
this, |
|
79 |
args.concat(_Array_slice_.call(arguments)) |
|
80 |
); |
|
81 |
if (Object(result) === result) { |
|
82 |
return result; |
|
83 |
} |
|
84 |
return this; |
|
85 |
|
|
86 |
} else { |
|
87 |
// 15.3.4.5.1 [[Call]] |
|
88 |
// When the [[Call]] internal method of a function object, F, |
|
89 |
// which was created using the bind function is called with a |
|
90 |
// this value and a list of arguments ExtraArgs, the following |
|
91 |
// steps are taken: |
|
92 |
// 1. Let boundArgs be the value of F's [[BoundArgs]] internal |
|
93 |
// property. |
|
94 |
// 2. Let boundThis be the value of F's [[BoundThis]] internal |
|
95 |
// property. |
|
96 |
// 3. Let target be the value of F's [[TargetFunction]] internal |
|
97 |
// property. |
|
98 |
// 4. Let args be a new list containing the same values as the |
|
99 |
// list boundArgs in the same order followed by the same |
|
100 |
// values as the list ExtraArgs in the same order. |
|
101 |
// 5. Return the result of calling the [[Call]] internal method |
|
102 |
// of target providing boundThis as the this value and |
|
103 |
// providing args as the arguments. |
|
104 |
|
|
105 |
// equiv: target.call(this, ...boundArgs, ...args) |
|
106 |
return target.apply( |
|
107 |
that, |
|
108 |
args.concat(_Array_slice_.call(arguments)) |
|
109 |
); |
|
110 |
|
|
111 |
} |
|
112 |
|
|
113 |
}; |
|
114 |
if(target.prototype) { |
|
115 |
Empty.prototype = target.prototype; |
|
116 |
bound.prototype = new Empty(); |
|
117 |
// Clean up dangling references. |
|
118 |
Empty.prototype = null; |
|
119 |
} |
|
120 |
// XXX bound.length is never writable, so don't even try |
|
121 |
// |
|
122 |
// 15. If the [[Class]] internal property of Target is "Function", then |
|
123 |
// a. Let L be the length property of Target minus the length of A. |
|
124 |
// b. Set the length own property of F to either 0 or L, whichever is |
|
125 |
// larger. |
|
126 |
// 16. Else set the length own property of F to 0. |
|
127 |
// 17. Set the attributes of the length own property of F to the values |
|
128 |
// specified in 15.3.5.1. |
|
129 |
|
|
130 |
// TODO |
|
131 |
// 18. Set the [[Extensible]] internal property of F to true. |
|
132 |
|
|
133 |
// TODO |
|
134 |
// 19. Let thrower be the [[ThrowTypeError]] function Object (13.2.3). |
|
135 |
// 20. Call the [[DefineOwnProperty]] internal method of F with |
|
136 |
// arguments "caller", PropertyDescriptor {[[Get]]: thrower, [[Set]]: |
|
137 |
// thrower, [[Enumerable]]: false, [[Configurable]]: false}, and |
|
138 |
// false. |
|
139 |
// 21. Call the [[DefineOwnProperty]] internal method of F with |
|
140 |
// arguments "arguments", PropertyDescriptor {[[Get]]: thrower, |
|
141 |
// [[Set]]: thrower, [[Enumerable]]: false, [[Configurable]]: false}, |
|
142 |
// and false. |
|
143 |
|
|
144 |
// TODO |
|
145 |
// NOTE Function objects created using Function.prototype.bind do not |
|
146 |
// have a prototype property or the [[Code]], [[FormalParameters]], and |
|
147 |
// [[Scope]] internal properties. |
|
148 |
// XXX can't delete prototype in pure-js. |
|
149 |
|
|
150 |
// 22. Return F. |
|
151 |
return bound; |
|
152 |
}; |
|
153 |
} |
|
154 |
|
|
155 |
// Shortcut to an often accessed properties, in order to avoid multiple |
|
156 |
// dereference that costs universally. |
|
157 |
// _Please note: Shortcuts are defined after `Function.prototype.bind` as we |
|
158 |
// us it in defining shortcuts. |
|
159 |
var call = Function.prototype.call; |
|
160 |
var prototypeOfArray = Array.prototype; |
|
161 |
var prototypeOfObject = Object.prototype; |
|
162 |
var _Array_slice_ = prototypeOfArray.slice; |
|
163 |
// Having a toString local variable name breaks in Opera so use _toString. |
|
164 |
var _toString = call.bind(prototypeOfObject.toString); |
|
165 |
var owns = call.bind(prototypeOfObject.hasOwnProperty); |
|
166 |
|
|
167 |
// If JS engine supports accessors creating shortcuts. |
|
168 |
var defineGetter; |
|
169 |
var defineSetter; |
|
170 |
var lookupGetter; |
|
171 |
var lookupSetter; |
|
172 |
var supportsAccessors; |
|
173 |
if ((supportsAccessors = owns(prototypeOfObject, "__defineGetter__"))) { |
|
174 |
defineGetter = call.bind(prototypeOfObject.__defineGetter__); |
|
175 |
defineSetter = call.bind(prototypeOfObject.__defineSetter__); |
|
176 |
lookupGetter = call.bind(prototypeOfObject.__lookupGetter__); |
|
177 |
lookupSetter = call.bind(prototypeOfObject.__lookupSetter__); |
|
178 |
} |
|
179 |
|
|
180 |
// |
|
181 |
// Array |
|
182 |
// ===== |
|
183 |
// |
|
184 |
|
|
185 |
// ES5 15.4.4.12 |
|
186 |
// http://es5.github.com/#x15.4.4.12 |
|
187 |
// Default value for second param |
|
188 |
// [bugfix, ielt9, old browsers] |
|
189 |
// IE < 9 bug: [1,2].splice(0).join("") == "" but should be "12" |
|
190 |
if ([1,2].splice(0).length != 2) { |
|
191 |
var array_splice = Array.prototype.splice; |
|
192 |
|
|
193 |
if(function() { // test IE < 9 to splice bug - see issue #138 |
|
194 |
function makeArray(l) { |
|
195 |
var a = []; |
|
196 |
while (l--) { |
|
197 |
a.unshift(l) |
|
198 |
} |
|
199 |
return a |
|
200 |
} |
|
201 |
|
|
202 |
var array = [] |
|
203 |
, lengthBefore |
|
204 |
; |
|
205 |
|
|
206 |
array.splice.bind(array, 0, 0).apply(null, makeArray(20)); |
|
207 |
array.splice.bind(array, 0, 0).apply(null, makeArray(26)); |
|
208 |
|
|
209 |
lengthBefore = array.length; //20 |
|
210 |
array.splice(5, 0, "XXX"); // add one element |
|
211 |
|
|
212 |
if(lengthBefore + 1 == array.length) { |
|
213 |
return true;// has right splice implementation without bugs |
|
214 |
} |
|
215 |
// else { |
|
216 |
// IE8 bug |
|
217 |
// } |
|
218 |
}()) {//IE 6/7 |
|
219 |
Array.prototype.splice = function(start, deleteCount) { |
|
220 |
if (!arguments.length) { |
|
221 |
return []; |
|
222 |
} else { |
|
223 |
return array_splice.apply(this, [ |
|
224 |
start === void 0 ? 0 : start, |
|
225 |
deleteCount === void 0 ? (this.length - start) : deleteCount |
|
226 |
].concat(_Array_slice_.call(arguments, 2))) |
|
227 |
} |
|
228 |
}; |
|
229 |
} |
|
230 |
else {//IE8 |
|
231 |
Array.prototype.splice = function(start, deleteCount) { |
|
232 |
var result |
|
233 |
, args = _Array_slice_.call(arguments, 2) |
|
234 |
, addElementsCount = args.length |
|
235 |
; |
|
236 |
|
|
237 |
if(!arguments.length) { |
|
238 |
return []; |
|
239 |
} |
|
240 |
|
|
241 |
if(start === void 0) { // default |
|
242 |
start = 0; |
|
243 |
} |
|
244 |
if(deleteCount === void 0) { // default |
|
245 |
deleteCount = this.length - start; |
|
246 |
} |
|
247 |
|
|
248 |
if(addElementsCount > 0) { |
|
249 |
if(deleteCount <= 0) { |
|
250 |
if(start == this.length) { // tiny optimisation #1 |
|
251 |
this.push.apply(this, args); |
|
252 |
return []; |
|
253 |
} |
|
254 |
|
|
255 |
if(start == 0) { // tiny optimisation #2 |
|
256 |
this.unshift.apply(this, args); |
|
257 |
return []; |
|
258 |
} |
|
259 |
} |
|
260 |
|
|
261 |
// Array.prototype.splice implementation |
|
262 |
result = _Array_slice_.call(this, start, start + deleteCount);// delete part |
|
263 |
args.push.apply(args, _Array_slice_.call(this, start + deleteCount, this.length));// right part |
|
264 |
args.unshift.apply(args, _Array_slice_.call(this, 0, start));// left part |
|
265 |
|
|
266 |
// delete all items from this array and replace it to 'left part' + _Array_slice_.call(arguments, 2) + 'right part' |
|
267 |
args.unshift(0, this.length); |
|
268 |
|
|
269 |
array_splice.apply(this, args); |
|
270 |
|
|
271 |
return result; |
|
272 |
} |
|
273 |
|
|
274 |
return array_splice.call(this, start, deleteCount); |
|
275 |
} |
|
276 |
|
|
277 |
} |
|
278 |
} |
|
279 |
|
|
280 |
// ES5 15.4.4.12 |
|
281 |
// http://es5.github.com/#x15.4.4.13 |
|
282 |
// Return len+argCount. |
|
283 |
// [bugfix, ielt8] |
|
284 |
// IE < 8 bug: [].unshift(0) == undefined but should be "1" |
|
285 |
if ([].unshift(0) != 1) { |
|
286 |
var array_unshift = Array.prototype.unshift; |
|
287 |
Array.prototype.unshift = function() { |
|
288 |
array_unshift.apply(this, arguments); |
|
289 |
return this.length; |
|
290 |
}; |
|
291 |
} |
|
292 |
|
|
293 |
// ES5 15.4.3.2 |
|
294 |
// http://es5.github.com/#x15.4.3.2 |
|
295 |
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray |
|
296 |
if (!Array.isArray) { |
|
297 |
Array.isArray = function isArray(obj) { |
|
298 |
return _toString(obj) == "[object Array]"; |
|
299 |
}; |
|
300 |
} |
|
301 |
|
|
302 |
// The IsCallable() check in the Array functions |
|
303 |
// has been replaced with a strict check on the |
|
304 |
// internal class of the object to trap cases where |
|
305 |
// the provided function was actually a regular |
|
306 |
// expression literal, which in V8 and |
|
307 |
// JavaScriptCore is a typeof "function". Only in |
|
308 |
// V8 are regular expression literals permitted as |
|
309 |
// reduce parameters, so it is desirable in the |
|
310 |
// general case for the shim to match the more |
|
311 |
// strict and common behavior of rejecting regular |
|
312 |
// expressions. |
|
313 |
|
|
314 |
// ES5 15.4.4.18 |
|
315 |
// http://es5.github.com/#x15.4.4.18 |
|
316 |
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/forEach |
|
317 |
|
|
318 |
// Check failure of by-index access of string characters (IE < 9) |
|
319 |
// and failure of `0 in boxedString` (Rhino) |
|
320 |
var boxedString = Object("a"), |
|
321 |
splitString = boxedString[0] != "a" || !(0 in boxedString); |
|
322 |
|
|
323 |
if (!Array.prototype.forEach) { |
|
324 |
Array.prototype.forEach = function forEach(fun /*, thisp*/) { |
|
325 |
var object = toObject(this), |
|
326 |
self = splitString && _toString(this) == "[object String]" ? |
|
327 |
this.split("") : |
|
328 |
object, |
|
329 |
thisp = arguments[1], |
|
330 |
i = -1, |
|
331 |
length = self.length >>> 0; |
|
332 |
|
|
333 |
// If no callback function or if callback is not a callable function |
|
334 |
if (_toString(fun) != "[object Function]") { |
|
335 |
throw new TypeError(); // TODO message |
|
336 |
} |
|
337 |
|
|
338 |
while (++i < length) { |
|
339 |
if (i in self) { |
|
340 |
// Invoke the callback function with call, passing arguments: |
|
341 |
// context, property value, property key, thisArg object |
|
342 |
// context |
|
343 |
fun.call(thisp, self[i], i, object); |
|
344 |
} |
|
345 |
} |
|
346 |
}; |
|
347 |
} |
|
348 |
|
|
349 |
// ES5 15.4.4.19 |
|
350 |
// http://es5.github.com/#x15.4.4.19 |
|
351 |
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/map |
|
352 |
if (!Array.prototype.map) { |
|
353 |
Array.prototype.map = function map(fun /*, thisp*/) { |
|
354 |
var object = toObject(this), |
|
355 |
self = splitString && _toString(this) == "[object String]" ? |
|
356 |
this.split("") : |
|
357 |
object, |
|
358 |
length = self.length >>> 0, |
|
359 |
result = Array(length), |
|
360 |
thisp = arguments[1]; |
|
361 |
|
|
362 |
// If no callback function or if callback is not a callable function |
|
363 |
if (_toString(fun) != "[object Function]") { |
|
364 |
throw new TypeError(fun + " is not a function"); |
|
365 |
} |
|
366 |
|
|
367 |
for (var i = 0; i < length; i++) { |
|
368 |
if (i in self) |
|
369 |
result[i] = fun.call(thisp, self[i], i, object); |
|
370 |
} |
|
371 |
return result; |
|
372 |
}; |
|
373 |
} |
|
374 |
|
|
375 |
// ES5 15.4.4.20 |
|
376 |
// http://es5.github.com/#x15.4.4.20 |
|
377 |
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/filter |
|
378 |
if (!Array.prototype.filter) { |
|
379 |
Array.prototype.filter = function filter(fun /*, thisp */) { |
|
380 |
var object = toObject(this), |
|
381 |
self = splitString && _toString(this) == "[object String]" ? |
|
382 |
this.split("") : |
|
383 |
object, |
|
384 |
length = self.length >>> 0, |
|
385 |
result = [], |
|
386 |
value, |
|
387 |
thisp = arguments[1]; |
|
388 |
|
|
389 |
// If no callback function or if callback is not a callable function |
|
390 |
if (_toString(fun) != "[object Function]") { |
|
391 |
throw new TypeError(fun + " is not a function"); |
|
392 |
} |
|
393 |
|
|
394 |
for (var i = 0; i < length; i++) { |
|
395 |
if (i in self) { |
|
396 |
value = self[i]; |
|
397 |
if (fun.call(thisp, value, i, object)) { |
|
398 |
result.push(value); |
|
399 |
} |
|
400 |
} |
|
401 |
} |
|
402 |
return result; |
|
403 |
}; |
|
404 |
} |
|
405 |
|
|
406 |
// ES5 15.4.4.16 |
|
407 |
// http://es5.github.com/#x15.4.4.16 |
|
408 |
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every |
|
409 |
if (!Array.prototype.every) { |
|
410 |
Array.prototype.every = function every(fun /*, thisp */) { |
|
411 |
var object = toObject(this), |
|
412 |
self = splitString && _toString(this) == "[object String]" ? |
|
413 |
this.split("") : |
|
414 |
object, |
|
415 |
length = self.length >>> 0, |
|
416 |
thisp = arguments[1]; |
|
417 |
|
|
418 |
// If no callback function or if callback is not a callable function |
|
419 |
if (_toString(fun) != "[object Function]") { |
|
420 |
throw new TypeError(fun + " is not a function"); |
|
421 |
} |
|
422 |
|
|
423 |
for (var i = 0; i < length; i++) { |
|
424 |
if (i in self && !fun.call(thisp, self[i], i, object)) { |
|
425 |
return false; |
|
426 |
} |
|
427 |
} |
|
428 |
return true; |
|
429 |
}; |
|
430 |
} |
|
431 |
|
|
432 |
// ES5 15.4.4.17 |
|
433 |
// http://es5.github.com/#x15.4.4.17 |
|
434 |
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some |
|
435 |
if (!Array.prototype.some) { |
|
436 |
Array.prototype.some = function some(fun /*, thisp */) { |
|
437 |
var object = toObject(this), |
|
438 |
self = splitString && _toString(this) == "[object String]" ? |
|
439 |
this.split("") : |
|
440 |
object, |
|
441 |
length = self.length >>> 0, |
|
442 |
thisp = arguments[1]; |
|
443 |
|
|
444 |
// If no callback function or if callback is not a callable function |
|
445 |
if (_toString(fun) != "[object Function]") { |
|
446 |
throw new TypeError(fun + " is not a function"); |
|
447 |
} |
|
448 |
|
|
449 |
for (var i = 0; i < length; i++) { |
|
450 |
if (i in self && fun.call(thisp, self[i], i, object)) { |
|
451 |
return true; |
|
452 |
} |
|
453 |
} |
|
454 |
return false; |
|
455 |
}; |
|
456 |
} |
|
457 |
|
|
458 |
// ES5 15.4.4.21 |
|
459 |
// http://es5.github.com/#x15.4.4.21 |
|
460 |
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduce |
|
461 |
if (!Array.prototype.reduce) { |
|
462 |
Array.prototype.reduce = function reduce(fun /*, initial*/) { |
|
463 |
var object = toObject(this), |
|
464 |
self = splitString && _toString(this) == "[object String]" ? |
|
465 |
this.split("") : |
|
466 |
object, |
|
467 |
length = self.length >>> 0; |
|
468 |
|
|
469 |
// If no callback function or if callback is not a callable function |
|
470 |
if (_toString(fun) != "[object Function]") { |
|
471 |
throw new TypeError(fun + " is not a function"); |
|
472 |
} |
|
473 |
|
|
474 |
// no value to return if no initial value and an empty array |
|
475 |
if (!length && arguments.length == 1) { |
|
476 |
throw new TypeError("reduce of empty array with no initial value"); |
|
477 |
} |
|
478 |
|
|
479 |
var i = 0; |
|
480 |
var result; |
|
481 |
if (arguments.length >= 2) { |
|
482 |
result = arguments[1]; |
|
483 |
} else { |
|
484 |
do { |
|
485 |
if (i in self) { |
|
486 |
result = self[i++]; |
|
487 |
break; |
|
488 |
} |
|
489 |
|
|
490 |
// if array contains no values, no initial value to return |
|
491 |
if (++i >= length) { |
|
492 |
throw new TypeError("reduce of empty array with no initial value"); |
|
493 |
} |
|
494 |
} while (true); |
|
495 |
} |
|
496 |
|
|
497 |
for (; i < length; i++) { |
|
498 |
if (i in self) { |
|
499 |
result = fun.call(void 0, result, self[i], i, object); |
|
500 |
} |
|
501 |
} |
|
502 |
|
|
503 |
return result; |
|
504 |
}; |
|
505 |
} |
|
506 |
|
|
507 |
// ES5 15.4.4.22 |
|
508 |
// http://es5.github.com/#x15.4.4.22 |
|
509 |
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduceRight |
|
510 |
if (!Array.prototype.reduceRight) { |
|
511 |
Array.prototype.reduceRight = function reduceRight(fun /*, initial*/) { |
|
512 |
var object = toObject(this), |
|
513 |
self = splitString && _toString(this) == "[object String]" ? |
|
514 |
this.split("") : |
|
515 |
object, |
|
516 |
length = self.length >>> 0; |
|
517 |
|
|
518 |
// If no callback function or if callback is not a callable function |
|
519 |
if (_toString(fun) != "[object Function]") { |
|
520 |
throw new TypeError(fun + " is not a function"); |
|
521 |
} |
|
522 |
|
|
523 |
// no value to return if no initial value, empty array |
|
524 |
if (!length && arguments.length == 1) { |
|
525 |
throw new TypeError("reduceRight of empty array with no initial value"); |
|
526 |
} |
|
527 |
|
|
528 |
var result, i = length - 1; |
|
529 |
if (arguments.length >= 2) { |
|
530 |
result = arguments[1]; |
|
531 |
} else { |
|
532 |
do { |
|
533 |
if (i in self) { |
|
534 |
result = self[i--]; |
|
535 |
break; |
|
536 |
} |
|
537 |
|
|
538 |
// if array contains no values, no initial value to return |
|
539 |
if (--i < 0) { |
|
540 |
throw new TypeError("reduceRight of empty array with no initial value"); |
|
541 |
} |
|
542 |
} while (true); |
|
543 |
} |
|
544 |
|
|
545 |
if (i < 0) { |
|
546 |
return result; |
|
547 |
} |
|
548 |
|
|
549 |
do { |
|
550 |
if (i in this) { |
|
551 |
result = fun.call(void 0, result, self[i], i, object); |
|
552 |
} |
|
553 |
} while (i--); |
|
554 |
|
|
555 |
return result; |
|
556 |
}; |
|
557 |
} |
|
558 |
|
|
559 |
// ES5 15.4.4.14 |
|
560 |
// http://es5.github.com/#x15.4.4.14 |
|
561 |
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf |
|
562 |
if (!Array.prototype.indexOf || ([0, 1].indexOf(1, 2) != -1)) { |
|
563 |
Array.prototype.indexOf = function indexOf(sought /*, fromIndex */ ) { |
|
564 |
var self = splitString && _toString(this) == "[object String]" ? |
|
565 |
this.split("") : |
|
566 |
toObject(this), |
|
567 |
length = self.length >>> 0; |
|
568 |
|
|
569 |
if (!length) { |
|
570 |
return -1; |
|
571 |
} |
|
572 |
|
|
573 |
var i = 0; |
|
574 |
if (arguments.length > 1) { |
|
575 |
i = toInteger(arguments[1]); |
|
576 |
} |
|
577 |
|
|
578 |
// handle negative indices |
|
579 |
i = i >= 0 ? i : Math.max(0, length + i); |
|
580 |
for (; i < length; i++) { |
|
581 |
if (i in self && self[i] === sought) { |
|
582 |
return i; |
|
583 |
} |
|
584 |
} |
|
585 |
return -1; |
|
586 |
}; |
|
587 |
} |
|
588 |
|
|
589 |
// ES5 15.4.4.15 |
|
590 |
// http://es5.github.com/#x15.4.4.15 |
|
591 |
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/lastIndexOf |
|
592 |
if (!Array.prototype.lastIndexOf || ([0, 1].lastIndexOf(0, -3) != -1)) { |
|
593 |
Array.prototype.lastIndexOf = function lastIndexOf(sought /*, fromIndex */) { |
|
594 |
var self = splitString && _toString(this) == "[object String]" ? |
|
595 |
this.split("") : |
|
596 |
toObject(this), |
|
597 |
length = self.length >>> 0; |
|
598 |
|
|
599 |
if (!length) { |
|
600 |
return -1; |
|
601 |
} |
|
602 |
var i = length - 1; |
|
603 |
if (arguments.length > 1) { |
|
604 |
i = Math.min(i, toInteger(arguments[1])); |
|
605 |
} |
|
606 |
// handle negative indices |
|
607 |
i = i >= 0 ? i : length - Math.abs(i); |
|
608 |
for (; i >= 0; i--) { |
|
609 |
if (i in self && sought === self[i]) { |
|
610 |
return i; |
|
611 |
} |
|
612 |
} |
|
613 |
return -1; |
|
614 |
}; |
|
615 |
} |
|
616 |
|
|
617 |
// |
|
618 |
// Object |
|
619 |
// ====== |
|
620 |
// |
|
621 |
|
|
622 |
// ES5 15.2.3.14 |
|
623 |
// http://es5.github.com/#x15.2.3.14 |
|
624 |
if (!Object.keys) { |
|
625 |
// http://whattheheadsaid.com/2010/10/a-safer-object-keys-compatibility-implementation |
|
626 |
var hasDontEnumBug = true, |
|
627 |
dontEnums = [ |
|
628 |
"toString", |
|
629 |
"toLocaleString", |
|
630 |
"valueOf", |
|
631 |
"hasOwnProperty", |
|
632 |
"isPrototypeOf", |
|
633 |
"propertyIsEnumerable", |
|
634 |
"constructor" |
|
635 |
], |
|
636 |
dontEnumsLength = dontEnums.length; |
|
637 |
|
|
638 |
for (var key in {"toString": null}) { |
|
639 |
hasDontEnumBug = false; |
|
640 |
} |
|
641 |
|
|
642 |
Object.keys = function keys(object) { |
|
643 |
|
|
644 |
if ( |
|
645 |
(typeof object != "object" && typeof object != "function") || |
|
646 |
object === null |
|
647 |
) { |
|
648 |
throw new TypeError("Object.keys called on a non-object"); |
|
649 |
} |
|
650 |
|
|
651 |
var keys = []; |
|
652 |
for (var name in object) { |
|
653 |
if (owns(object, name)) { |
|
654 |
keys.push(name); |
|
655 |
} |
|
656 |
} |
|
657 |
|
|
658 |
if (hasDontEnumBug) { |
|
659 |
for (var i = 0, ii = dontEnumsLength; i < ii; i++) { |
|
660 |
var dontEnum = dontEnums[i]; |
|
661 |
if (owns(object, dontEnum)) { |
|
662 |
keys.push(dontEnum); |
|
663 |
} |
|
664 |
} |
|
665 |
} |
|
666 |
return keys; |
|
667 |
}; |
|
668 |
|
|
669 |
} |
|
670 |
|
|
671 |
// |
|
672 |
// Date |
|
673 |
// ==== |
|
674 |
// |
|
675 |
|
|
676 |
// ES5 15.9.5.43 |
|
677 |
// http://es5.github.com/#x15.9.5.43 |
|
678 |
// This function returns a String value represent the instance in time |
|
679 |
// represented by this Date object. The format of the String is the Date Time |
|
680 |
// string format defined in 15.9.1.15. All fields are present in the String. |
|
681 |
// The time zone is always UTC, denoted by the suffix Z. If the time value of |
|
682 |
// this object is not a finite Number a RangeError exception is thrown. |
|
683 |
var negativeDate = -62198755200000, |
|
684 |
negativeYearString = "-000001"; |
|
685 |
if ( |
|
686 |
!Date.prototype.toISOString || |
|
687 |
(new Date(negativeDate).toISOString().indexOf(negativeYearString) === -1) |
|
688 |
) { |
|
689 |
Date.prototype.toISOString = function toISOString() { |
|
690 |
var result, length, value, year, month; |
|
691 |
if (!isFinite(this)) { |
|
692 |
throw new RangeError("Date.prototype.toISOString called on non-finite value."); |
|
693 |
} |
|
694 |
|
|
695 |
year = this.getUTCFullYear(); |
|
696 |
|
|
697 |
month = this.getUTCMonth(); |
|
698 |
// see https://github.com/kriskowal/es5-shim/issues/111 |
|
699 |
year += Math.floor(month / 12); |
|
700 |
month = (month % 12 + 12) % 12; |
|
701 |
|
|
702 |
// the date time string format is specified in 15.9.1.15. |
|
703 |
result = [month + 1, this.getUTCDate(), |
|
704 |
this.getUTCHours(), this.getUTCMinutes(), this.getUTCSeconds()]; |
|
705 |
year = ( |
|
706 |
(year < 0 ? "-" : (year > 9999 ? "+" : "")) + |
|
707 |
("00000" + Math.abs(year)) |
|
708 |
.slice(0 <= year && year <= 9999 ? -4 : -6) |
|
709 |
); |
|
710 |
|
|
711 |
length = result.length; |
|
712 |
while (length--) { |
|
713 |
value = result[length]; |
|
714 |
// pad months, days, hours, minutes, and seconds to have two |
|
715 |
// digits. |
|
716 |
if (value < 10) { |
|
717 |
result[length] = "0" + value; |
|
718 |
} |
|
719 |
} |
|
720 |
// pad milliseconds to have three digits. |
|
721 |
return ( |
|
722 |
year + "-" + result.slice(0, 2).join("-") + |
|
723 |
"T" + result.slice(2).join(":") + "." + |
|
724 |
("000" + this.getUTCMilliseconds()).slice(-3) + "Z" |
|
725 |
); |
|
726 |
}; |
|
727 |
} |
|
728 |
|
|
729 |
|
|
730 |
// ES5 15.9.5.44 |
|
731 |
// http://es5.github.com/#x15.9.5.44 |
|
732 |
// This function provides a String representation of a Date object for use by |
|
733 |
// JSON.stringify (15.12.3). |
|
734 |
var dateToJSONIsSupported = false; |
|
735 |
try { |
|
736 |
dateToJSONIsSupported = ( |
|
737 |
Date.prototype.toJSON && |
|
738 |
new Date(NaN).toJSON() === null && |
|
739 |
new Date(negativeDate).toJSON().indexOf(negativeYearString) !== -1 && |
|
740 |
Date.prototype.toJSON.call({ // generic |
|
741 |
toISOString: function () { |
|
742 |
return true; |
|
743 |
} |
|
744 |
}) |
|
745 |
); |
|
746 |
} catch (e) { |
|
747 |
} |
|
748 |
if (!dateToJSONIsSupported) { |
|
749 |
Date.prototype.toJSON = function toJSON(key) { |
|
750 |
// When the toJSON method is called with argument key, the following |
|
751 |
// steps are taken: |
|
752 |
|
|
753 |
// 1. Let O be the result of calling ToObject, giving it the this |
|
754 |
// value as its argument. |
|
755 |
// 2. Let tv be toPrimitive(O, hint Number). |
|
756 |
var o = Object(this), |
|
757 |
tv = toPrimitive(o), |
|
758 |
toISO; |
|
759 |
// 3. If tv is a Number and is not finite, return null. |
|
760 |
if (typeof tv === "number" && !isFinite(tv)) { |
|
761 |
return null; |
|
762 |
} |
|
763 |
// 4. Let toISO be the result of calling the [[Get]] internal method of |
|
764 |
// O with argument "toISOString". |
|
765 |
toISO = o.toISOString; |
|
766 |
// 5. If IsCallable(toISO) is false, throw a TypeError exception. |
|
767 |
if (typeof toISO != "function") { |
|
768 |
throw new TypeError("toISOString property is not callable"); |
|
769 |
} |
|
770 |
// 6. Return the result of calling the [[Call]] internal method of |
|
771 |
// toISO with O as the this value and an empty argument list. |
|
772 |
return toISO.call(o); |
|
773 |
|
|
774 |
// NOTE 1 The argument is ignored. |
|
775 |
|
|
776 |
// NOTE 2 The toJSON function is intentionally generic; it does not |
|
777 |
// require that its this value be a Date object. Therefore, it can be |
|
778 |
// transferred to other kinds of objects for use as a method. However, |
|
779 |
// it does require that any such object have a toISOString method. An |
|
780 |
// object is free to use the argument key to filter its |
|
781 |
// stringification. |
|
782 |
}; |
|
783 |
} |
|
784 |
|
|
785 |
// ES5 15.9.4.2 |
|
786 |
// http://es5.github.com/#x15.9.4.2 |
|
787 |
// based on work shared by Daniel Friesen (dantman) |
|
788 |
// http://gist.github.com/303249 |
|
789 |
if (!Date.parse || "Date.parse is buggy") { |
|
790 |
// XXX global assignment won't work in embeddings that use |
|
791 |
// an alternate object for the context. |
|
792 |
Date = (function(NativeDate) { |
|
793 |
|
|
794 |
// Date.length === 7 |
|
795 |
function Date(Y, M, D, h, m, s, ms) { |
|
796 |
var length = arguments.length; |
|
797 |
if (this instanceof NativeDate) { |
|
798 |
var date = length == 1 && String(Y) === Y ? // isString(Y) |
|
799 |
// We explicitly pass it through parse: |
|
800 |
new NativeDate(Date.parse(Y)) : |
|
801 |
// We have to manually make calls depending on argument |
|
802 |
// length here |
|
803 |
length >= 7 ? new NativeDate(Y, M, D, h, m, s, ms) : |
|
804 |
length >= 6 ? new NativeDate(Y, M, D, h, m, s) : |
|
805 |
length >= 5 ? new NativeDate(Y, M, D, h, m) : |
|
806 |
length >= 4 ? new NativeDate(Y, M, D, h) : |
|
807 |
length >= 3 ? new NativeDate(Y, M, D) : |
|
808 |
length >= 2 ? new NativeDate(Y, M) : |
|
809 |
length >= 1 ? new NativeDate(Y) : |
|
810 |
new NativeDate(); |
|
811 |
// Prevent mixups with unfixed Date object |
|
812 |
date.constructor = Date; |
|
813 |
return date; |
|
814 |
} |
|
815 |
return NativeDate.apply(this, arguments); |
|
816 |
}; |
|
817 |
|
|
818 |
// 15.9.1.15 Date Time String Format. |
|
819 |
var isoDateExpression = new RegExp("^" + |
|
820 |
"(\\d{4}|[\+\-]\\d{6})" + // four-digit year capture or sign + |
|
821 |
// 6-digit extended year |
|
822 |
"(?:-(\\d{2})" + // optional month capture |
|
823 |
"(?:-(\\d{2})" + // optional day capture |
|
824 |
"(?:" + // capture hours:minutes:seconds.milliseconds |
|
825 |
"T(\\d{2})" + // hours capture |
|
826 |
":(\\d{2})" + // minutes capture |
|
827 |
"(?:" + // optional :seconds.milliseconds |
|
828 |
":(\\d{2})" + // seconds capture |
|
829 |
"(?:(\\.\\d{1,}))?" + // milliseconds capture |
|
830 |
")?" + |
|
831 |
"(" + // capture UTC offset component |
|
832 |
"Z|" + // UTC capture |
|
833 |
"(?:" + // offset specifier +/-hours:minutes |
|
834 |
"([-+])" + // sign capture |
|
835 |
"(\\d{2})" + // hours offset capture |
|
836 |
":(\\d{2})" + // minutes offset capture |
|
837 |
")" + |
|
838 |
")?)?)?)?" + |
|
839 |
"$"); |
|
840 |
|
|
841 |
var months = [ |
|
842 |
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 |
|
843 |
]; |
|
844 |
|
|
845 |
function dayFromMonth(year, month) { |
|
846 |
var t = month > 1 ? 1 : 0; |
|
847 |
return ( |
|
848 |
months[month] + |
|
849 |
Math.floor((year - 1969 + t) / 4) - |
|
850 |
Math.floor((year - 1901 + t) / 100) + |
|
851 |
Math.floor((year - 1601 + t) / 400) + |
|
852 |
365 * (year - 1970) |
|
853 |
); |
|
854 |
} |
|
855 |
|
|
856 |
// Copy any custom methods a 3rd party library may have added |
|
857 |
for (var key in NativeDate) { |
|
858 |
Date[key] = NativeDate[key]; |
|
859 |
} |
|
860 |
|
|
861 |
// Copy "native" methods explicitly; they may be non-enumerable |
|
862 |
Date.now = NativeDate.now; |
|
863 |
Date.UTC = NativeDate.UTC; |
|
864 |
Date.prototype = NativeDate.prototype; |
|
865 |
Date.prototype.constructor = Date; |
|
866 |
|
|
867 |
// Upgrade Date.parse to handle simplified ISO 8601 strings |
|
868 |
Date.parse = function parse(string) { |
|
869 |
var match = isoDateExpression.exec(string); |
|
870 |
if (match) { |
|
871 |
// parse months, days, hours, minutes, seconds, and milliseconds |
|
872 |
// provide default values if necessary |
|
873 |
// parse the UTC offset component |
|
874 |
var year = Number(match[1]), |
|
875 |
month = Number(match[2] || 1) - 1, |
|
876 |
day = Number(match[3] || 1) - 1, |
|
877 |
hour = Number(match[4] || 0), |
|
878 |
minute = Number(match[5] || 0), |
|
879 |
second = Number(match[6] || 0), |
|
880 |
millisecond = Math.floor(Number(match[7] || 0) * 1000), |
|
881 |
// When time zone is missed, local offset should be used |
|
882 |
// (ES 5.1 bug) |
|
883 |
// see https://bugs.ecmascript.org/show_bug.cgi?id=112 |
|
884 |
offset = !match[4] || match[8] ? |
|
885 |
0 : Number(new NativeDate(1970, 0)), |
|
886 |
signOffset = match[9] === "-" ? 1 : -1, |
|
887 |
hourOffset = Number(match[10] || 0), |
|
888 |
minuteOffset = Number(match[11] || 0), |
|
889 |
result; |
|
890 |
if ( |
|
891 |
hour < ( |
|
892 |
minute > 0 || second > 0 || millisecond > 0 ? |
|
893 |
24 : 25 |
|
894 |
) && |
|
895 |
minute < 60 && second < 60 && millisecond < 1000 && |
|
896 |
month > -1 && month < 12 && hourOffset < 24 && |
|
897 |
minuteOffset < 60 && // detect invalid offsets |
|
898 |
day > -1 && |
|
899 |
day < ( |
|
900 |
dayFromMonth(year, month + 1) - |
|
901 |
dayFromMonth(year, month) |
|
902 |
) |
|
903 |
) { |
|
904 |
result = ( |
|
905 |
(dayFromMonth(year, month) + day) * 24 + |
|
906 |
hour + |
|
907 |
hourOffset * signOffset |
|
908 |
) * 60; |
|
909 |
result = ( |
|
910 |
(result + minute + minuteOffset * signOffset) * 60 + |
|
911 |
second |
|
912 |
) * 1000 + millisecond + offset; |
|
913 |
if (-8.64e15 <= result && result <= 8.64e15) { |
|
914 |
return result; |
|
915 |
} |
|
916 |
} |
|
917 |
return NaN; |
|
918 |
} |
|
919 |
return NativeDate.parse.apply(this, arguments); |
|
920 |
}; |
|
921 |
|
|
922 |
return Date; |
|
923 |
})(Date); |
|
924 |
} |
|
925 |
|
|
926 |
// ES5 15.9.4.4 |
|
927 |
// http://es5.github.com/#x15.9.4.4 |
|
928 |
if (!Date.now) { |
|
929 |
Date.now = function now() { |
|
930 |
return new Date().getTime(); |
|
931 |
}; |
|
932 |
} |
|
933 |
|
|
934 |
|
|
935 |
// |
|
936 |
// Number |
|
937 |
// ====== |
|
938 |
// |
|
939 |
|
|
940 |
// ES5.1 15.7.4.5 |
|
941 |
// http://es5.github.com/#x15.7.4.5 |
|
942 |
if (!Number.prototype.toFixed || (0.00008).toFixed(3) !== '0.000' || (0.9).toFixed(0) === '0' || (1.255).toFixed(2) !== '1.25' || (1000000000000000128).toFixed(0) !== "1000000000000000128") { |
|
943 |
// Hide these variables and functions |
|
944 |
(function () { |
|
945 |
var base, size, data, i; |
|
946 |
|
|
947 |
base = 1e7; |
|
948 |
size = 6; |
|
949 |
data = [0, 0, 0, 0, 0, 0]; |
|
950 |
|
|
951 |
function multiply(n, c) { |
|
952 |
var i = -1; |
|
953 |
while (++i < size) { |
|
954 |
c += n * data[i]; |
|
955 |
data[i] = c % base; |
|
956 |
c = Math.floor(c / base); |
|
957 |
} |
|
958 |
} |
|
959 |
|
|
960 |
function divide(n) { |
|
961 |
var i = size, c = 0; |
|
962 |
while (--i >= 0) { |
|
963 |
c += data[i]; |
|
964 |
data[i] = Math.floor(c / n); |
|
965 |
c = (c % n) * base; |
|
966 |
} |
|
967 |
} |
|
968 |
|
|
969 |
function toString() { |
|
970 |
var i = size; |
|
971 |
var s = ''; |
|
972 |
while (--i >= 0) { |
|
973 |
if (s !== '' || i === 0 || data[i] !== 0) { |
|
974 |
var t = String(data[i]); |
|
975 |
if (s === '') { |
|
976 |
s = t; |
|
977 |
} else { |
|
978 |
s += '0000000'.slice(0, 7 - t.length) + t; |
|
979 |
} |
|
980 |
} |
|
981 |
} |
|
982 |
return s; |
|
983 |
} |
|
984 |
|
|
985 |
function pow(x, n, acc) { |
|
986 |
return (n === 0 ? acc : (n % 2 === 1 ? pow(x, n - 1, acc * x) : pow(x * x, n / 2, acc))); |
|
987 |
} |
|
988 |
|
|
989 |
function log(x) { |
|
990 |
var n = 0; |
|
991 |
while (x >= 4096) { |
|
992 |
n += 12; |
|
993 |
x /= 4096; |
|
994 |
} |
|
995 |
while (x >= 2) { |
|
996 |
n += 1; |
|
997 |
x /= 2; |
|
998 |
} |
|
999 |
return n; |
|
1000 |
} |
|
1001 |
|
|
1002 |
Number.prototype.toFixed = function (fractionDigits) { |
|
1003 |
var f, x, s, m, e, z, j, k; |
|
1004 |
|
|
1005 |
// Test for NaN and round fractionDigits down |
|
1006 |
f = Number(fractionDigits); |
|
1007 |
f = f !== f ? 0 : Math.floor(f); |
|
1008 |
|
|
1009 |
if (f < 0 || f > 20) { |
|
1010 |
throw new RangeError("Number.toFixed called with invalid number of decimals"); |
|
1011 |
} |
|
1012 |
|
|
1013 |
x = Number(this); |
|
1014 |
|
|
1015 |
// Test for NaN |
|
1016 |
if (x !== x) { |
|
1017 |
return "NaN"; |
|
1018 |
} |
|
1019 |
|
|
1020 |
// If it is too big or small, return the string value of the number |
|
1021 |
if (x <= -1e21 || x >= 1e21) { |
|
1022 |
return String(x); |
|
1023 |
} |
|
1024 |
|
|
1025 |
s = ""; |
|
1026 |
|
|
1027 |
if (x < 0) { |
|
1028 |
s = "-"; |
|
1029 |
x = -x; |
|
1030 |
} |
|
1031 |
|
|
1032 |
m = "0"; |
|
1033 |
|
|
1034 |
if (x > 1e-21) { |
|
1035 |
// 1e-21 < x < 1e21 |
|
1036 |
// -70 < log2(x) < 70 |
|
1037 |
e = log(x * pow(2, 69, 1)) - 69; |
|
1038 |
z = (e < 0 ? x * pow(2, -e, 1) : x / pow(2, e, 1)); |
|
1039 |
z *= 0x10000000000000; // Math.pow(2, 52); |
|
1040 |
e = 52 - e; |
|
1041 |
|
|
1042 |
// -18 < e < 122 |
|
1043 |
// x = z / 2 ^ e |
|
1044 |
if (e > 0) { |
|
1045 |
multiply(0, z); |
|
1046 |
j = f; |
|
1047 |
|
|
1048 |
while (j >= 7) { |
|
1049 |
multiply(1e7, 0); |
|
1050 |
j -= 7; |
|
1051 |
} |
|
1052 |
|
|
1053 |
multiply(pow(10, j, 1), 0); |
|
1054 |
j = e - 1; |
|
1055 |
|
|
1056 |
while (j >= 23) { |
|
1057 |
divide(1 << 23); |
|
1058 |
j -= 23; |
|
1059 |
} |
|
1060 |
|
|
1061 |
divide(1 << j); |
|
1062 |
multiply(1, 1); |
|
1063 |
divide(2); |
|
1064 |
m = toString(); |
|
1065 |
} else { |
|
1066 |
multiply(0, z); |
|
1067 |
multiply(1 << (-e), 0); |
|
1068 |
m = toString() + '0.00000000000000000000'.slice(2, 2 + f); |
|
1069 |
} |
|
1070 |
} |
|
1071 |
|
|
1072 |
if (f > 0) { |
|
1073 |
k = m.length; |
|
1074 |
|
|
1075 |
if (k <= f) { |
|
1076 |
m = s + '0.0000000000000000000'.slice(0, f - k + 2) + m; |
|
1077 |
} else { |
|
1078 |
m = s + m.slice(0, k - f) + '.' + m.slice(k - f); |
|
1079 |
} |
|
1080 |
} else { |
|
1081 |
m = s + m; |
|
1082 |
} |
|
1083 |
|
|
1084 |
return m; |
|
1085 |
} |
|
1086 |
}()); |
|
1087 |
} |
|
1088 |
|
|
1089 |
|
|
1090 |
// |
|
1091 |
// String |
|
1092 |
// ====== |
|
1093 |
// |
|
1094 |
|
|
1095 |
|
|
1096 |
// ES5 15.5.4.14 |
|
1097 |
// http://es5.github.com/#x15.5.4.14 |
|
1098 |
|
|
1099 |
// [bugfix, IE lt 9, firefox 4, Konqueror, Opera, obscure browsers] |
|
1100 |
// Many browsers do not split properly with regular expressions or they |
|
1101 |
// do not perform the split correctly under obscure conditions. |
|
1102 |
// See http://blog.stevenlevithan.com/archives/cross-browser-split |
|
1103 |
// I've tested in many browsers and this seems to cover the deviant ones: |
|
1104 |
// 'ab'.split(/(?:ab)*/) should be ["", ""], not [""] |
|
1105 |
// '.'.split(/(.?)(.?)/) should be ["", ".", "", ""], not ["", ""] |
|
1106 |
// 'tesst'.split(/(s)*/) should be ["t", undefined, "e", "s", "t"], not |
|
1107 |
// [undefined, "t", undefined, "e", ...] |
|
1108 |
// ''.split(/.?/) should be [], not [""] |
|
1109 |
// '.'.split(/()()/) should be ["."], not ["", "", "."] |
|
1110 |
|
|
1111 |
var string_split = String.prototype.split; |
|
1112 |
if ( |
|
1113 |
'ab'.split(/(?:ab)*/).length !== 2 || |
|
1114 |
'.'.split(/(.?)(.?)/).length !== 4 || |
|
1115 |
'tesst'.split(/(s)*/)[1] === "t" || |
|
1116 |
''.split(/.?/).length === 0 || |
|
1117 |
'.'.split(/()()/).length > 1 |
|
1118 |
) { |
|
1119 |
(function () { |
|
1120 |
var compliantExecNpcg = /()??/.exec("")[1] === void 0; // NPCG: nonparticipating capturing group |
|
1121 |
|
|
1122 |
String.prototype.split = function (separator, limit) { |
|
1123 |
var string = this; |
|
1124 |
if (separator === void 0 && limit === 0) |
|
1125 |
return []; |
|
1126 |
|
|
1127 |
// If `separator` is not a regex, use native split |
|
1128 |
if (Object.prototype.toString.call(separator) !== "[object RegExp]") { |
|
1129 |
return string_split.apply(this, arguments); |
|
1130 |
} |
|
1131 |
|
|
1132 |
var output = [], |
|
1133 |
flags = (separator.ignoreCase ? "i" : "") + |
|
1134 |
(separator.multiline ? "m" : "") + |
|
1135 |
(separator.extended ? "x" : "") + // Proposed for ES6 |
|
1136 |
(separator.sticky ? "y" : ""), // Firefox 3+ |
|
1137 |
lastLastIndex = 0, |
|
1138 |
// Make `global` and avoid `lastIndex` issues by working with a copy |
|
1139 |
separator = new RegExp(separator.source, flags + "g"), |
|
1140 |
separator2, match, lastIndex, lastLength; |
|
1141 |
string += ""; // Type-convert |
|
1142 |
if (!compliantExecNpcg) { |
|
1143 |
// Doesn't need flags gy, but they don't hurt |
|
1144 |
separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags); |
|
1145 |
} |
|
1146 |
/* Values for `limit`, per the spec: |
|
1147 |
* If undefined: 4294967295 // Math.pow(2, 32) - 1 |
|
1148 |
* If 0, Infinity, or NaN: 0 |
|
1149 |
* If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296; |
|
1150 |
* If negative number: 4294967296 - Math.floor(Math.abs(limit)) |
|
1151 |
* If other: Type-convert, then use the above rules |
|
1152 |
*/ |
|
1153 |
limit = limit === void 0 ? |
|
1154 |
-1 >>> 0 : // Math.pow(2, 32) - 1 |
|
1155 |
limit >>> 0; // ToUint32(limit) |
|
1156 |
while (match = separator.exec(string)) { |
|
1157 |
// `separator.lastIndex` is not reliable cross-browser |
|
1158 |
lastIndex = match.index + match[0].length; |
|
1159 |
if (lastIndex > lastLastIndex) { |
|
1160 |
output.push(string.slice(lastLastIndex, match.index)); |
|
1161 |
// Fix browsers whose `exec` methods don't consistently return `undefined` for |
|
1162 |
// nonparticipating capturing groups |
|
1163 |
if (!compliantExecNpcg && match.length > 1) { |
|
1164 |
match[0].replace(separator2, function () { |
|
1165 |
for (var i = 1; i < arguments.length - 2; i++) { |
|
1166 |
if (arguments[i] === void 0) { |
|
1167 |
match[i] = void 0; |
|
1168 |
} |
|
1169 |
} |
|
1170 |
}); |
|
1171 |
} |
|
1172 |
if (match.length > 1 && match.index < string.length) { |
|
1173 |
Array.prototype.push.apply(output, match.slice(1)); |
|
1174 |
} |
|
1175 |
lastLength = match[0].length; |
|
1176 |
lastLastIndex = lastIndex; |
|
1177 |
if (output.length >= limit) { |
|
1178 |
break; |
|
1179 |
} |
|
1180 |
} |
|
1181 |
if (separator.lastIndex === match.index) { |
|
1182 |
separator.lastIndex++; // Avoid an infinite loop |
|
1183 |
} |
|
1184 |
} |
|
1185 |
if (lastLastIndex === string.length) { |
|
1186 |
if (lastLength || !separator.test("")) { |
|
1187 |
output.push(""); |
|
1188 |
} |
|
1189 |
} else { |
|
1190 |
output.push(string.slice(lastLastIndex)); |
|
1191 |
} |
|
1192 |
return output.length > limit ? output.slice(0, limit) : output; |
|
1193 |
}; |
|
1194 |
}()); |
|
1195 |
|
|
1196 |
// [bugfix, chrome] |
|
1197 |
// If separator is undefined, then the result array contains just one String, |
|
1198 |
// which is the this value (converted to a String). If limit is not undefined, |
|
1199 |
// then the output array is truncated so that it contains no more than limit |
|
1200 |
// elements. |
|
1201 |
// "0".split(undefined, 0) -> [] |
|
1202 |
} else if ("0".split(void 0, 0).length) { |
|
1203 |
String.prototype.split = function(separator, limit) { |
|
1204 |
if (separator === void 0 && limit === 0) return []; |
|
1205 |
return string_split.apply(this, arguments); |
|
1206 |
} |
|
1207 |
} |
|
1208 |
|
|
1209 |
|
|
1210 |
// ECMA-262, 3rd B.2.3 |
|
1211 |
// Note an ECMAScript standart, although ECMAScript 3rd Edition has a |
|
1212 |
// non-normative section suggesting uniform semantics and it should be |
|
1213 |
// normalized across all browsers |
|
1214 |
// [bugfix, IE lt 9] IE < 9 substr() with negative value not working in IE |
|
1215 |
if("".substr && "0b".substr(-1) !== "b") { |
|
1216 |
var string_substr = String.prototype.substr; |
|
1217 |
/** |
|
1218 |
* Get the substring of a string |
|
1219 |
* @param {integer} start where to start the substring |
|
1220 |
* @param {integer} length how many characters to return |
|
1221 |
* @return {string} |
|
1222 |
*/ |
|
1223 |
String.prototype.substr = function(start, length) { |
|
1224 |
return string_substr.call( |
|
1225 |
this, |
|
1226 |
start < 0 ? ((start = this.length + start) < 0 ? 0 : start) : start, |
|
1227 |
length |
|
1228 |
); |
|
1229 |
} |
|
1230 |
} |
|
1231 |
|
|
1232 |
// ES5 15.5.4.20 |
|
1233 |
// http://es5.github.com/#x15.5.4.20 |
|
1234 |
var ws = "\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003" + |
|
1235 |
"\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028" + |
|
1236 |
"\u2029\uFEFF"; |
|
1237 |
if (!String.prototype.trim || ws.trim()) { |
|
1238 |
// http://blog.stevenlevithan.com/archives/faster-trim-javascript |
|
1239 |
// http://perfectionkills.com/whitespace-deviations/ |
|
1240 |
ws = "[" + ws + "]"; |
|
1241 |
var trimBeginRegexp = new RegExp("^" + ws + ws + "*"), |
|
1242 |
trimEndRegexp = new RegExp(ws + ws + "*$"); |
|
1243 |
String.prototype.trim = function trim() { |
|
1244 |
if (this === void 0 || this === null) { |
|
1245 |
throw new TypeError("can't convert "+this+" to object"); |
|
1246 |
} |
|
1247 |
return String(this) |
|
1248 |
.replace(trimBeginRegexp, "") |
|
1249 |
.replace(trimEndRegexp, ""); |
|
1250 |
}; |
|
1251 |
} |
|
1252 |
|
|
1253 |
// |
|
1254 |
// Util |
|
1255 |
// ====== |
|
1256 |
// |
|
1257 |
|
|
1258 |
// ES5 9.4 |
|
1259 |
// http://es5.github.com/#x9.4 |
|
1260 |
// http://jsperf.com/to-integer |
|
1261 |
|
|
1262 |
function toInteger(n) { |
|
1263 |
n = +n; |
|
1264 |
if (n !== n) { // isNaN |
|
1265 |
n = 0; |
|
1266 |
} else if (n !== 0 && n !== (1/0) && n !== -(1/0)) { |
|
1267 |
n = (n > 0 || -1) * Math.floor(Math.abs(n)); |
|
1268 |
} |
|
1269 |
return n; |
|
1270 |
} |
|
1271 |
|
|
1272 |
function isPrimitive(input) { |
|
1273 |
var type = typeof input; |
|
1274 |
return ( |
|
1275 |
input === null || |
|
1276 |
type === "undefined" || |
|
1277 |
type === "boolean" || |
|
1278 |
type === "number" || |
|
1279 |
type === "string" |
|
1280 |
); |
|
1281 |
} |
|
1282 |
|
|
1283 |
function toPrimitive(input) { |
|
1284 |
var val, valueOf, toString; |
|
1285 |
if (isPrimitive(input)) { |
|
1286 |
return input; |
|
1287 |
} |
|
1288 |
valueOf = input.valueOf; |
|
1289 |
if (typeof valueOf === "function") { |
|
1290 |
val = valueOf.call(input); |
|
1291 |
if (isPrimitive(val)) { |
|
1292 |
return val; |
|
1293 |
} |
|
1294 |
} |
|
1295 |
toString = input.toString; |
|
1296 |
if (typeof toString === "function") { |
|
1297 |
val = toString.call(input); |
|
1298 |
if (isPrimitive(val)) { |
|
1299 |
return val; |
|
1300 |
} |
|
1301 |
} |
|
1302 |
throw new TypeError(); |
|
1303 |
} |
|
1304 |
|
|
1305 |
// ES5 9.9 |
|
1306 |
// http://es5.github.com/#x9.9 |
|
1307 |
var toObject = function (o) { |
|
1308 |
if (o == null) { // this matches both null and undefined |
|
1309 |
throw new TypeError("can't convert "+o+" to object"); |
|
1310 |
} |
|
1311 |
return Object(o); |
|
1312 |
}; |
|
1313 |
|
|
1314 |
}); |