2 * https://github.com/es-shims/es5-shim
3 * @license es5-shim Copyright 2009-2014 by contributors, MIT License
4 * see https://github.com/es-shims/es5-shim/blob/master/LICENSE
7 // vim: ts=4 sts=4 sw=4 expandtab
9 //Add semicolon to prevent IIFE from being passed as argument to concated code.
12 // UMD (Universal Module Definition)
13 // see https://github.com/umdjs/umd/blob/master/returnExports.js
14 (function (root
, factory
) {
15 if (typeof define
=== 'function' && define
.amd
) {
16 // AMD. Register as an anonymous module.
18 } else if (typeof exports
=== 'object') {
19 // Node. Does not work with strict CommonJS, but
20 // only CommonJS-like enviroments that support module.exports,
22 module
.exports
= factory();
24 // Browser globals (root is window)
25 root
.returnExports
= factory();
30 * Brings an environment as close to ECMAScript 5 compliance
31 * as is possible with the facilities of erstwhile engines.
33 * Annotated ES5: http://es5.github.com/ (specific links below)
34 * ES5 Spec: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
35 * Required reading: http://javascriptweblog.wordpress.com/2011/12/05/extending-javascript-natives/
38 // Shortcut to an often accessed properties, in order to avoid multiple
39 // dereference that costs universally.
40 var ArrayPrototype
= Array
.prototype;
41 var ObjectPrototype
= Object
.prototype;
42 var FunctionPrototype
= Function
.prototype;
43 var StringPrototype
= String
.prototype;
44 var NumberPrototype
= Number
.prototype;
45 var _Array_slice_
= ArrayPrototype
.slice
;
46 var array_splice
= ArrayPrototype
.splice
;
47 var array_push
= ArrayPrototype
.push
;
48 var array_unshift
= ArrayPrototype
.unshift
;
49 var call
= FunctionPrototype
.call
;
51 // Having a toString local variable name breaks in Opera so use _toString.
52 var _toString
= ObjectPrototype
.toString
;
54 var isFunction = function (val
) {
55 return ObjectPrototype
.toString
.call(val
) === '[object Function]';
57 var isRegex = function (val
) {
58 return ObjectPrototype
.toString
.call(val
) === '[object RegExp]';
60 var isArray
= function isArray(obj
) {
61 return _toString
.call(obj
) === "[object Array]";
63 var isString
= function isString(obj
) {
64 return _toString
.call(obj
) === "[object String]";
66 var isArguments
= function isArguments(value
) {
67 var str
= _toString
.call(value
);
68 var isArgs
= str
=== '[object Arguments]';
70 isArgs
= !isArray(str
)
72 && typeof value
=== 'object'
73 && typeof value
.length
=== 'number'
75 && isFunction(value
.callee
);
80 var supportsDescriptors
= Object
.defineProperty
&& (function () {
82 Object
.defineProperty({}, 'x', {});
84 } catch (e
) { /* this is ES3 */
89 // Define configurable, writable and non-enumerable props
90 // if they don't exist.
92 if (supportsDescriptors
) {
93 defineProperty = function (object
, name
, method
, forceAssign
) {
94 if (!forceAssign
&& (name
in object
)) { return; }
95 Object
.defineProperty(object
, name
, {
103 defineProperty = function (object
, name
, method
, forceAssign
) {
104 if (!forceAssign
&& (name
in object
)) { return; }
105 object
[name
] = method
;
108 var defineProperties = function (object
, map
, forceAssign
) {
109 for (var name
in map
) {
110 if (ObjectPrototype
.hasOwnProperty
.call(map
, name
)) {
111 defineProperty(object
, name
, map
[name
], forceAssign
);
122 // http://es5.github.com/#x9.4
123 // http://jsperf.com/to-integer
125 function toInteger(n
) {
127 if (n
!== n
) { // isNaN
129 } else if (n
!== 0 && n
!== (1 / 0) && n
!== -(1 / 0)) {
130 n
= (n
> 0 || -1) * Math
.floor(Math
.abs(n
));
135 function isPrimitive(input
) {
136 var type
= typeof input
;
139 type
=== "undefined" ||
140 type
=== "boolean" ||
146 function toPrimitive(input
) {
147 var val
, valueOf
, toStr
;
148 if (isPrimitive(input
)) {
151 valueOf
= input
.valueOf
;
152 if (isFunction(valueOf
)) {
153 val
= valueOf
.call(input
);
154 if (isPrimitive(val
)) {
158 toStr
= input
.toString
;
159 if (isFunction(toStr
)) {
160 val
= toStr
.call(input
);
161 if (isPrimitive(val
)) {
165 throw new TypeError();
169 // http://es5.github.com/#x9.9
170 var toObject = function (o
) {
171 if (o
== null) { // this matches both null and undefined
172 throw new TypeError("can't convert " + o
+ " to object");
177 var ToUint32
= function ToUint32(x
) {
187 // http://es5.github.com/#x15.3.4.5
191 defineProperties(FunctionPrototype
, {
192 bind
: function bind(that
) { // .length is 1
193 // 1. Let Target be the this value.
195 // 2. If IsCallable(Target) is false, throw a TypeError exception.
196 if (!isFunction(target
)) {
197 throw new TypeError("Function.prototype.bind called on incompatible " + target
);
199 // 3. Let A be a new (possibly empty) internal list of all of the
200 // argument values provided after thisArg (arg1, arg2 etc), in order.
201 // XXX slicedArgs will stand in for "A" if used
202 var args
= _Array_slice_
.call(arguments
, 1); // for normal call
203 // 4. Let F be a new native ECMAScript object.
204 // 11. Set the [[Prototype]] internal property of F to the standard
205 // built-in Function prototype object as specified in 15.3.3.1.
206 // 12. Set the [[Call]] internal property of F as described in
208 // 13. Set the [[Construct]] internal property of F as described in
210 // 14. Set the [[HasInstance]] internal property of F as described in
212 var binder = function () {
214 if (this instanceof bound
) {
215 // 15.3.4.5.2 [[Construct]]
216 // When the [[Construct]] internal method of a function object,
217 // F that was created using the bind function is called with a
218 // list of arguments ExtraArgs, the following steps are taken:
219 // 1. Let target be the value of F's [[TargetFunction]]
220 // internal property.
221 // 2. If target has no [[Construct]] internal method, a
222 // TypeError exception is thrown.
223 // 3. Let boundArgs be the value of F's [[BoundArgs]] internal
225 // 4. Let args be a new list containing the same values as the
226 // list boundArgs in the same order followed by the same
227 // values as the list ExtraArgs in the same order.
228 // 5. Return the result of calling the [[Construct]] internal
229 // method of target providing args as the arguments.
231 var result
= target
.apply(
233 args
.concat(_Array_slice_
.call(arguments
))
235 if (Object(result
) === result
) {
241 // 15.3.4.5.1 [[Call]]
242 // When the [[Call]] internal method of a function object, F,
243 // which was created using the bind function is called with a
244 // this value and a list of arguments ExtraArgs, the following
246 // 1. Let boundArgs be the value of F's [[BoundArgs]] internal
248 // 2. Let boundThis be the value of F's [[BoundThis]] internal
250 // 3. Let target be the value of F's [[TargetFunction]] internal
252 // 4. Let args be a new list containing the same values as the
253 // list boundArgs in the same order followed by the same
254 // values as the list ExtraArgs in the same order.
255 // 5. Return the result of calling the [[Call]] internal method
256 // of target providing boundThis as the this value and
257 // providing args as the arguments.
259 // equiv: target.call(this, ...boundArgs, ...args)
262 args
.concat(_Array_slice_
.call(arguments
))
269 // 15. If the [[Class]] internal property of Target is "Function", then
270 // a. Let L be the length property of Target minus the length of A.
271 // b. Set the length own property of F to either 0 or L, whichever is
273 // 16. Else set the length own property of F to 0.
275 var boundLength
= Math
.max(0, target
.length
- args
.length
);
277 // 17. Set the attributes of the length own property of F to the values
278 // specified in 15.3.5.1.
280 for (var i
= 0; i
< boundLength
; i
++) {
281 boundArgs
.push("$" + i
);
284 // XXX Build a dynamic function with desired amount of arguments is the only
285 // way to set the length property of a function.
286 // In environments where Content Security Policies enabled (Chrome extensions,
287 // for ex.) all use of eval or Function costructor throws an exception.
288 // However in all of these environments Function.prototype.bind exists
289 // and so this code will never be executed.
290 var bound
= Function("binder", "return function (" + boundArgs
.join(",") + "){return binder.apply(this,arguments)}")(binder
);
292 if (target
.prototype) {
293 Empty
.prototype = target
.prototype;
294 bound
.prototype = new Empty();
295 // Clean up dangling references.
296 Empty
.prototype = null;
300 // 18. Set the [[Extensible]] internal property of F to true.
303 // 19. Let thrower be the [[ThrowTypeError]] function Object (13.2.3).
304 // 20. Call the [[DefineOwnProperty]] internal method of F with
305 // arguments "caller", PropertyDescriptor {[[Get]]: thrower, [[Set]]:
306 // thrower, [[Enumerable]]: false, [[Configurable]]: false}, and
308 // 21. Call the [[DefineOwnProperty]] internal method of F with
309 // arguments "arguments", PropertyDescriptor {[[Get]]: thrower,
310 // [[Set]]: thrower, [[Enumerable]]: false, [[Configurable]]: false},
314 // NOTE Function objects created using Function.prototype.bind do not
315 // have a prototype property or the [[Code]], [[FormalParameters]], and
316 // [[Scope]] internal properties.
317 // XXX can't delete prototype in pure-js.
324 // _Please note: Shortcuts are defined after `Function.prototype.bind` as we
325 // us it in defining shortcuts.
326 var owns
= call
.bind(ObjectPrototype
.hasOwnProperty
);
328 // If JS engine supports accessors creating shortcuts.
333 var supportsAccessors
;
334 if ((supportsAccessors
= owns(ObjectPrototype
, "__defineGetter__"))) {
335 defineGetter
= call
.bind(ObjectPrototype
.__defineGetter__
);
336 defineSetter
= call
.bind(ObjectPrototype
.__defineSetter__
);
337 lookupGetter
= call
.bind(ObjectPrototype
.__lookupGetter__
);
338 lookupSetter
= call
.bind(ObjectPrototype
.__lookupSetter__
);
347 // http://es5.github.com/#x15.4.4.12
348 var spliceNoopReturnsEmptyArray
= (function () {
350 var result
= a
.splice();
351 return a
.length
=== 2 && isArray(result
) && result
.length
=== 0;
353 defineProperties(ArrayPrototype
, {
354 // Safari 5.0 bug where .splice() returns undefined
355 splice
: function splice(start
, deleteCount
) {
356 if (arguments
.length
=== 0) {
359 return array_splice
.apply(this, arguments
);
362 }, spliceNoopReturnsEmptyArray
);
364 var spliceWorksWithEmptyObject
= (function () {
366 ArrayPrototype
.splice
.call(obj
, 0, 0, 1);
367 return obj
.length
=== 1;
369 defineProperties(ArrayPrototype
, {
370 splice
: function splice(start
, deleteCount
) {
371 if (arguments
.length
=== 0) { return []; }
372 var args
= arguments
;
373 this.length
= Math
.max(toInteger(this.length
), 0);
374 if (arguments
.length
> 0 && typeof deleteCount
!== 'number') {
375 args
= _Array_slice_
.call(arguments
);
376 if (args
.length
< 2) {
377 args
.push(this.length
- start
);
379 args
[1] = toInteger(deleteCount
);
382 return array_splice
.apply(this, args
);
384 }, !spliceWorksWithEmptyObject
);
387 // http://es5.github.com/#x15.4.4.13
388 // Return len+argCount.
390 // IE < 8 bug: [].unshift(0) === undefined but should be "1"
391 var hasUnshiftReturnValueBug
= [].unshift(0) !== 1;
392 defineProperties(ArrayPrototype
, {
393 unshift: function () {
394 array_unshift
.apply(this, arguments
);
397 }, hasUnshiftReturnValueBug
);
400 // http://es5.github.com/#x15.4.3.2
401 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray
402 defineProperties(Array
, { isArray
: isArray
});
404 // The IsCallable() check in the Array functions
405 // has been replaced with a strict check on the
406 // internal class of the object to trap cases where
407 // the provided function was actually a regular
408 // expression literal, which in V8 and
409 // JavaScriptCore is a typeof "function". Only in
410 // V8 are regular expression literals permitted as
411 // reduce parameters, so it is desirable in the
412 // general case for the shim to match the more
413 // strict and common behavior of rejecting regular
417 // http://es5.github.com/#x15.4.4.18
418 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/forEach
420 // Check failure of by-index access of string characters (IE < 9)
421 // and failure of `0 in boxedString` (Rhino)
422 var boxedString
= Object("a");
423 var splitString
= boxedString
[0] !== "a" || !(0 in boxedString
);
425 var properlyBoxesContext
= function properlyBoxed(method
) {
426 // Check node 0.6.21 bug where third parameter is not boxed
427 var properlyBoxesNonStrict
= true;
428 var properlyBoxesStrict
= true;
430 method
.call('foo', function (_
, __
, context
) {
431 if (typeof context
!== 'object') { properlyBoxesNonStrict
= false; }
434 method
.call([1], function () {
436 properlyBoxesStrict
= typeof this === 'string';
439 return !!method
&& properlyBoxesNonStrict
&& properlyBoxesStrict
;
442 defineProperties(ArrayPrototype
, {
443 forEach
: function forEach(fun
/*, thisp*/) {
444 var object
= toObject(this),
445 self
= splitString
&& isString(this) ? this.split('') : object
,
446 thisp
= arguments
[1],
448 length
= self
.length
>>> 0;
450 // If no callback function or if callback is not a callable function
451 if (!isFunction(fun
)) {
452 throw new TypeError(); // TODO message
455 while (++i
< length
) {
457 // Invoke the callback function with call, passing arguments:
458 // context, property value, property key, thisArg object
460 fun
.call(thisp
, self
[i
], i
, object
);
464 }, !properlyBoxesContext(ArrayPrototype
.forEach
));
467 // http://es5.github.com/#x15.4.4.19
468 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/map
469 defineProperties(ArrayPrototype
, {
470 map
: function map(fun
/*, thisp*/) {
471 var object
= toObject(this),
472 self
= splitString
&& isString(this) ? this.split('') : object
,
473 length
= self
.length
>>> 0,
474 result
= Array(length
),
475 thisp
= arguments
[1];
477 // If no callback function or if callback is not a callable function
478 if (!isFunction(fun
)) {
479 throw new TypeError(fun
+ " is not a function");
482 for (var i
= 0; i
< length
; i
++) {
484 result
[i
] = fun
.call(thisp
, self
[i
], i
, object
);
489 }, !properlyBoxesContext(ArrayPrototype
.map
));
492 // http://es5.github.com/#x15.4.4.20
493 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/filter
494 defineProperties(ArrayPrototype
, {
495 filter
: function filter(fun
/*, thisp */) {
496 var object
= toObject(this),
497 self
= splitString
&& isString(this) ? this.split('') : object
,
498 length
= self
.length
>>> 0,
501 thisp
= arguments
[1];
503 // If no callback function or if callback is not a callable function
504 if (!isFunction(fun
)) {
505 throw new TypeError(fun
+ " is not a function");
508 for (var i
= 0; i
< length
; i
++) {
511 if (fun
.call(thisp
, value
, i
, object
)) {
518 }, !properlyBoxesContext(ArrayPrototype
.filter
));
521 // http://es5.github.com/#x15.4.4.16
522 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every
523 defineProperties(ArrayPrototype
, {
524 every
: function every(fun
/*, thisp */) {
525 var object
= toObject(this),
526 self
= splitString
&& isString(this) ? this.split('') : object
,
527 length
= self
.length
>>> 0,
528 thisp
= arguments
[1];
530 // If no callback function or if callback is not a callable function
531 if (!isFunction(fun
)) {
532 throw new TypeError(fun
+ " is not a function");
535 for (var i
= 0; i
< length
; i
++) {
536 if (i
in self
&& !fun
.call(thisp
, self
[i
], i
, object
)) {
542 }, !properlyBoxesContext(ArrayPrototype
.every
));
545 // http://es5.github.com/#x15.4.4.17
546 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some
547 defineProperties(ArrayPrototype
, {
548 some
: function some(fun
/*, thisp */) {
549 var object
= toObject(this),
550 self
= splitString
&& isString(this) ? this.split('') : object
,
551 length
= self
.length
>>> 0,
552 thisp
= arguments
[1];
554 // If no callback function or if callback is not a callable function
555 if (!isFunction(fun
)) {
556 throw new TypeError(fun
+ " is not a function");
559 for (var i
= 0; i
< length
; i
++) {
560 if (i
in self
&& fun
.call(thisp
, self
[i
], i
, object
)) {
566 }, !properlyBoxesContext(ArrayPrototype
.some
));
569 // http://es5.github.com/#x15.4.4.21
570 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduce
571 var reduceCoercesToObject
= false;
572 if (ArrayPrototype
.reduce
) {
573 reduceCoercesToObject
= typeof ArrayPrototype
.reduce
.call('es5', function (_
, __
, ___
, list
) { return list
; }) === 'object';
575 defineProperties(ArrayPrototype
, {
576 reduce
: function reduce(fun
/*, initial*/) {
577 var object
= toObject(this),
578 self
= splitString
&& isString(this) ? this.split('') : object
,
579 length
= self
.length
>>> 0;
581 // If no callback function or if callback is not a callable function
582 if (!isFunction(fun
)) {
583 throw new TypeError(fun
+ " is not a function");
586 // no value to return if no initial value and an empty array
587 if (!length
&& arguments
.length
=== 1) {
588 throw new TypeError("reduce of empty array with no initial value");
593 if (arguments
.length
>= 2) {
594 result
= arguments
[1];
602 // if array contains no values, no initial value to return
604 throw new TypeError("reduce of empty array with no initial value");
609 for (; i
< length
; i
++) {
611 result
= fun
.call(void 0, result
, self
[i
], i
, object
);
617 }, !reduceCoercesToObject
);
620 // http://es5.github.com/#x15.4.4.22
621 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduceRight
622 var reduceRightCoercesToObject
= false;
623 if (ArrayPrototype
.reduceRight
) {
624 reduceRightCoercesToObject
= typeof ArrayPrototype
.reduceRight
.call('es5', function (_
, __
, ___
, list
) { return list
; }) === 'object';
626 defineProperties(ArrayPrototype
, {
627 reduceRight
: function reduceRight(fun
/*, initial*/) {
628 var object
= toObject(this),
629 self
= splitString
&& isString(this) ? this.split('') : object
,
630 length
= self
.length
>>> 0;
632 // If no callback function or if callback is not a callable function
633 if (!isFunction(fun
)) {
634 throw new TypeError(fun
+ " is not a function");
637 // no value to return if no initial value, empty array
638 if (!length
&& arguments
.length
=== 1) {
639 throw new TypeError("reduceRight of empty array with no initial value");
642 var result
, i
= length
- 1;
643 if (arguments
.length
>= 2) {
644 result
= arguments
[1];
652 // if array contains no values, no initial value to return
654 throw new TypeError("reduceRight of empty array with no initial value");
665 result
= fun
.call(void 0, result
, self
[i
], i
, object
);
671 }, !reduceRightCoercesToObject
);
674 // http://es5.github.com/#x15.4.4.14
675 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
676 var hasFirefox2IndexOfBug
= Array
.prototype.indexOf
&& [0, 1].indexOf(1, 2) !== -1;
677 defineProperties(ArrayPrototype
, {
678 indexOf
: function indexOf(sought
/*, fromIndex */ ) {
679 var self
= splitString
&& isString(this) ? this.split('') : toObject(this),
680 length
= self
.length
>>> 0;
687 if (arguments
.length
> 1) {
688 i
= toInteger(arguments
[1]);
691 // handle negative indices
692 i
= i
>= 0 ? i
: Math
.max(0, length
+ i
);
693 for (; i
< length
; i
++) {
694 if (i
in self
&& self
[i
] === sought
) {
700 }, hasFirefox2IndexOfBug
);
703 // http://es5.github.com/#x15.4.4.15
704 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/lastIndexOf
705 var hasFirefox2LastIndexOfBug
= Array
.prototype.lastIndexOf
&& [0, 1].lastIndexOf(0, -3) !== -1;
706 defineProperties(ArrayPrototype
, {
707 lastIndexOf
: function lastIndexOf(sought
/*, fromIndex */) {
708 var self
= splitString
&& isString(this) ? this.split('') : toObject(this),
709 length
= self
.length
>>> 0;
715 if (arguments
.length
> 1) {
716 i
= Math
.min(i
, toInteger(arguments
[1]));
718 // handle negative indices
719 i
= i
>= 0 ? i
: length
- Math
.abs(i
);
720 for (; i
>= 0; i
--) {
721 if (i
in self
&& sought
=== self
[i
]) {
727 }, hasFirefox2LastIndexOfBug
);
735 // http://es5.github.com/#x15.2.3.14
737 // http://whattheheadsaid.com/2010/10/a-safer-object-keys-compatibility-implementation
738 var hasDontEnumBug
= !({'toString': null}).propertyIsEnumerable('toString'),
739 hasProtoEnumBug
= (function () {}).propertyIsEnumerable('prototype'),
746 "propertyIsEnumerable",
749 dontEnumsLength
= dontEnums
.length
;
751 defineProperties(Object
, {
752 keys
: function keys(object
) {
753 var isFn
= isFunction(object
),
754 isArgs
= isArguments(object
),
755 isObject
= object
!== null && typeof object
=== 'object',
756 isStr
= isObject
&& isString(object
);
758 if (!isObject
&& !isFn
&& !isArgs
) {
759 throw new TypeError("Object.keys called on a non-object");
763 var skipProto
= hasProtoEnumBug
&& isFn
;
764 if (isStr
|| isArgs
) {
765 for (var i
= 0; i
< object
.length
; ++i
) {
766 theKeys
.push(String(i
));
769 for (var name
in object
) {
770 if (!(skipProto
&& name
=== 'prototype') && owns(object
, name
)) {
771 theKeys
.push(String(name
));
776 if (hasDontEnumBug
) {
777 var ctor
= object
.constructor,
778 skipConstructor
= ctor
&& ctor
.prototype === object
;
779 for (var j
= 0; j
< dontEnumsLength
; j
++) {
780 var dontEnum
= dontEnums
[j
];
781 if (!(skipConstructor
&& dontEnum
=== 'constructor') && owns(object
, dontEnum
)) {
782 theKeys
.push(dontEnum
);
790 var keysWorksWithArguments
= Object
.keys
&& (function () {
792 return Object
.keys(arguments
).length
=== 2;
794 var originalKeys
= Object
.keys
;
795 defineProperties(Object
, {
796 keys
: function keys(object
) {
797 if (isArguments(object
)) {
798 return originalKeys(ArrayPrototype
.slice
.call(object
));
800 return originalKeys(object
);
803 }, !keysWorksWithArguments
);
811 // http://es5.github.com/#x15.9.5.43
812 // This function returns a String value represent the instance in time
813 // represented by this Date object. The format of the String is the Date Time
814 // string format defined in 15.9.1.15. All fields are present in the String.
815 // The time zone is always UTC, denoted by the suffix Z. If the time value of
816 // this object is not a finite Number a RangeError exception is thrown.
817 var negativeDate
= -62198755200000;
818 var negativeYearString
= "-000001";
819 var hasNegativeDateBug
= Date
.prototype.toISOString
&& new Date(negativeDate
).toISOString().indexOf(negativeYearString
) === -1;
821 defineProperties(Date
.prototype, {
822 toISOString
: function toISOString() {
823 var result
, length
, value
, year
, month
;
824 if (!isFinite(this)) {
825 throw new RangeError("Date.prototype.toISOString called on non-finite value.");
828 year
= this.getUTCFullYear();
830 month
= this.getUTCMonth();
831 // see https://github.com/es-shims/es5-shim/issues/111
832 year
+= Math
.floor(month
/ 12);
833 month
= (month
% 12 + 12) % 12;
835 // the date time string format is specified in 15.9.1.15.
836 result
= [month
+ 1, this.getUTCDate(), this.getUTCHours(), this.getUTCMinutes(), this.getUTCSeconds()];
838 (year
< 0 ? "-" : (year
> 9999 ? "+" : "")) +
839 ("00000" + Math
.abs(year
)).slice(0 <= year
&& year
<= 9999 ? -4 : -6)
842 length
= result
.length
;
844 value
= result
[length
];
845 // pad months, days, hours, minutes, and seconds to have two
848 result
[length
] = "0" + value
;
851 // pad milliseconds to have three digits.
853 year
+ "-" + result
.slice(0, 2).join("-") +
854 "T" + result
.slice(2).join(":") + "." +
855 ("000" + this.getUTCMilliseconds()).slice(-3) + "Z"
858 }, hasNegativeDateBug
);
862 // http://es5.github.com/#x15.9.5.44
863 // This function provides a String representation of a Date object for use by
864 // JSON.stringify (15.12.3).
865 var dateToJSONIsSupported
= false;
867 dateToJSONIsSupported
= (
868 Date
.prototype.toJSON
&&
869 new Date(NaN
).toJSON() === null &&
870 new Date(negativeDate
).toJSON().indexOf(negativeYearString
) !== -1 &&
871 Date
.prototype.toJSON
.call({ // generic
872 toISOString: function () {
879 if (!dateToJSONIsSupported
) {
880 Date
.prototype.toJSON
= function toJSON(key
) {
881 // When the toJSON method is called with argument key, the following
884 // 1. Let O be the result of calling ToObject, giving it the this
885 // value as its argument.
886 // 2. Let tv be toPrimitive(O, hint Number).
887 var o
= Object(this),
890 // 3. If tv is a Number and is not finite, return null.
891 if (typeof tv
=== "number" && !isFinite(tv
)) {
894 // 4. Let toISO be the result of calling the [[Get]] internal method of
895 // O with argument "toISOString".
896 toISO
= o
.toISOString
;
897 // 5. If IsCallable(toISO) is false, throw a TypeError exception.
898 if (typeof toISO
!== "function") {
899 throw new TypeError("toISOString property is not callable");
901 // 6. Return the result of calling the [[Call]] internal method of
902 // toISO with O as the this value and an empty argument list.
903 return toISO
.call(o
);
905 // NOTE 1 The argument is ignored.
907 // NOTE 2 The toJSON function is intentionally generic; it does not
908 // require that its this value be a Date object. Therefore, it can be
909 // transferred to other kinds of objects for use as a method. However,
910 // it does require that any such object have a toISOString method. An
911 // object is free to use the argument key to filter its
917 // http://es5.github.com/#x15.9.4.2
918 // based on work shared by Daniel Friesen (dantman)
919 // http://gist.github.com/303249
920 var supportsExtendedYears
= Date
.parse('+033658-09-27T01:46:40.000Z') === 1e15
;
921 var acceptsInvalidDates
= !isNaN(Date
.parse('2012-04-04T24:00:00.500Z')) || !isNaN(Date
.parse('2012-11-31T23:59:59.000Z'));
922 var doesNotParseY2KNewYear
= isNaN(Date
.parse("2000-01-01T00:00:00.000Z"));
923 if (!Date
.parse
|| doesNotParseY2KNewYear
|| acceptsInvalidDates
|| !supportsExtendedYears
) {
924 // XXX global assignment won't work in embeddings that use
925 // an alternate object for the context.
926 Date
= (function (NativeDate
) {
929 function Date(Y
, M
, D
, h
, m
, s
, ms
) {
930 var length
= arguments
.length
;
931 if (this instanceof NativeDate
) {
932 var date
= length
=== 1 && String(Y
) === Y
? // isString(Y)
933 // We explicitly pass it through parse:
934 new NativeDate(Date
.parse(Y
)) :
935 // We have to manually make calls depending on argument
937 length
>= 7 ? new NativeDate(Y
, M
, D
, h
, m
, s
, ms
) :
938 length
>= 6 ? new NativeDate(Y
, M
, D
, h
, m
, s
) :
939 length
>= 5 ? new NativeDate(Y
, M
, D
, h
, m
) :
940 length
>= 4 ? new NativeDate(Y
, M
, D
, h
) :
941 length
>= 3 ? new NativeDate(Y
, M
, D
) :
942 length
>= 2 ? new NativeDate(Y
, M
) :
943 length
>= 1 ? new NativeDate(Y
) :
945 // Prevent mixups with unfixed Date object
946 date
.constructor = Date
;
949 return NativeDate
.apply(this, arguments
);
952 // 15.9.1.15 Date Time String Format.
953 var isoDateExpression
= new RegExp("^" +
954 "(\\d{4}|[\+\-]\\d{6})" + // four-digit year capture or sign +
955 // 6-digit extended year
956 "(?:-(\\d{2})" + // optional month capture
957 "(?:-(\\d{2})" + // optional day capture
958 "(?:" + // capture hours:minutes:seconds.milliseconds
959 "T(\\d{2})" + // hours capture
960 ":(\\d{2})" + // minutes capture
961 "(?:" + // optional :seconds.milliseconds
962 ":(\\d{2})" + // seconds capture
963 "(?:(\\.\\d{1,}))?" + // milliseconds capture
965 "(" + // capture UTC offset component
966 "Z|" + // UTC capture
967 "(?:" + // offset specifier +/-hours:minutes
968 "([-+])" + // sign capture
969 "(\\d{2})" + // hours offset capture
970 ":(\\d{2})" + // minutes offset capture
976 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
979 function dayFromMonth(year
, month
) {
980 var t
= month
> 1 ? 1 : 0;
983 Math
.floor((year
- 1969 + t
) / 4) -
984 Math
.floor((year
- 1901 + t
) / 100) +
985 Math
.floor((year
- 1601 + t
) / 400) +
991 return Number(new NativeDate(1970, 0, 1, 0, 0, 0, t
));
994 // Copy any custom methods a 3rd party library may have added
995 for (var key
in NativeDate
) {
996 Date
[key
] = NativeDate
[key
];
999 // Copy "native" methods explicitly; they may be non-enumerable
1000 Date
.now
= NativeDate
.now
;
1001 Date
.UTC
= NativeDate
.UTC
;
1002 Date
.prototype = NativeDate
.prototype;
1003 Date
.prototype.constructor = Date
;
1005 // Upgrade Date.parse to handle simplified ISO 8601 strings
1006 Date
.parse
= function parse(string
) {
1007 var match
= isoDateExpression
.exec(string
);
1009 // parse months, days, hours, minutes, seconds, and milliseconds
1010 // provide default values if necessary
1011 // parse the UTC offset component
1012 var year
= Number(match
[1]),
1013 month
= Number(match
[2] || 1) - 1,
1014 day
= Number(match
[3] || 1) - 1,
1015 hour
= Number(match
[4] || 0),
1016 minute
= Number(match
[5] || 0),
1017 second
= Number(match
[6] || 0),
1018 millisecond
= Math
.floor(Number(match
[7] || 0) * 1000),
1019 // When time zone is missed, local offset should be used
1021 // see https://bugs.ecmascript.org/show_bug.cgi?id=112
1022 isLocalTime
= Boolean(match
[4] && !match
[8]),
1023 signOffset
= match
[9] === "-" ? 1 : -1,
1024 hourOffset
= Number(match
[10] || 0),
1025 minuteOffset
= Number(match
[11] || 0),
1029 minute
> 0 || second
> 0 || millisecond
> 0 ?
1032 minute
< 60 && second
< 60 && millisecond
< 1000 &&
1033 month
> -1 && month
< 12 && hourOffset
< 24 &&
1034 minuteOffset
< 60 && // detect invalid offsets
1037 dayFromMonth(year
, month
+ 1) -
1038 dayFromMonth(year
, month
)
1042 (dayFromMonth(year
, month
) + day
) * 24 +
1044 hourOffset
* signOffset
1047 (result
+ minute
+ minuteOffset
* signOffset
) * 60 +
1049 ) * 1000 + millisecond
;
1051 result
= toUTC(result
);
1053 if (-8.64e15
<= result
&& result
<= 8.64e15
) {
1059 return NativeDate
.parse
.apply(this, arguments
);
1067 // http://es5.github.com/#x15.9.4.4
1069 Date
.now
= function now() {
1070 return new Date().getTime();
1081 // http://es5.github.com/#x15.7.4.5
1082 var hasToFixedBugs
= NumberPrototype
.toFixed
&& (
1083 (0.00008).toFixed(3) !== '0.000'
1084 || (0.9).toFixed(0) !== '1'
1085 || (1.255).toFixed(2) !== '1.25'
1086 || (1000000000000000128).toFixed(0) !== "1000000000000000128"
1089 var toFixedHelpers
= {
1092 data
: [0, 0, 0, 0, 0, 0],
1093 multiply
: function multiply(n
, c
) {
1095 while (++i
< toFixedHelpers
.size
) {
1096 c
+= n
* toFixedHelpers
.data
[i
];
1097 toFixedHelpers
.data
[i
] = c
% toFixedHelpers
.base
;
1098 c
= Math
.floor(c
/ toFixedHelpers
.base
);
1101 divide
: function divide(n
) {
1102 var i
= toFixedHelpers
.size
, c
= 0;
1104 c
+= toFixedHelpers
.data
[i
];
1105 toFixedHelpers
.data
[i
] = Math
.floor(c
/ n
);
1106 c
= (c
% n
) * toFixedHelpers
.base
;
1109 numToString
: function numToString() {
1110 var i
= toFixedHelpers
.size
;
1113 if (s
!== '' || i
=== 0 || toFixedHelpers
.data
[i
] !== 0) {
1114 var t
= String(toFixedHelpers
.data
[i
]);
1118 s
+= '0000000'.slice(0, 7 - t
.length
) + t
;
1124 pow
: function pow(x
, n
, acc
) {
1125 return (n
=== 0 ? acc
: (n
% 2 === 1 ? pow(x
, n
- 1, acc
* x
) : pow(x
* x
, n
/ 2, acc
)));
1127 log
: function log(x
) {
1141 defineProperties(NumberPrototype
, {
1142 toFixed
: function toFixed(fractionDigits
) {
1143 var f
, x
, s
, m
, e
, z
, j
, k
;
1145 // Test for NaN and round fractionDigits down
1146 f
= Number(fractionDigits
);
1147 f
= f
!== f
? 0 : Math
.floor(f
);
1149 if (f
< 0 || f
> 20) {
1150 throw new RangeError("Number.toFixed called with invalid number of decimals");
1160 // If it is too big or small, return the string value of the number
1161 if (x
<= -1e21
|| x
>= 1e21
) {
1176 // -70 < log2(x) < 70
1177 e
= toFixedHelpers
.log(x
* toFixedHelpers
.pow(2, 69, 1)) - 69;
1178 z
= (e
< 0 ? x
* toFixedHelpers
.pow(2, -e
, 1) : x
/ toFixedHelpers
.pow(2, e
, 1));
1179 z
*= 0x10000000000000; // Math.pow(2, 52);
1185 toFixedHelpers
.multiply(0, z
);
1189 toFixedHelpers
.multiply(1e7
, 0);
1193 toFixedHelpers
.multiply(toFixedHelpers
.pow(10, j
, 1), 0);
1197 toFixedHelpers
.divide(1 << 23);
1201 toFixedHelpers
.divide(1 << j
);
1202 toFixedHelpers
.multiply(1, 1);
1203 toFixedHelpers
.divide(2);
1204 m
= toFixedHelpers
.numToString();
1206 toFixedHelpers
.multiply(0, z
);
1207 toFixedHelpers
.multiply(1 << (-e
), 0);
1208 m
= toFixedHelpers
.numToString() + '0.00000000000000000000'.slice(2, 2 + f
);
1216 m
= s
+ '0.0000000000000000000'.slice(0, f
- k
+ 2) + m
;
1218 m
= s
+ m
.slice(0, k
- f
) + '.' + m
.slice(k
- f
);
1235 // http://es5.github.com/#x15.5.4.14
1237 // [bugfix, IE lt 9, firefox 4, Konqueror, Opera, obscure browsers]
1238 // Many browsers do not split properly with regular expressions or they
1239 // do not perform the split correctly under obscure conditions.
1240 // See http://blog.stevenlevithan.com/archives/cross-browser-split
1241 // I've tested in many browsers and this seems to cover the deviant ones:
1242 // 'ab'.split(/(?:ab)*/) should be ["", ""], not [""]
1243 // '.'.split(/(.?)(.?)/) should be ["", ".", "", ""], not ["", ""]
1244 // 'tesst'.split(/(s)*/) should be ["t", undefined, "e", "s", "t"], not
1245 // [undefined, "t", undefined, "e", ...]
1246 // ''.split(/.?/) should be [], not [""]
1247 // '.'.split(/()()/) should be ["."], not ["", "", "."]
1249 var string_split
= StringPrototype
.split
;
1251 'ab'.split(/(?:ab)*/).length
!== 2 ||
1252 '.'.split(/(.?)(.?)/).length
!== 4 ||
1253 'tesst'.split(/(s)*/)[1] === "t" ||
1254 'test'.split(/(?:)/, -1).length
!== 4 ||
1255 ''.split(/.?/).length
||
1256 '.'.split(/()()/).length
> 1
1259 var compliantExecNpcg
= /()??/.exec("")[1] === void 0; // NPCG: nonparticipating capturing group
1261 StringPrototype
.split = function (separator
, limit
) {
1263 if (separator
=== void 0 && limit
=== 0) {
1267 // If `separator` is not a regex, use native split
1268 if (_toString
.call(separator
) !== "[object RegExp]") {
1269 return string_split
.call(this, separator
, limit
);
1273 flags
= (separator
.ignoreCase
? "i" : "") +
1274 (separator
.multiline
? "m" : "") +
1275 (separator
.extended
? "x" : "") + // Proposed for ES6
1276 (separator
.sticky
? "y" : ""), // Firefox 3+
1278 // Make `global` and avoid `lastIndex` issues by working with a copy
1279 separator2
, match
, lastIndex
, lastLength
;
1280 separator
= new RegExp(separator
.source
, flags
+ "g");
1281 string
+= ""; // Type-convert
1282 if (!compliantExecNpcg
) {
1283 // Doesn't need flags gy, but they don't hurt
1284 separator2
= new RegExp("^" + separator
.source
+ "$(?!\\s)", flags
);
1286 /* Values for `limit`, per the spec:
1287 * If undefined: 4294967295 // Math.pow(2, 32) - 1
1288 * If 0, Infinity, or NaN: 0
1289 * If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296;
1290 * If negative number: 4294967296 - Math.floor(Math.abs(limit))
1291 * If other: Type-convert, then use the above rules
1293 limit
= limit
=== void 0 ?
1294 -1 >>> 0 : // Math.pow(2, 32) - 1
1296 while (match
= separator
.exec(string
)) {
1297 // `separator.lastIndex` is not reliable cross-browser
1298 lastIndex
= match
.index
+ match
[0].length
;
1299 if (lastIndex
> lastLastIndex
) {
1300 output
.push(string
.slice(lastLastIndex
, match
.index
));
1301 // Fix browsers whose `exec` methods don't consistently return `undefined` for
1302 // nonparticipating capturing groups
1303 if (!compliantExecNpcg
&& match
.length
> 1) {
1304 match
[0].replace(separator2
, function () {
1305 for (var i
= 1; i
< arguments
.length
- 2; i
++) {
1306 if (arguments
[i
] === void 0) {
1312 if (match
.length
> 1 && match
.index
< string
.length
) {
1313 ArrayPrototype
.push
.apply(output
, match
.slice(1));
1315 lastLength
= match
[0].length
;
1316 lastLastIndex
= lastIndex
;
1317 if (output
.length
>= limit
) {
1321 if (separator
.lastIndex
=== match
.index
) {
1322 separator
.lastIndex
++; // Avoid an infinite loop
1325 if (lastLastIndex
=== string
.length
) {
1326 if (lastLength
|| !separator
.test("")) {
1330 output
.push(string
.slice(lastLastIndex
));
1332 return output
.length
> limit
? output
.slice(0, limit
) : output
;
1337 // If separator is undefined, then the result array contains just one String,
1338 // which is the this value (converted to a String). If limit is not undefined,
1339 // then the output array is truncated so that it contains no more than limit
1341 // "0".split(undefined, 0) -> []
1342 } else if ("0".split(void 0, 0).length
) {
1343 StringPrototype
.split
= function split(separator
, limit
) {
1344 if (separator
=== void 0 && limit
=== 0) { return []; }
1345 return string_split
.call(this, separator
, limit
);
1349 var str_replace
= StringPrototype
.replace
;
1350 var replaceReportsGroupsCorrectly
= (function () {
1352 'x'.replace(/x(.)?/g, function (match
, group
) {
1355 return groups
.length
=== 1 && typeof groups
[0] === 'undefined';
1358 if (!replaceReportsGroupsCorrectly
) {
1359 StringPrototype
.replace
= function replace(searchValue
, replaceValue
) {
1360 var isFn
= isFunction(replaceValue
);
1361 var hasCapturingGroups
= isRegex(searchValue
) && (/\)[*?]/).test(searchValue
.source
);
1362 if (!isFn
|| !hasCapturingGroups
) {
1363 return str_replace
.call(this, searchValue
, replaceValue
);
1365 var wrappedReplaceValue = function (match
) {
1366 var length
= arguments
.length
;
1367 var originalLastIndex
= searchValue
.lastIndex
;
1368 searchValue
.lastIndex
= 0;
1369 var args
= searchValue
.exec(match
);
1370 searchValue
.lastIndex
= originalLastIndex
;
1371 args
.push(arguments
[length
- 2], arguments
[length
- 1]);
1372 return replaceValue
.apply(this, args
);
1374 return str_replace
.call(this, searchValue
, wrappedReplaceValue
);
1379 // ECMA-262, 3rd B.2.3
1380 // Not an ECMAScript standard, although ECMAScript 3rd Edition has a
1381 // non-normative section suggesting uniform semantics and it should be
1382 // normalized across all browsers
1383 // [bugfix, IE lt 9] IE < 9 substr() with negative value not working in IE
1384 var string_substr
= StringPrototype
.substr
;
1385 var hasNegativeSubstrBug
= "".substr
&& "0b".substr(-1) !== "b";
1386 defineProperties(StringPrototype
, {
1387 substr
: function substr(start
, length
) {
1388 return string_substr
.call(
1390 start
< 0 ? ((start
= this.length
+ start
) < 0 ? 0 : start
) : start
,
1394 }, hasNegativeSubstrBug
);
1397 // whitespace from: http://es5.github.io/#x15.5.4.20
1398 var ws
= "\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003" +
1399 "\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028" +
1401 var zeroWidth
= '\u200b';
1402 var wsRegexChars
= "[" + ws
+ "]";
1403 var trimBeginRegexp
= new RegExp("^" + wsRegexChars
+ wsRegexChars
+ "*");
1404 var trimEndRegexp
= new RegExp(wsRegexChars
+ wsRegexChars
+ "*$");
1405 var hasTrimWhitespaceBug
= StringPrototype
.trim
&& (ws
.trim() || !zeroWidth
.trim());
1406 defineProperties(StringPrototype
, {
1407 // http://blog.stevenlevithan.com/archives/faster-trim-javascript
1408 // http://perfectionkills.com/whitespace-deviations/
1409 trim
: function trim() {
1410 if (this === void 0 || this === null) {
1411 throw new TypeError("can't convert " + this + " to object");
1413 return String(this).replace(trimBeginRegexp
, "").replace(trimEndRegexp
, "");
1415 }, hasTrimWhitespaceBug
);
1418 if (parseInt(ws
+ '08') !== 8 || parseInt(ws
+ '0x16') !== 22) {
1419 parseInt
= (function (origParseInt
) {
1420 var hexRegex
= /^0[xX]/;
1421 return function parseIntES5(str
, radix
) {
1422 str
= String(str
).trim();
1423 if (!Number(radix
)) {
1424 radix
= hexRegex
.test(str
) ? 16 : 10;
1426 return origParseInt(str
, radix
);