es5-shim.js 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098
  1. /*!
  2. * https://github.com/es-shims/es5-shim
  3. * @license es5-shim Copyright 2009-2015 by contributors, MIT License
  4. * see https://github.com/es-shims/es5-shim/blob/master/LICENSE
  5. */
  6. // vim: ts=4 sts=4 sw=4 expandtab
  7. // Add semicolon to prevent IIFE from being passed as argument to concatenated code.
  8. ;
  9. // UMD (Universal Module Definition)
  10. // see https://github.com/umdjs/umd/blob/master/templates/returnExports.js
  11. (function (root, factory) {
  12. 'use strict';
  13. /* global define, exports, module */
  14. if (typeof define === 'function' && define.amd) {
  15. // AMD. Register as an anonymous module.
  16. define(factory);
  17. } else if (typeof exports === 'object') {
  18. // Node. Does not work with strict CommonJS, but
  19. // only CommonJS-like enviroments that support module.exports,
  20. // like Node.
  21. module.exports = factory();
  22. } else {
  23. // Browser globals (root is window)
  24. root.returnExports = factory();
  25. }
  26. }(this, function () {
  27. /**
  28. * Brings an environment as close to ECMAScript 5 compliance
  29. * as is possible with the facilities of erstwhile engines.
  30. *
  31. * Annotated ES5: http://es5.github.com/ (specific links below)
  32. * ES5 Spec: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
  33. * Required reading: http://javascriptweblog.wordpress.com/2011/12/05/extending-javascript-natives/
  34. */
  35. // Shortcut to an often accessed properties, in order to avoid multiple
  36. // dereference that costs universally. This also holds a reference to known-good
  37. // functions.
  38. var $Array = Array;
  39. var ArrayPrototype = $Array.prototype;
  40. var $Object = Object;
  41. var ObjectPrototype = $Object.prototype;
  42. var $Function = Function;
  43. var FunctionPrototype = $Function.prototype;
  44. var $String = String;
  45. var StringPrototype = $String.prototype;
  46. var $Number = Number;
  47. var NumberPrototype = $Number.prototype;
  48. var array_slice = ArrayPrototype.slice;
  49. var array_splice = ArrayPrototype.splice;
  50. var array_push = ArrayPrototype.push;
  51. var array_unshift = ArrayPrototype.unshift;
  52. var array_concat = ArrayPrototype.concat;
  53. var array_join = ArrayPrototype.join;
  54. var call = FunctionPrototype.call;
  55. var apply = FunctionPrototype.apply;
  56. var max = Math.max;
  57. var min = Math.min;
  58. // Having a toString local variable name breaks in Opera so use to_string.
  59. var to_string = ObjectPrototype.toString;
  60. /* global Symbol */
  61. /* eslint-disable one-var-declaration-per-line, no-redeclare, max-statements-per-line */
  62. var hasToStringTag = typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol';
  63. var isCallable; /* inlined from https://npmjs.com/is-callable */ var fnToStr = Function.prototype.toString, constructorRegex = /^\s*class /, isES6ClassFn = function isES6ClassFn(value) { try { var fnStr = fnToStr.call(value); var singleStripped = fnStr.replace(/\/\/.*\n/g, ''); var multiStripped = singleStripped.replace(/\/\*[.\s\S]*\*\//g, ''); var spaceStripped = multiStripped.replace(/\n/mg, ' ').replace(/ {2}/g, ' '); return constructorRegex.test(spaceStripped); } catch (e) { return false; /* not a function */ } }, tryFunctionObject = function tryFunctionObject(value) { try { if (isES6ClassFn(value)) { return false; } fnToStr.call(value); return true; } catch (e) { return false; } }, fnClass = '[object Function]', genClass = '[object GeneratorFunction]', isCallable = function isCallable(value) { if (!value) { return false; } if (typeof value !== 'function' && typeof value !== 'object') { return false; } if (hasToStringTag) { return tryFunctionObject(value); } if (isES6ClassFn(value)) { return false; } var strClass = to_string.call(value); return strClass === fnClass || strClass === genClass; };
  64. var isRegex; /* inlined from https://npmjs.com/is-regex */ var regexExec = RegExp.prototype.exec, tryRegexExec = function tryRegexExec(value) { try { regexExec.call(value); return true; } catch (e) { return false; } }, regexClass = '[object RegExp]'; isRegex = function isRegex(value) { if (typeof value !== 'object') { return false; } return hasToStringTag ? tryRegexExec(value) : to_string.call(value) === regexClass; };
  65. var isString; /* inlined from https://npmjs.com/is-string */ var strValue = String.prototype.valueOf, tryStringObject = function tryStringObject(value) { try { strValue.call(value); return true; } catch (e) { return false; } }, stringClass = '[object String]'; isString = function isString(value) { if (typeof value === 'string') { return true; } if (typeof value !== 'object') { return false; } return hasToStringTag ? tryStringObject(value) : to_string.call(value) === stringClass; };
  66. /* eslint-enable one-var-declaration-per-line, no-redeclare, max-statements-per-line */
  67. /* inlined from http://npmjs.com/define-properties */
  68. var supportsDescriptors = $Object.defineProperty && (function () {
  69. try {
  70. var obj = {};
  71. $Object.defineProperty(obj, 'x', { enumerable: false, value: obj });
  72. for (var _ in obj) { // jscs:ignore disallowUnusedVariables
  73. return false;
  74. }
  75. return obj.x === obj;
  76. } catch (e) { /* this is ES3 */
  77. return false;
  78. }
  79. }());
  80. var defineProperties = (function (has) {
  81. // Define configurable, writable, and non-enumerable props
  82. // if they don't exist.
  83. var defineProperty;
  84. if (supportsDescriptors) {
  85. defineProperty = function (object, name, method, forceAssign) {
  86. if (!forceAssign && (name in object)) {
  87. return;
  88. }
  89. $Object.defineProperty(object, name, {
  90. configurable: true,
  91. enumerable: false,
  92. writable: true,
  93. value: method
  94. });
  95. };
  96. } else {
  97. defineProperty = function (object, name, method, forceAssign) {
  98. if (!forceAssign && (name in object)) {
  99. return;
  100. }
  101. object[name] = method;
  102. };
  103. }
  104. return function defineProperties(object, map, forceAssign) {
  105. for (var name in map) {
  106. if (has.call(map, name)) {
  107. defineProperty(object, name, map[name], forceAssign);
  108. }
  109. }
  110. };
  111. }(ObjectPrototype.hasOwnProperty));
  112. //
  113. // Util
  114. // ======
  115. //
  116. /* replaceable with https://npmjs.com/package/es-abstract /helpers/isPrimitive */
  117. var isPrimitive = function isPrimitive(input) {
  118. var type = typeof input;
  119. return input === null || (type !== 'object' && type !== 'function');
  120. };
  121. var isActualNaN = $Number.isNaN || function isActualNaN(x) {
  122. return x !== x;
  123. };
  124. var ES = {
  125. // ES5 9.4
  126. // http://es5.github.com/#x9.4
  127. // http://jsperf.com/to-integer
  128. /* replaceable with https://npmjs.com/package/es-abstract ES5.ToInteger */
  129. ToInteger: function ToInteger(num) {
  130. var n = +num;
  131. if (isActualNaN(n)) {
  132. n = 0;
  133. } else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0)) {
  134. n = (n > 0 || -1) * Math.floor(Math.abs(n));
  135. }
  136. return n;
  137. },
  138. /* replaceable with https://npmjs.com/package/es-abstract ES5.ToPrimitive */
  139. ToPrimitive: function ToPrimitive(input) {
  140. var val, valueOf, toStr;
  141. if (isPrimitive(input)) {
  142. return input;
  143. }
  144. valueOf = input.valueOf;
  145. if (isCallable(valueOf)) {
  146. val = valueOf.call(input);
  147. if (isPrimitive(val)) {
  148. return val;
  149. }
  150. }
  151. toStr = input.toString;
  152. if (isCallable(toStr)) {
  153. val = toStr.call(input);
  154. if (isPrimitive(val)) {
  155. return val;
  156. }
  157. }
  158. throw new TypeError();
  159. },
  160. // ES5 9.9
  161. // http://es5.github.com/#x9.9
  162. /* replaceable with https://npmjs.com/package/es-abstract ES5.ToObject */
  163. ToObject: function (o) {
  164. if (o == null) { // this matches both null and undefined
  165. throw new TypeError("can't convert " + o + ' to object');
  166. }
  167. return $Object(o);
  168. },
  169. /* replaceable with https://npmjs.com/package/es-abstract ES5.ToUint32 */
  170. ToUint32: function ToUint32(x) {
  171. return x >>> 0;
  172. }
  173. };
  174. //
  175. // Function
  176. // ========
  177. //
  178. // ES-5 15.3.4.5
  179. // http://es5.github.com/#x15.3.4.5
  180. var Empty = function Empty() {};
  181. defineProperties(FunctionPrototype, {
  182. bind: function bind(that) { // .length is 1
  183. // 1. Let Target be the this value.
  184. var target = this;
  185. // 2. If IsCallable(Target) is false, throw a TypeError exception.
  186. if (!isCallable(target)) {
  187. throw new TypeError('Function.prototype.bind called on incompatible ' + target);
  188. }
  189. // 3. Let A be a new (possibly empty) internal list of all of the
  190. // argument values provided after thisArg (arg1, arg2 etc), in order.
  191. // XXX slicedArgs will stand in for "A" if used
  192. var args = array_slice.call(arguments, 1); // for normal call
  193. // 4. Let F be a new native ECMAScript object.
  194. // 11. Set the [[Prototype]] internal property of F to the standard
  195. // built-in Function prototype object as specified in 15.3.3.1.
  196. // 12. Set the [[Call]] internal property of F as described in
  197. // 15.3.4.5.1.
  198. // 13. Set the [[Construct]] internal property of F as described in
  199. // 15.3.4.5.2.
  200. // 14. Set the [[HasInstance]] internal property of F as described in
  201. // 15.3.4.5.3.
  202. var bound;
  203. var binder = function () {
  204. if (this instanceof bound) {
  205. // 15.3.4.5.2 [[Construct]]
  206. // When the [[Construct]] internal method of a function object,
  207. // F that was created using the bind function is called with a
  208. // list of arguments ExtraArgs, the following steps are taken:
  209. // 1. Let target be the value of F's [[TargetFunction]]
  210. // internal property.
  211. // 2. If target has no [[Construct]] internal method, a
  212. // TypeError exception is thrown.
  213. // 3. Let boundArgs be the value of F's [[BoundArgs]] internal
  214. // property.
  215. // 4. Let args be a new list containing the same values as the
  216. // list boundArgs in the same order followed by the same
  217. // values as the list ExtraArgs in the same order.
  218. // 5. Return the result of calling the [[Construct]] internal
  219. // method of target providing args as the arguments.
  220. var result = apply.call(
  221. target,
  222. this,
  223. array_concat.call(args, array_slice.call(arguments))
  224. );
  225. if ($Object(result) === result) {
  226. return result;
  227. }
  228. return this;
  229. } else {
  230. // 15.3.4.5.1 [[Call]]
  231. // When the [[Call]] internal method of a function object, F,
  232. // which was created using the bind function is called with a
  233. // this value and a list of arguments ExtraArgs, the following
  234. // steps are taken:
  235. // 1. Let boundArgs be the value of F's [[BoundArgs]] internal
  236. // property.
  237. // 2. Let boundThis be the value of F's [[BoundThis]] internal
  238. // property.
  239. // 3. Let target be the value of F's [[TargetFunction]] internal
  240. // property.
  241. // 4. Let args be a new list containing the same values as the
  242. // list boundArgs in the same order followed by the same
  243. // values as the list ExtraArgs in the same order.
  244. // 5. Return the result of calling the [[Call]] internal method
  245. // of target providing boundThis as the this value and
  246. // providing args as the arguments.
  247. // equiv: target.call(this, ...boundArgs, ...args)
  248. return apply.call(
  249. target,
  250. that,
  251. array_concat.call(args, array_slice.call(arguments))
  252. );
  253. }
  254. };
  255. // 15. If the [[Class]] internal property of Target is "Function", then
  256. // a. Let L be the length property of Target minus the length of A.
  257. // b. Set the length own property of F to either 0 or L, whichever is
  258. // larger.
  259. // 16. Else set the length own property of F to 0.
  260. var boundLength = max(0, target.length - args.length);
  261. // 17. Set the attributes of the length own property of F to the values
  262. // specified in 15.3.5.1.
  263. var boundArgs = [];
  264. for (var i = 0; i < boundLength; i++) {
  265. array_push.call(boundArgs, '$' + i);
  266. }
  267. // XXX Build a dynamic function with desired amount of arguments is the only
  268. // way to set the length property of a function.
  269. // In environments where Content Security Policies enabled (Chrome extensions,
  270. // for ex.) all use of eval or Function costructor throws an exception.
  271. // However in all of these environments Function.prototype.bind exists
  272. // and so this code will never be executed.
  273. bound = $Function('binder', 'return function (' + array_join.call(boundArgs, ',') + '){ return binder.apply(this, arguments); }')(binder);
  274. if (target.prototype) {
  275. Empty.prototype = target.prototype;
  276. bound.prototype = new Empty();
  277. // Clean up dangling references.
  278. Empty.prototype = null;
  279. }
  280. // TODO
  281. // 18. Set the [[Extensible]] internal property of F to true.
  282. // TODO
  283. // 19. Let thrower be the [[ThrowTypeError]] function Object (13.2.3).
  284. // 20. Call the [[DefineOwnProperty]] internal method of F with
  285. // arguments "caller", PropertyDescriptor {[[Get]]: thrower, [[Set]]:
  286. // thrower, [[Enumerable]]: false, [[Configurable]]: false}, and
  287. // false.
  288. // 21. Call the [[DefineOwnProperty]] internal method of F with
  289. // arguments "arguments", PropertyDescriptor {[[Get]]: thrower,
  290. // [[Set]]: thrower, [[Enumerable]]: false, [[Configurable]]: false},
  291. // and false.
  292. // TODO
  293. // NOTE Function objects created using Function.prototype.bind do not
  294. // have a prototype property or the [[Code]], [[FormalParameters]], and
  295. // [[Scope]] internal properties.
  296. // XXX can't delete prototype in pure-js.
  297. // 22. Return F.
  298. return bound;
  299. }
  300. });
  301. // _Please note: Shortcuts are defined after `Function.prototype.bind` as we
  302. // use it in defining shortcuts.
  303. var owns = call.bind(ObjectPrototype.hasOwnProperty);
  304. var toStr = call.bind(ObjectPrototype.toString);
  305. var arraySlice = call.bind(array_slice);
  306. var arraySliceApply = apply.bind(array_slice);
  307. /* globals document */
  308. if (typeof document === 'object' && document && document.documentElement) {
  309. try {
  310. arraySlice(document.documentElement.childNodes);
  311. } catch (e) {
  312. var origArraySlice = arraySlice;
  313. var origArraySliceApply = arraySliceApply;
  314. arraySlice = function arraySliceIE(arr) {
  315. var r = [];
  316. var i = arr.length;
  317. while (i-- > 0) {
  318. r[i] = arr[i];
  319. }
  320. return origArraySliceApply(r, origArraySlice(arguments, 1));
  321. };
  322. arraySliceApply = function arraySliceApplyIE(arr, args) {
  323. return origArraySliceApply(arraySlice(arr), args);
  324. };
  325. }
  326. }
  327. var strSlice = call.bind(StringPrototype.slice);
  328. var strSplit = call.bind(StringPrototype.split);
  329. var strIndexOf = call.bind(StringPrototype.indexOf);
  330. var pushCall = call.bind(array_push);
  331. var isEnum = call.bind(ObjectPrototype.propertyIsEnumerable);
  332. var arraySort = call.bind(ArrayPrototype.sort);
  333. //
  334. // Array
  335. // =====
  336. //
  337. var isArray = $Array.isArray || function isArray(obj) {
  338. return toStr(obj) === '[object Array]';
  339. };
  340. // ES5 15.4.4.12
  341. // http://es5.github.com/#x15.4.4.13
  342. // Return len+argCount.
  343. // [bugfix, ielt8]
  344. // IE < 8 bug: [].unshift(0) === undefined but should be "1"
  345. var hasUnshiftReturnValueBug = [].unshift(0) !== 1;
  346. defineProperties(ArrayPrototype, {
  347. unshift: function () {
  348. array_unshift.apply(this, arguments);
  349. return this.length;
  350. }
  351. }, hasUnshiftReturnValueBug);
  352. // ES5 15.4.3.2
  353. // http://es5.github.com/#x15.4.3.2
  354. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray
  355. defineProperties($Array, { isArray: isArray });
  356. // The IsCallable() check in the Array functions
  357. // has been replaced with a strict check on the
  358. // internal class of the object to trap cases where
  359. // the provided function was actually a regular
  360. // expression literal, which in V8 and
  361. // JavaScriptCore is a typeof "function". Only in
  362. // V8 are regular expression literals permitted as
  363. // reduce parameters, so it is desirable in the
  364. // general case for the shim to match the more
  365. // strict and common behavior of rejecting regular
  366. // expressions.
  367. // ES5 15.4.4.18
  368. // http://es5.github.com/#x15.4.4.18
  369. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/forEach
  370. // Check failure of by-index access of string characters (IE < 9)
  371. // and failure of `0 in boxedString` (Rhino)
  372. var boxedString = $Object('a');
  373. var splitString = boxedString[0] !== 'a' || !(0 in boxedString);
  374. var properlyBoxesContext = function properlyBoxed(method) {
  375. // Check node 0.6.21 bug where third parameter is not boxed
  376. var properlyBoxesNonStrict = true;
  377. var properlyBoxesStrict = true;
  378. var threwException = false;
  379. if (method) {
  380. try {
  381. method.call('foo', function (_, __, context) {
  382. if (typeof context !== 'object') {
  383. properlyBoxesNonStrict = false;
  384. }
  385. });
  386. method.call([1], function () {
  387. 'use strict';
  388. properlyBoxesStrict = typeof this === 'string';
  389. }, 'x');
  390. } catch (e) {
  391. threwException = true;
  392. }
  393. }
  394. return !!method && !threwException && properlyBoxesNonStrict && properlyBoxesStrict;
  395. };
  396. defineProperties(ArrayPrototype, {
  397. forEach: function forEach(callbackfn/*, thisArg*/) {
  398. var object = ES.ToObject(this);
  399. var self = splitString && isString(this) ? strSplit(this, '') : object;
  400. var i = -1;
  401. var length = ES.ToUint32(self.length);
  402. var T;
  403. if (arguments.length > 1) {
  404. T = arguments[1];
  405. }
  406. // If no callback function or if callback is not a callable function
  407. if (!isCallable(callbackfn)) {
  408. throw new TypeError('Array.prototype.forEach callback must be a function');
  409. }
  410. while (++i < length) {
  411. if (i in self) {
  412. // Invoke the callback function with call, passing arguments:
  413. // context, property value, property key, thisArg object
  414. if (typeof T === 'undefined') {
  415. callbackfn(self[i], i, object);
  416. } else {
  417. callbackfn.call(T, self[i], i, object);
  418. }
  419. }
  420. }
  421. }
  422. }, !properlyBoxesContext(ArrayPrototype.forEach));
  423. // ES5 15.4.4.19
  424. // http://es5.github.com/#x15.4.4.19
  425. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/map
  426. defineProperties(ArrayPrototype, {
  427. map: function map(callbackfn/*, thisArg*/) {
  428. var object = ES.ToObject(this);
  429. var self = splitString && isString(this) ? strSplit(this, '') : object;
  430. var length = ES.ToUint32(self.length);
  431. var result = $Array(length);
  432. var T;
  433. if (arguments.length > 1) {
  434. T = arguments[1];
  435. }
  436. // If no callback function or if callback is not a callable function
  437. if (!isCallable(callbackfn)) {
  438. throw new TypeError('Array.prototype.map callback must be a function');
  439. }
  440. for (var i = 0; i < length; i++) {
  441. if (i in self) {
  442. if (typeof T === 'undefined') {
  443. result[i] = callbackfn(self[i], i, object);
  444. } else {
  445. result[i] = callbackfn.call(T, self[i], i, object);
  446. }
  447. }
  448. }
  449. return result;
  450. }
  451. }, !properlyBoxesContext(ArrayPrototype.map));
  452. // ES5 15.4.4.20
  453. // http://es5.github.com/#x15.4.4.20
  454. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/filter
  455. defineProperties(ArrayPrototype, {
  456. filter: function filter(callbackfn/*, thisArg*/) {
  457. var object = ES.ToObject(this);
  458. var self = splitString && isString(this) ? strSplit(this, '') : object;
  459. var length = ES.ToUint32(self.length);
  460. var result = [];
  461. var value;
  462. var T;
  463. if (arguments.length > 1) {
  464. T = arguments[1];
  465. }
  466. // If no callback function or if callback is not a callable function
  467. if (!isCallable(callbackfn)) {
  468. throw new TypeError('Array.prototype.filter callback must be a function');
  469. }
  470. for (var i = 0; i < length; i++) {
  471. if (i in self) {
  472. value = self[i];
  473. if (typeof T === 'undefined' ? callbackfn(value, i, object) : callbackfn.call(T, value, i, object)) {
  474. pushCall(result, value);
  475. }
  476. }
  477. }
  478. return result;
  479. }
  480. }, !properlyBoxesContext(ArrayPrototype.filter));
  481. // ES5 15.4.4.16
  482. // http://es5.github.com/#x15.4.4.16
  483. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every
  484. defineProperties(ArrayPrototype, {
  485. every: function every(callbackfn/*, thisArg*/) {
  486. var object = ES.ToObject(this);
  487. var self = splitString && isString(this) ? strSplit(this, '') : object;
  488. var length = ES.ToUint32(self.length);
  489. var T;
  490. if (arguments.length > 1) {
  491. T = arguments[1];
  492. }
  493. // If no callback function or if callback is not a callable function
  494. if (!isCallable(callbackfn)) {
  495. throw new TypeError('Array.prototype.every callback must be a function');
  496. }
  497. for (var i = 0; i < length; i++) {
  498. if (i in self && !(typeof T === 'undefined' ? callbackfn(self[i], i, object) : callbackfn.call(T, self[i], i, object))) {
  499. return false;
  500. }
  501. }
  502. return true;
  503. }
  504. }, !properlyBoxesContext(ArrayPrototype.every));
  505. // ES5 15.4.4.17
  506. // http://es5.github.com/#x15.4.4.17
  507. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some
  508. defineProperties(ArrayPrototype, {
  509. some: function some(callbackfn/*, thisArg */) {
  510. var object = ES.ToObject(this);
  511. var self = splitString && isString(this) ? strSplit(this, '') : object;
  512. var length = ES.ToUint32(self.length);
  513. var T;
  514. if (arguments.length > 1) {
  515. T = arguments[1];
  516. }
  517. // If no callback function or if callback is not a callable function
  518. if (!isCallable(callbackfn)) {
  519. throw new TypeError('Array.prototype.some callback must be a function');
  520. }
  521. for (var i = 0; i < length; i++) {
  522. if (i in self && (typeof T === 'undefined' ? callbackfn(self[i], i, object) : callbackfn.call(T, self[i], i, object))) {
  523. return true;
  524. }
  525. }
  526. return false;
  527. }
  528. }, !properlyBoxesContext(ArrayPrototype.some));
  529. // ES5 15.4.4.21
  530. // http://es5.github.com/#x15.4.4.21
  531. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduce
  532. var reduceCoercesToObject = false;
  533. if (ArrayPrototype.reduce) {
  534. reduceCoercesToObject = typeof ArrayPrototype.reduce.call('es5', function (_, __, ___, list) {
  535. return list;
  536. }) === 'object';
  537. }
  538. defineProperties(ArrayPrototype, {
  539. reduce: function reduce(callbackfn/*, initialValue*/) {
  540. var object = ES.ToObject(this);
  541. var self = splitString && isString(this) ? strSplit(this, '') : object;
  542. var length = ES.ToUint32(self.length);
  543. // If no callback function or if callback is not a callable function
  544. if (!isCallable(callbackfn)) {
  545. throw new TypeError('Array.prototype.reduce callback must be a function');
  546. }
  547. // no value to return if no initial value and an empty array
  548. if (length === 0 && arguments.length === 1) {
  549. throw new TypeError('reduce of empty array with no initial value');
  550. }
  551. var i = 0;
  552. var result;
  553. if (arguments.length >= 2) {
  554. result = arguments[1];
  555. } else {
  556. do {
  557. if (i in self) {
  558. result = self[i++];
  559. break;
  560. }
  561. // if array contains no values, no initial value to return
  562. if (++i >= length) {
  563. throw new TypeError('reduce of empty array with no initial value');
  564. }
  565. } while (true);
  566. }
  567. for (; i < length; i++) {
  568. if (i in self) {
  569. result = callbackfn(result, self[i], i, object);
  570. }
  571. }
  572. return result;
  573. }
  574. }, !reduceCoercesToObject);
  575. // ES5 15.4.4.22
  576. // http://es5.github.com/#x15.4.4.22
  577. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduceRight
  578. var reduceRightCoercesToObject = false;
  579. if (ArrayPrototype.reduceRight) {
  580. reduceRightCoercesToObject = typeof ArrayPrototype.reduceRight.call('es5', function (_, __, ___, list) {
  581. return list;
  582. }) === 'object';
  583. }
  584. defineProperties(ArrayPrototype, {
  585. reduceRight: function reduceRight(callbackfn/*, initial*/) {
  586. var object = ES.ToObject(this);
  587. var self = splitString && isString(this) ? strSplit(this, '') : object;
  588. var length = ES.ToUint32(self.length);
  589. // If no callback function or if callback is not a callable function
  590. if (!isCallable(callbackfn)) {
  591. throw new TypeError('Array.prototype.reduceRight callback must be a function');
  592. }
  593. // no value to return if no initial value, empty array
  594. if (length === 0 && arguments.length === 1) {
  595. throw new TypeError('reduceRight of empty array with no initial value');
  596. }
  597. var result;
  598. var i = length - 1;
  599. if (arguments.length >= 2) {
  600. result = arguments[1];
  601. } else {
  602. do {
  603. if (i in self) {
  604. result = self[i--];
  605. break;
  606. }
  607. // if array contains no values, no initial value to return
  608. if (--i < 0) {
  609. throw new TypeError('reduceRight of empty array with no initial value');
  610. }
  611. } while (true);
  612. }
  613. if (i < 0) {
  614. return result;
  615. }
  616. do {
  617. if (i in self) {
  618. result = callbackfn(result, self[i], i, object);
  619. }
  620. } while (i--);
  621. return result;
  622. }
  623. }, !reduceRightCoercesToObject);
  624. // ES5 15.4.4.14
  625. // http://es5.github.com/#x15.4.4.14
  626. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
  627. var hasFirefox2IndexOfBug = ArrayPrototype.indexOf && [0, 1].indexOf(1, 2) !== -1;
  628. defineProperties(ArrayPrototype, {
  629. indexOf: function indexOf(searchElement/*, fromIndex */) {
  630. var self = splitString && isString(this) ? strSplit(this, '') : ES.ToObject(this);
  631. var length = ES.ToUint32(self.length);
  632. if (length === 0) {
  633. return -1;
  634. }
  635. var i = 0;
  636. if (arguments.length > 1) {
  637. i = ES.ToInteger(arguments[1]);
  638. }
  639. // handle negative indices
  640. i = i >= 0 ? i : max(0, length + i);
  641. for (; i < length; i++) {
  642. if (i in self && self[i] === searchElement) {
  643. return i;
  644. }
  645. }
  646. return -1;
  647. }
  648. }, hasFirefox2IndexOfBug);
  649. // ES5 15.4.4.15
  650. // http://es5.github.com/#x15.4.4.15
  651. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/lastIndexOf
  652. var hasFirefox2LastIndexOfBug = ArrayPrototype.lastIndexOf && [0, 1].lastIndexOf(0, -3) !== -1;
  653. defineProperties(ArrayPrototype, {
  654. lastIndexOf: function lastIndexOf(searchElement/*, fromIndex */) {
  655. var self = splitString && isString(this) ? strSplit(this, '') : ES.ToObject(this);
  656. var length = ES.ToUint32(self.length);
  657. if (length === 0) {
  658. return -1;
  659. }
  660. var i = length - 1;
  661. if (arguments.length > 1) {
  662. i = min(i, ES.ToInteger(arguments[1]));
  663. }
  664. // handle negative indices
  665. i = i >= 0 ? i : length - Math.abs(i);
  666. for (; i >= 0; i--) {
  667. if (i in self && searchElement === self[i]) {
  668. return i;
  669. }
  670. }
  671. return -1;
  672. }
  673. }, hasFirefox2LastIndexOfBug);
  674. // ES5 15.4.4.12
  675. // http://es5.github.com/#x15.4.4.12
  676. var spliceNoopReturnsEmptyArray = (function () {
  677. var a = [1, 2];
  678. var result = a.splice();
  679. return a.length === 2 && isArray(result) && result.length === 0;
  680. }());
  681. defineProperties(ArrayPrototype, {
  682. // Safari 5.0 bug where .splice() returns undefined
  683. splice: function splice(start, deleteCount) {
  684. if (arguments.length === 0) {
  685. return [];
  686. } else {
  687. return array_splice.apply(this, arguments);
  688. }
  689. }
  690. }, !spliceNoopReturnsEmptyArray);
  691. var spliceWorksWithEmptyObject = (function () {
  692. var obj = {};
  693. ArrayPrototype.splice.call(obj, 0, 0, 1);
  694. return obj.length === 1;
  695. }());
  696. defineProperties(ArrayPrototype, {
  697. splice: function splice(start, deleteCount) {
  698. if (arguments.length === 0) {
  699. return [];
  700. }
  701. var args = arguments;
  702. this.length = max(ES.ToInteger(this.length), 0);
  703. if (arguments.length > 0 && typeof deleteCount !== 'number') {
  704. args = arraySlice(arguments);
  705. if (args.length < 2) {
  706. pushCall(args, this.length - start);
  707. } else {
  708. args[1] = ES.ToInteger(deleteCount);
  709. }
  710. }
  711. return array_splice.apply(this, args);
  712. }
  713. }, !spliceWorksWithEmptyObject);
  714. var spliceWorksWithLargeSparseArrays = (function () {
  715. // Per https://github.com/es-shims/es5-shim/issues/295
  716. // Safari 7/8 breaks with sparse arrays of size 1e5 or greater
  717. var arr = new $Array(1e5);
  718. // note: the index MUST be 8 or larger or the test will false pass
  719. arr[8] = 'x';
  720. arr.splice(1, 1);
  721. // note: this test must be defined *after* the indexOf shim
  722. // per https://github.com/es-shims/es5-shim/issues/313
  723. return arr.indexOf('x') === 7;
  724. }());
  725. var spliceWorksWithSmallSparseArrays = (function () {
  726. // Per https://github.com/es-shims/es5-shim/issues/295
  727. // Opera 12.15 breaks on this, no idea why.
  728. var n = 256;
  729. var arr = [];
  730. arr[n] = 'a';
  731. arr.splice(n + 1, 0, 'b');
  732. return arr[n] === 'a';
  733. }());
  734. defineProperties(ArrayPrototype, {
  735. splice: function splice(start, deleteCount) {
  736. var O = ES.ToObject(this);
  737. var A = [];
  738. var len = ES.ToUint32(O.length);
  739. var relativeStart = ES.ToInteger(start);
  740. var actualStart = relativeStart < 0 ? max((len + relativeStart), 0) : min(relativeStart, len);
  741. var actualDeleteCount = min(max(ES.ToInteger(deleteCount), 0), len - actualStart);
  742. var k = 0;
  743. var from;
  744. while (k < actualDeleteCount) {
  745. from = $String(actualStart + k);
  746. if (owns(O, from)) {
  747. A[k] = O[from];
  748. }
  749. k += 1;
  750. }
  751. var items = arraySlice(arguments, 2);
  752. var itemCount = items.length;
  753. var to;
  754. if (itemCount < actualDeleteCount) {
  755. k = actualStart;
  756. var maxK = len - actualDeleteCount;
  757. while (k < maxK) {
  758. from = $String(k + actualDeleteCount);
  759. to = $String(k + itemCount);
  760. if (owns(O, from)) {
  761. O[to] = O[from];
  762. } else {
  763. delete O[to];
  764. }
  765. k += 1;
  766. }
  767. k = len;
  768. var minK = len - actualDeleteCount + itemCount;
  769. while (k > minK) {
  770. delete O[k - 1];
  771. k -= 1;
  772. }
  773. } else if (itemCount > actualDeleteCount) {
  774. k = len - actualDeleteCount;
  775. while (k > actualStart) {
  776. from = $String(k + actualDeleteCount - 1);
  777. to = $String(k + itemCount - 1);
  778. if (owns(O, from)) {
  779. O[to] = O[from];
  780. } else {
  781. delete O[to];
  782. }
  783. k -= 1;
  784. }
  785. }
  786. k = actualStart;
  787. for (var i = 0; i < items.length; ++i) {
  788. O[k] = items[i];
  789. k += 1;
  790. }
  791. O.length = len - actualDeleteCount + itemCount;
  792. return A;
  793. }
  794. }, !spliceWorksWithLargeSparseArrays || !spliceWorksWithSmallSparseArrays);
  795. var originalJoin = ArrayPrototype.join;
  796. var hasStringJoinBug;
  797. try {
  798. hasStringJoinBug = Array.prototype.join.call('123', ',') !== '1,2,3';
  799. } catch (e) {
  800. hasStringJoinBug = true;
  801. }
  802. if (hasStringJoinBug) {
  803. defineProperties(ArrayPrototype, {
  804. join: function join(separator) {
  805. var sep = typeof separator === 'undefined' ? ',' : separator;
  806. return originalJoin.call(isString(this) ? strSplit(this, '') : this, sep);
  807. }
  808. }, hasStringJoinBug);
  809. }
  810. var hasJoinUndefinedBug = [1, 2].join(undefined) !== '1,2';
  811. if (hasJoinUndefinedBug) {
  812. defineProperties(ArrayPrototype, {
  813. join: function join(separator) {
  814. var sep = typeof separator === 'undefined' ? ',' : separator;
  815. return originalJoin.call(this, sep);
  816. }
  817. }, hasJoinUndefinedBug);
  818. }
  819. var pushShim = function push(item) {
  820. var O = ES.ToObject(this);
  821. var n = ES.ToUint32(O.length);
  822. var i = 0;
  823. while (i < arguments.length) {
  824. O[n + i] = arguments[i];
  825. i += 1;
  826. }
  827. O.length = n + i;
  828. return n + i;
  829. };
  830. var pushIsNotGeneric = (function () {
  831. var obj = {};
  832. var result = Array.prototype.push.call(obj, undefined);
  833. return result !== 1 || obj.length !== 1 || typeof obj[0] !== 'undefined' || !owns(obj, 0);
  834. }());
  835. defineProperties(ArrayPrototype, {
  836. push: function push(item) {
  837. if (isArray(this)) {
  838. return array_push.apply(this, arguments);
  839. }
  840. return pushShim.apply(this, arguments);
  841. }
  842. }, pushIsNotGeneric);
  843. // This fixes a very weird bug in Opera 10.6 when pushing `undefined
  844. var pushUndefinedIsWeird = (function () {
  845. var arr = [];
  846. var result = arr.push(undefined);
  847. return result !== 1 || arr.length !== 1 || typeof arr[0] !== 'undefined' || !owns(arr, 0);
  848. }());
  849. defineProperties(ArrayPrototype, { push: pushShim }, pushUndefinedIsWeird);
  850. // ES5 15.2.3.14
  851. // http://es5.github.io/#x15.4.4.10
  852. // Fix boxed string bug
  853. defineProperties(ArrayPrototype, {
  854. slice: function (start, end) {
  855. var arr = isString(this) ? strSplit(this, '') : this;
  856. return arraySliceApply(arr, arguments);
  857. }
  858. }, splitString);
  859. var sortIgnoresNonFunctions = (function () {
  860. try {
  861. [1, 2].sort(null);
  862. } catch (e) {
  863. try {
  864. [1, 2].sort({});
  865. } catch (e2) {
  866. return false;
  867. }
  868. }
  869. return true;
  870. }());
  871. var sortThrowsOnRegex = (function () {
  872. // this is a problem in Firefox 4, in which `typeof /a/ === 'function'`
  873. try {
  874. [1, 2].sort(/a/);
  875. return false;
  876. } catch (e) {}
  877. return true;
  878. }());
  879. var sortIgnoresUndefined = (function () {
  880. // applies in IE 8, for one.
  881. try {
  882. [1, 2].sort(undefined);
  883. return true;
  884. } catch (e) {}
  885. return false;
  886. }());
  887. defineProperties(ArrayPrototype, {
  888. sort: function sort(compareFn) {
  889. if (typeof compareFn === 'undefined') {
  890. return arraySort(this);
  891. }
  892. if (!isCallable(compareFn)) {
  893. throw new TypeError('Array.prototype.sort callback must be a function');
  894. }
  895. return arraySort(this, compareFn);
  896. }
  897. }, sortIgnoresNonFunctions || !sortIgnoresUndefined || !sortThrowsOnRegex);
  898. //
  899. // Object
  900. // ======
  901. //
  902. // ES5 15.2.3.14
  903. // http://es5.github.com/#x15.2.3.14
  904. // http://whattheheadsaid.com/2010/10/a-safer-object-keys-compatibility-implementation
  905. var hasDontEnumBug = !isEnum({ 'toString': null }, 'toString'); // jscs:ignore disallowQuotedKeysInObjects
  906. var hasProtoEnumBug = isEnum(function () {}, 'prototype');
  907. var hasStringEnumBug = !owns('x', '0');
  908. var equalsConstructorPrototype = function (o) {
  909. var ctor = o.constructor;
  910. return ctor && ctor.prototype === o;
  911. };
  912. var excludedKeys = {
  913. $window: true,
  914. $console: true,
  915. $parent: true,
  916. $self: true,
  917. $frame: true,
  918. $frames: true,
  919. $frameElement: true,
  920. $webkitIndexedDB: true,
  921. $webkitStorageInfo: true,
  922. $external: true,
  923. $width: true,
  924. $height: true,
  925. $top: true,
  926. $localStorage: true
  927. };
  928. var hasAutomationEqualityBug = (function () {
  929. /* globals window */
  930. if (typeof window === 'undefined') {
  931. return false;
  932. }
  933. for (var k in window) {
  934. try {
  935. if (!excludedKeys['$' + k] && owns(window, k) && window[k] !== null && typeof window[k] === 'object') {
  936. equalsConstructorPrototype(window[k]);
  937. }
  938. } catch (e) {
  939. return true;
  940. }
  941. }
  942. return false;
  943. }());
  944. var equalsConstructorPrototypeIfNotBuggy = function (object) {
  945. if (typeof window === 'undefined' || !hasAutomationEqualityBug) {
  946. return equalsConstructorPrototype(object);
  947. }
  948. try {
  949. return equalsConstructorPrototype(object);
  950. } catch (e) {
  951. return false;
  952. }
  953. };
  954. var dontEnums = [
  955. 'toString',
  956. 'toLocaleString',
  957. 'valueOf',
  958. 'hasOwnProperty',
  959. 'isPrototypeOf',
  960. 'propertyIsEnumerable',
  961. 'constructor'
  962. ];
  963. var dontEnumsLength = dontEnums.length;
  964. // taken directly from https://github.com/ljharb/is-arguments/blob/master/index.js
  965. // can be replaced with require('is-arguments') if we ever use a build process instead
  966. var isStandardArguments = function isArguments(value) {
  967. return toStr(value) === '[object Arguments]';
  968. };
  969. var isLegacyArguments = function isArguments(value) {
  970. return value !== null
  971. && typeof value === 'object'
  972. && typeof value.length === 'number'
  973. && value.length >= 0
  974. && !isArray(value)
  975. && isCallable(value.callee);
  976. };
  977. var isArguments = isStandardArguments(arguments) ? isStandardArguments : isLegacyArguments;
  978. defineProperties($Object, {
  979. keys: function keys(object) {
  980. var isFn = isCallable(object);
  981. var isArgs = isArguments(object);
  982. var isObject = object !== null && typeof object === 'object';
  983. var isStr = isObject && isString(object);
  984. if (!isObject && !isFn && !isArgs) {
  985. throw new TypeError('Object.keys called on a non-object');
  986. }
  987. var theKeys = [];
  988. var skipProto = hasProtoEnumBug && isFn;
  989. if ((isStr && hasStringEnumBug) || isArgs) {
  990. for (var i = 0; i < object.length; ++i) {
  991. pushCall(theKeys, $String(i));
  992. }
  993. }
  994. if (!isArgs) {
  995. for (var name in object) {
  996. if (!(skipProto && name === 'prototype') && owns(object, name)) {
  997. pushCall(theKeys, $String(name));
  998. }
  999. }
  1000. }
  1001. if (hasDontEnumBug) {
  1002. var skipConstructor = equalsConstructorPrototypeIfNotBuggy(object);
  1003. for (var j = 0; j < dontEnumsLength; j++) {
  1004. var dontEnum = dontEnums[j];
  1005. if (!(skipConstructor && dontEnum === 'constructor') && owns(object, dontEnum)) {
  1006. pushCall(theKeys, dontEnum);
  1007. }
  1008. }
  1009. }
  1010. return theKeys;
  1011. }
  1012. });
  1013. var keysWorksWithArguments = $Object.keys && (function () {
  1014. // Safari 5.0 bug
  1015. return $Object.keys(arguments).length === 2;
  1016. }(1, 2));
  1017. var keysHasArgumentsLengthBug = $Object.keys && (function () {
  1018. var argKeys = $Object.keys(arguments);
  1019. return arguments.length !== 1 || argKeys.length !== 1 || argKeys[0] !== 1;
  1020. }(1));
  1021. var originalKeys = $Object.keys;
  1022. defineProperties($Object, {
  1023. keys: function keys(object) {
  1024. if (isArguments(object)) {
  1025. return originalKeys(arraySlice(object));
  1026. } else {
  1027. return originalKeys(object);
  1028. }
  1029. }
  1030. }, !keysWorksWithArguments || keysHasArgumentsLengthBug);
  1031. //
  1032. // Date
  1033. // ====
  1034. //
  1035. var hasNegativeMonthYearBug = new Date(-3509827329600292).getUTCMonth() !== 0;
  1036. var aNegativeTestDate = new Date(-1509842289600292);
  1037. var aPositiveTestDate = new Date(1449662400000);
  1038. var hasToUTCStringFormatBug = aNegativeTestDate.toUTCString() !== 'Mon, 01 Jan -45875 11:59:59 GMT';
  1039. var hasToDateStringFormatBug;
  1040. var hasToStringFormatBug;
  1041. var timeZoneOffset = aNegativeTestDate.getTimezoneOffset();
  1042. if (timeZoneOffset < -720) {
  1043. hasToDateStringFormatBug = aNegativeTestDate.toDateString() !== 'Tue Jan 02 -45875';
  1044. hasToStringFormatBug = !(/^Thu Dec 10 2015 \d\d:\d\d:\d\d GMT[-+]\d\d\d\d(?: |$)/).test(String(aPositiveTestDate));
  1045. } else {
  1046. hasToDateStringFormatBug = aNegativeTestDate.toDateString() !== 'Mon Jan 01 -45875';
  1047. hasToStringFormatBug = !(/^Wed Dec 09 2015 \d\d:\d\d:\d\d GMT[-+]\d\d\d\d(?: |$)/).test(String(aPositiveTestDate));
  1048. }
  1049. var originalGetFullYear = call.bind(Date.prototype.getFullYear);
  1050. var originalGetMonth = call.bind(Date.prototype.getMonth);
  1051. var originalGetDate = call.bind(Date.prototype.getDate);
  1052. var originalGetUTCFullYear = call.bind(Date.prototype.getUTCFullYear);
  1053. var originalGetUTCMonth = call.bind(Date.prototype.getUTCMonth);
  1054. var originalGetUTCDate = call.bind(Date.prototype.getUTCDate);
  1055. var originalGetUTCDay = call.bind(Date.prototype.getUTCDay);
  1056. var originalGetUTCHours = call.bind(Date.prototype.getUTCHours);
  1057. var originalGetUTCMinutes = call.bind(Date.prototype.getUTCMinutes);
  1058. var originalGetUTCSeconds = call.bind(Date.prototype.getUTCSeconds);
  1059. var originalGetUTCMilliseconds = call.bind(Date.prototype.getUTCMilliseconds);
  1060. var dayName = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
  1061. var monthName = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  1062. var daysInMonth = function daysInMonth(month, year) {
  1063. return originalGetDate(new Date(year, month, 0));
  1064. };
  1065. defineProperties(Date.prototype, {
  1066. getFullYear: function getFullYear() {
  1067. if (!this || !(this instanceof Date)) {
  1068. throw new TypeError('this is not a Date object.');
  1069. }
  1070. var year = originalGetFullYear(this);
  1071. if (year < 0 && originalGetMonth(this) > 11) {
  1072. return year + 1;
  1073. }
  1074. return year;
  1075. },
  1076. getMonth: function getMonth() {
  1077. if (!this || !(this instanceof Date)) {
  1078. throw new TypeError('this is not a Date object.');
  1079. }
  1080. var year = originalGetFullYear(this);
  1081. var month = originalGetMonth(this);
  1082. if (year < 0 && month > 11) {
  1083. return 0;
  1084. }
  1085. return month;
  1086. },
  1087. getDate: function getDate() {
  1088. if (!this || !(this instanceof Date)) {
  1089. throw new TypeError('this is not a Date object.');
  1090. }
  1091. var year = originalGetFullYear(this);
  1092. var month = originalGetMonth(this);
  1093. var date = originalGetDate(this);
  1094. if (year < 0 && month > 11) {
  1095. if (month === 12) {
  1096. return date;
  1097. }
  1098. var days = daysInMonth(0, year + 1);
  1099. return (days - date) + 1;
  1100. }
  1101. return date;
  1102. },
  1103. getUTCFullYear: function getUTCFullYear() {
  1104. if (!this || !(this instanceof Date)) {
  1105. throw new TypeError('this is not a Date object.');
  1106. }
  1107. var year = originalGetUTCFullYear(this);
  1108. if (year < 0 && originalGetUTCMonth(this) > 11) {
  1109. return year + 1;
  1110. }
  1111. return year;
  1112. },
  1113. getUTCMonth: function getUTCMonth() {
  1114. if (!this || !(this instanceof Date)) {
  1115. throw new TypeError('this is not a Date object.');
  1116. }
  1117. var year = originalGetUTCFullYear(this);
  1118. var month = originalGetUTCMonth(this);
  1119. if (year < 0 && month > 11) {
  1120. return 0;
  1121. }
  1122. return month;
  1123. },
  1124. getUTCDate: function getUTCDate() {
  1125. if (!this || !(this instanceof Date)) {
  1126. throw new TypeError('this is not a Date object.');
  1127. }
  1128. var year = originalGetUTCFullYear(this);
  1129. var month = originalGetUTCMonth(this);
  1130. var date = originalGetUTCDate(this);
  1131. if (year < 0 && month > 11) {
  1132. if (month === 12) {
  1133. return date;
  1134. }
  1135. var days = daysInMonth(0, year + 1);
  1136. return (days - date) + 1;
  1137. }
  1138. return date;
  1139. }
  1140. }, hasNegativeMonthYearBug);
  1141. defineProperties(Date.prototype, {
  1142. toUTCString: function toUTCString() {
  1143. if (!this || !(this instanceof Date)) {
  1144. throw new TypeError('this is not a Date object.');
  1145. }
  1146. var day = originalGetUTCDay(this);
  1147. var date = originalGetUTCDate(this);
  1148. var month = originalGetUTCMonth(this);
  1149. var year = originalGetUTCFullYear(this);
  1150. var hour = originalGetUTCHours(this);
  1151. var minute = originalGetUTCMinutes(this);
  1152. var second = originalGetUTCSeconds(this);
  1153. return dayName[day] + ', '
  1154. + (date < 10 ? '0' + date : date) + ' '
  1155. + monthName[month] + ' '
  1156. + year + ' '
  1157. + (hour < 10 ? '0' + hour : hour) + ':'
  1158. + (minute < 10 ? '0' + minute : minute) + ':'
  1159. + (second < 10 ? '0' + second : second) + ' GMT';
  1160. }
  1161. }, hasNegativeMonthYearBug || hasToUTCStringFormatBug);
  1162. // Opera 12 has `,`
  1163. defineProperties(Date.prototype, {
  1164. toDateString: function toDateString() {
  1165. if (!this || !(this instanceof Date)) {
  1166. throw new TypeError('this is not a Date object.');
  1167. }
  1168. var day = this.getDay();
  1169. var date = this.getDate();
  1170. var month = this.getMonth();
  1171. var year = this.getFullYear();
  1172. return dayName[day] + ' '
  1173. + monthName[month] + ' '
  1174. + (date < 10 ? '0' + date : date) + ' '
  1175. + year;
  1176. }
  1177. }, hasNegativeMonthYearBug || hasToDateStringFormatBug);
  1178. // can't use defineProperties here because of toString enumeration issue in IE <= 8
  1179. if (hasNegativeMonthYearBug || hasToStringFormatBug) {
  1180. Date.prototype.toString = function toString() {
  1181. if (!this || !(this instanceof Date)) {
  1182. throw new TypeError('this is not a Date object.');
  1183. }
  1184. var day = this.getDay();
  1185. var date = this.getDate();
  1186. var month = this.getMonth();
  1187. var year = this.getFullYear();
  1188. var hour = this.getHours();
  1189. var minute = this.getMinutes();
  1190. var second = this.getSeconds();
  1191. var timezoneOffset = this.getTimezoneOffset();
  1192. var hoursOffset = Math.floor(Math.abs(timezoneOffset) / 60);
  1193. var minutesOffset = Math.floor(Math.abs(timezoneOffset) % 60);
  1194. return dayName[day] + ' '
  1195. + monthName[month] + ' '
  1196. + (date < 10 ? '0' + date : date) + ' '
  1197. + year + ' '
  1198. + (hour < 10 ? '0' + hour : hour) + ':'
  1199. + (minute < 10 ? '0' + minute : minute) + ':'
  1200. + (second < 10 ? '0' + second : second) + ' GMT'
  1201. + (timezoneOffset > 0 ? '-' : '+')
  1202. + (hoursOffset < 10 ? '0' + hoursOffset : hoursOffset)
  1203. + (minutesOffset < 10 ? '0' + minutesOffset : minutesOffset);
  1204. };
  1205. if (supportsDescriptors) {
  1206. $Object.defineProperty(Date.prototype, 'toString', {
  1207. configurable: true,
  1208. enumerable: false,
  1209. writable: true
  1210. });
  1211. }
  1212. }
  1213. // ES5 15.9.5.43
  1214. // http://es5.github.com/#x15.9.5.43
  1215. // This function returns a String value represent the instance in time
  1216. // represented by this Date object. The format of the String is the Date Time
  1217. // string format defined in 15.9.1.15. All fields are present in the String.
  1218. // The time zone is always UTC, denoted by the suffix Z. If the time value of
  1219. // this object is not a finite Number a RangeError exception is thrown.
  1220. var negativeDate = -62198755200000;
  1221. var negativeYearString = '-000001';
  1222. var hasNegativeDateBug = Date.prototype.toISOString && new Date(negativeDate).toISOString().indexOf(negativeYearString) === -1; // eslint-disable-line max-len
  1223. var hasSafari51DateBug = Date.prototype.toISOString && new Date(-1).toISOString() !== '1969-12-31T23:59:59.999Z';
  1224. var getTime = call.bind(Date.prototype.getTime);
  1225. defineProperties(Date.prototype, {
  1226. toISOString: function toISOString() {
  1227. if (!isFinite(this) || !isFinite(getTime(this))) {
  1228. // Adope Photoshop requires the second check.
  1229. throw new RangeError('Date.prototype.toISOString called on non-finite value.');
  1230. }
  1231. var year = originalGetUTCFullYear(this);
  1232. var month = originalGetUTCMonth(this);
  1233. // see https://github.com/es-shims/es5-shim/issues/111
  1234. year += Math.floor(month / 12);
  1235. month = ((month % 12) + 12) % 12;
  1236. // the date time string format is specified in 15.9.1.15.
  1237. var result = [
  1238. month + 1,
  1239. originalGetUTCDate(this),
  1240. originalGetUTCHours(this),
  1241. originalGetUTCMinutes(this),
  1242. originalGetUTCSeconds(this)
  1243. ];
  1244. year = (
  1245. (year < 0 ? '-' : (year > 9999 ? '+' : ''))
  1246. + strSlice('00000' + Math.abs(year), (0 <= year && year <= 9999) ? -4 : -6)
  1247. );
  1248. for (var i = 0; i < result.length; ++i) {
  1249. // pad months, days, hours, minutes, and seconds to have two digits.
  1250. result[i] = strSlice('00' + result[i], -2);
  1251. }
  1252. // pad milliseconds to have three digits.
  1253. return (
  1254. year + '-' + arraySlice(result, 0, 2).join('-')
  1255. + 'T' + arraySlice(result, 2).join(':') + '.'
  1256. + strSlice('000' + originalGetUTCMilliseconds(this), -3) + 'Z'
  1257. );
  1258. }
  1259. }, hasNegativeDateBug || hasSafari51DateBug);
  1260. // ES5 15.9.5.44
  1261. // http://es5.github.com/#x15.9.5.44
  1262. // This function provides a String representation of a Date object for use by
  1263. // JSON.stringify (15.12.3).
  1264. var dateToJSONIsSupported = (function () {
  1265. try {
  1266. return Date.prototype.toJSON
  1267. && new Date(NaN).toJSON() === null
  1268. && new Date(negativeDate).toJSON().indexOf(negativeYearString) !== -1
  1269. && Date.prototype.toJSON.call({ // generic
  1270. toISOString: function () { return true; }
  1271. });
  1272. } catch (e) {
  1273. return false;
  1274. }
  1275. }());
  1276. if (!dateToJSONIsSupported) {
  1277. Date.prototype.toJSON = function toJSON(key) {
  1278. // When the toJSON method is called with argument key, the following
  1279. // steps are taken:
  1280. // 1. Let O be the result of calling ToObject, giving it the this
  1281. // value as its argument.
  1282. // 2. Let tv be ES.ToPrimitive(O, hint Number).
  1283. var O = $Object(this);
  1284. var tv = ES.ToPrimitive(O);
  1285. // 3. If tv is a Number and is not finite, return null.
  1286. if (typeof tv === 'number' && !isFinite(tv)) {
  1287. return null;
  1288. }
  1289. // 4. Let toISO be the result of calling the [[Get]] internal method of
  1290. // O with argument "toISOString".
  1291. var toISO = O.toISOString;
  1292. // 5. If IsCallable(toISO) is false, throw a TypeError exception.
  1293. if (!isCallable(toISO)) {
  1294. throw new TypeError('toISOString property is not callable');
  1295. }
  1296. // 6. Return the result of calling the [[Call]] internal method of
  1297. // toISO with O as the this value and an empty argument list.
  1298. return toISO.call(O);
  1299. // NOTE 1 The argument is ignored.
  1300. // NOTE 2 The toJSON function is intentionally generic; it does not
  1301. // require that its this value be a Date object. Therefore, it can be
  1302. // transferred to other kinds of objects for use as a method. However,
  1303. // it does require that any such object have a toISOString method. An
  1304. // object is free to use the argument key to filter its
  1305. // stringification.
  1306. };
  1307. }
  1308. // ES5 15.9.4.2
  1309. // http://es5.github.com/#x15.9.4.2
  1310. // based on work shared by Daniel Friesen (dantman)
  1311. // http://gist.github.com/303249
  1312. var supportsExtendedYears = Date.parse('+033658-09-27T01:46:40.000Z') === 1e15;
  1313. var acceptsInvalidDates = !isNaN(Date.parse('2012-04-04T24:00:00.500Z')) || !isNaN(Date.parse('2012-11-31T23:59:59.000Z')) || !isNaN(Date.parse('2012-12-31T23:59:60.000Z'));
  1314. var doesNotParseY2KNewYear = isNaN(Date.parse('2000-01-01T00:00:00.000Z'));
  1315. if (doesNotParseY2KNewYear || acceptsInvalidDates || !supportsExtendedYears) {
  1316. // XXX global assignment won't work in embeddings that use
  1317. // an alternate object for the context.
  1318. /* global Date: true */
  1319. var maxSafeUnsigned32Bit = Math.pow(2, 31) - 1;
  1320. var hasSafariSignedIntBug = isActualNaN(new Date(1970, 0, 1, 0, 0, 0, maxSafeUnsigned32Bit + 1).getTime());
  1321. // eslint-disable-next-line no-implicit-globals, no-global-assign
  1322. Date = (function (NativeDate) {
  1323. // Date.length === 7
  1324. var DateShim = function Date(Y, M, D, h, m, s, ms) {
  1325. var length = arguments.length;
  1326. var date;
  1327. if (this instanceof NativeDate) {
  1328. var seconds = s;
  1329. var millis = ms;
  1330. if (hasSafariSignedIntBug && length >= 7 && ms > maxSafeUnsigned32Bit) {
  1331. // work around a Safari 8/9 bug where it treats the seconds as signed
  1332. var msToShift = Math.floor(ms / maxSafeUnsigned32Bit) * maxSafeUnsigned32Bit;
  1333. var sToShift = Math.floor(msToShift / 1e3);
  1334. seconds += sToShift;
  1335. millis -= sToShift * 1e3;
  1336. }
  1337. date = length === 1 && $String(Y) === Y // isString(Y)
  1338. // We explicitly pass it through parse:
  1339. ? new NativeDate(DateShim.parse(Y))
  1340. // We have to manually make calls depending on argument
  1341. // length here
  1342. : length >= 7 ? new NativeDate(Y, M, D, h, m, seconds, millis)
  1343. : length >= 6 ? new NativeDate(Y, M, D, h, m, seconds)
  1344. : length >= 5 ? new NativeDate(Y, M, D, h, m)
  1345. : length >= 4 ? new NativeDate(Y, M, D, h)
  1346. : length >= 3 ? new NativeDate(Y, M, D)
  1347. : length >= 2 ? new NativeDate(Y, M)
  1348. : length >= 1 ? new NativeDate(Y instanceof NativeDate ? +Y : Y)
  1349. : new NativeDate();
  1350. } else {
  1351. date = NativeDate.apply(this, arguments);
  1352. }
  1353. if (!isPrimitive(date)) {
  1354. // Prevent mixups with unfixed Date object
  1355. defineProperties(date, { constructor: DateShim }, true);
  1356. }
  1357. return date;
  1358. };
  1359. // 15.9.1.15 Date Time String Format.
  1360. var isoDateExpression = new RegExp('^'
  1361. + '(\\d{4}|[+-]\\d{6})' // four-digit year capture or sign + 6-digit extended year
  1362. + '(?:-(\\d{2})' // optional month capture
  1363. + '(?:-(\\d{2})' // optional day capture
  1364. + '(?:' // capture hours:minutes:seconds.milliseconds
  1365. + 'T(\\d{2})' // hours capture
  1366. + ':(\\d{2})' // minutes capture
  1367. + '(?:' // optional :seconds.milliseconds
  1368. + ':(\\d{2})' // seconds capture
  1369. + '(?:(\\.\\d{1,}))?' // milliseconds capture
  1370. + ')?'
  1371. + '(' // capture UTC offset component
  1372. + 'Z|' // UTC capture
  1373. + '(?:' // offset specifier +/-hours:minutes
  1374. + '([-+])' // sign capture
  1375. + '(\\d{2})' // hours offset capture
  1376. + ':(\\d{2})' // minutes offset capture
  1377. + ')'
  1378. + ')?)?)?)?'
  1379. + '$');
  1380. var months = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365];
  1381. var dayFromMonth = function dayFromMonth(year, month) {
  1382. var t = month > 1 ? 1 : 0;
  1383. return (
  1384. months[month]
  1385. + Math.floor((year - 1969 + t) / 4)
  1386. - Math.floor((year - 1901 + t) / 100)
  1387. + Math.floor((year - 1601 + t) / 400)
  1388. + (365 * (year - 1970))
  1389. );
  1390. };
  1391. var toUTC = function toUTC(t) {
  1392. var s = 0;
  1393. var ms = t;
  1394. if (hasSafariSignedIntBug && ms > maxSafeUnsigned32Bit) {
  1395. // work around a Safari 8/9 bug where it treats the seconds as signed
  1396. var msToShift = Math.floor(ms / maxSafeUnsigned32Bit) * maxSafeUnsigned32Bit;
  1397. var sToShift = Math.floor(msToShift / 1e3);
  1398. s += sToShift;
  1399. ms -= sToShift * 1e3;
  1400. }
  1401. return $Number(new NativeDate(1970, 0, 1, 0, 0, s, ms));
  1402. };
  1403. // Copy any custom methods a 3rd party library may have added
  1404. for (var key in NativeDate) {
  1405. if (owns(NativeDate, key)) {
  1406. DateShim[key] = NativeDate[key];
  1407. }
  1408. }
  1409. // Copy "native" methods explicitly; they may be non-enumerable
  1410. defineProperties(DateShim, {
  1411. now: NativeDate.now,
  1412. UTC: NativeDate.UTC
  1413. }, true);
  1414. DateShim.prototype = NativeDate.prototype;
  1415. defineProperties(DateShim.prototype, { constructor: DateShim }, true);
  1416. // Upgrade Date.parse to handle simplified ISO 8601 strings
  1417. var parseShim = function parse(string) {
  1418. var match = isoDateExpression.exec(string);
  1419. if (match) {
  1420. // parse months, days, hours, minutes, seconds, and milliseconds
  1421. // provide default values if necessary
  1422. // parse the UTC offset component
  1423. var year = $Number(match[1]),
  1424. month = $Number(match[2] || 1) - 1,
  1425. day = $Number(match[3] || 1) - 1,
  1426. hour = $Number(match[4] || 0),
  1427. minute = $Number(match[5] || 0),
  1428. second = $Number(match[6] || 0),
  1429. millisecond = Math.floor($Number(match[7] || 0) * 1000),
  1430. // When time zone is missed, local offset should be used
  1431. // (ES 5.1 bug)
  1432. // see https://bugs.ecmascript.org/show_bug.cgi?id=112
  1433. isLocalTime = Boolean(match[4] && !match[8]),
  1434. signOffset = match[9] === '-' ? 1 : -1,
  1435. hourOffset = $Number(match[10] || 0),
  1436. minuteOffset = $Number(match[11] || 0),
  1437. result;
  1438. var hasMinutesOrSecondsOrMilliseconds = minute > 0 || second > 0 || millisecond > 0;
  1439. if (
  1440. hour < (hasMinutesOrSecondsOrMilliseconds ? 24 : 25)
  1441. && minute < 60 && second < 60 && millisecond < 1000
  1442. && month > -1 && month < 12 && hourOffset < 24
  1443. && minuteOffset < 60 // detect invalid offsets
  1444. && day > -1
  1445. && day < (dayFromMonth(year, month + 1) - dayFromMonth(year, month))
  1446. ) {
  1447. result = (
  1448. ((dayFromMonth(year, month) + day) * 24)
  1449. + hour
  1450. + (hourOffset * signOffset)
  1451. ) * 60;
  1452. result = ((
  1453. ((result + minute + (minuteOffset * signOffset)) * 60)
  1454. + second
  1455. ) * 1000) + millisecond;
  1456. if (isLocalTime) {
  1457. result = toUTC(result);
  1458. }
  1459. if (-8.64e15 <= result && result <= 8.64e15) {
  1460. return result;
  1461. }
  1462. }
  1463. return NaN;
  1464. }
  1465. return NativeDate.parse.apply(this, arguments);
  1466. };
  1467. defineProperties(DateShim, { parse: parseShim });
  1468. return DateShim;
  1469. }(Date));
  1470. /* global Date: false */
  1471. }
  1472. // ES5 15.9.4.4
  1473. // http://es5.github.com/#x15.9.4.4
  1474. if (!Date.now) {
  1475. Date.now = function now() {
  1476. return new Date().getTime();
  1477. };
  1478. }
  1479. //
  1480. // Number
  1481. // ======
  1482. //
  1483. // ES5.1 15.7.4.5
  1484. // http://es5.github.com/#x15.7.4.5
  1485. var hasToFixedBugs = NumberPrototype.toFixed && (
  1486. (0.00008).toFixed(3) !== '0.000'
  1487. || (0.9).toFixed(0) !== '1'
  1488. || (1.255).toFixed(2) !== '1.25'
  1489. || (1000000000000000128).toFixed(0) !== '1000000000000000128'
  1490. );
  1491. var toFixedHelpers = {
  1492. base: 1e7,
  1493. size: 6,
  1494. data: [0, 0, 0, 0, 0, 0],
  1495. multiply: function multiply(n, c) {
  1496. var i = -1;
  1497. var c2 = c;
  1498. while (++i < toFixedHelpers.size) {
  1499. c2 += n * toFixedHelpers.data[i];
  1500. toFixedHelpers.data[i] = c2 % toFixedHelpers.base;
  1501. c2 = Math.floor(c2 / toFixedHelpers.base);
  1502. }
  1503. },
  1504. divide: function divide(n) {
  1505. var i = toFixedHelpers.size;
  1506. var c = 0;
  1507. while (--i >= 0) {
  1508. c += toFixedHelpers.data[i];
  1509. toFixedHelpers.data[i] = Math.floor(c / n);
  1510. c = (c % n) * toFixedHelpers.base;
  1511. }
  1512. },
  1513. numToString: function numToString() {
  1514. var i = toFixedHelpers.size;
  1515. var s = '';
  1516. while (--i >= 0) {
  1517. if (s !== '' || i === 0 || toFixedHelpers.data[i] !== 0) {
  1518. var t = $String(toFixedHelpers.data[i]);
  1519. if (s === '') {
  1520. s = t;
  1521. } else {
  1522. s += strSlice('0000000', 0, 7 - t.length) + t;
  1523. }
  1524. }
  1525. }
  1526. return s;
  1527. },
  1528. pow: function pow(x, n, acc) {
  1529. return (n === 0 ? acc : (n % 2 === 1 ? pow(x, n - 1, acc * x) : pow(x * x, n / 2, acc)));
  1530. },
  1531. log: function log(x) {
  1532. var n = 0;
  1533. var x2 = x;
  1534. while (x2 >= 4096) {
  1535. n += 12;
  1536. x2 /= 4096;
  1537. }
  1538. while (x2 >= 2) {
  1539. n += 1;
  1540. x2 /= 2;
  1541. }
  1542. return n;
  1543. }
  1544. };
  1545. var toFixedShim = function toFixed(fractionDigits) {
  1546. var f, x, s, m, e, z, j, k;
  1547. // Test for NaN and round fractionDigits down
  1548. f = $Number(fractionDigits);
  1549. f = isActualNaN(f) ? 0 : Math.floor(f);
  1550. if (f < 0 || f > 20) {
  1551. throw new RangeError('Number.toFixed called with invalid number of decimals');
  1552. }
  1553. x = $Number(this);
  1554. if (isActualNaN(x)) {
  1555. return 'NaN';
  1556. }
  1557. // If it is too big or small, return the string value of the number
  1558. if (x <= -1e21 || x >= 1e21) {
  1559. return $String(x);
  1560. }
  1561. s = '';
  1562. if (x < 0) {
  1563. s = '-';
  1564. x = -x;
  1565. }
  1566. m = '0';
  1567. if (x > 1e-21) {
  1568. // 1e-21 < x < 1e21
  1569. // -70 < log2(x) < 70
  1570. e = toFixedHelpers.log(x * toFixedHelpers.pow(2, 69, 1)) - 69;
  1571. z = (e < 0 ? x * toFixedHelpers.pow(2, -e, 1) : x / toFixedHelpers.pow(2, e, 1));
  1572. z *= 0x10000000000000; // Math.pow(2, 52);
  1573. e = 52 - e;
  1574. // -18 < e < 122
  1575. // x = z / 2 ^ e
  1576. if (e > 0) {
  1577. toFixedHelpers.multiply(0, z);
  1578. j = f;
  1579. while (j >= 7) {
  1580. toFixedHelpers.multiply(1e7, 0);
  1581. j -= 7;
  1582. }
  1583. toFixedHelpers.multiply(toFixedHelpers.pow(10, j, 1), 0);
  1584. j = e - 1;
  1585. while (j >= 23) {
  1586. toFixedHelpers.divide(1 << 23);
  1587. j -= 23;
  1588. }
  1589. toFixedHelpers.divide(1 << j);
  1590. toFixedHelpers.multiply(1, 1);
  1591. toFixedHelpers.divide(2);
  1592. m = toFixedHelpers.numToString();
  1593. } else {
  1594. toFixedHelpers.multiply(0, z);
  1595. toFixedHelpers.multiply(1 << (-e), 0);
  1596. m = toFixedHelpers.numToString() + strSlice('0.00000000000000000000', 2, 2 + f);
  1597. }
  1598. }
  1599. if (f > 0) {
  1600. k = m.length;
  1601. if (k <= f) {
  1602. m = s + strSlice('0.0000000000000000000', 0, f - k + 2) + m;
  1603. } else {
  1604. m = s + strSlice(m, 0, k - f) + '.' + strSlice(m, k - f);
  1605. }
  1606. } else {
  1607. m = s + m;
  1608. }
  1609. return m;
  1610. };
  1611. defineProperties(NumberPrototype, { toFixed: toFixedShim }, hasToFixedBugs);
  1612. var hasToPrecisionUndefinedBug = (function () {
  1613. try {
  1614. return 1.0.toPrecision(undefined) === '1';
  1615. } catch (e) {
  1616. return true;
  1617. }
  1618. }());
  1619. var originalToPrecision = NumberPrototype.toPrecision;
  1620. defineProperties(NumberPrototype, {
  1621. toPrecision: function toPrecision(precision) {
  1622. return typeof precision === 'undefined' ? originalToPrecision.call(this) : originalToPrecision.call(this, precision);
  1623. }
  1624. }, hasToPrecisionUndefinedBug);
  1625. //
  1626. // String
  1627. // ======
  1628. //
  1629. // ES5 15.5.4.14
  1630. // http://es5.github.com/#x15.5.4.14
  1631. // [bugfix, IE lt 9, firefox 4, Konqueror, Opera, obscure browsers]
  1632. // Many browsers do not split properly with regular expressions or they
  1633. // do not perform the split correctly under obscure conditions.
  1634. // See http://blog.stevenlevithan.com/archives/cross-browser-split
  1635. // I've tested in many browsers and this seems to cover the deviant ones:
  1636. // 'ab'.split(/(?:ab)*/) should be ["", ""], not [""]
  1637. // '.'.split(/(.?)(.?)/) should be ["", ".", "", ""], not ["", ""]
  1638. // 'tesst'.split(/(s)*/) should be ["t", undefined, "e", "s", "t"], not
  1639. // [undefined, "t", undefined, "e", ...]
  1640. // ''.split(/.?/) should be [], not [""]
  1641. // '.'.split(/()()/) should be ["."], not ["", "", "."]
  1642. if (
  1643. 'ab'.split(/(?:ab)*/).length !== 2
  1644. || '.'.split(/(.?)(.?)/).length !== 4
  1645. || 'tesst'.split(/(s)*/)[1] === 't'
  1646. || 'test'.split(/(?:)/, -1).length !== 4
  1647. || ''.split(/.?/).length
  1648. || '.'.split(/()()/).length > 1
  1649. ) {
  1650. (function () {
  1651. var compliantExecNpcg = typeof (/()??/).exec('')[1] === 'undefined'; // NPCG: nonparticipating capturing group
  1652. var maxSafe32BitInt = Math.pow(2, 32) - 1;
  1653. StringPrototype.split = function (separator, limit) {
  1654. var string = String(this);
  1655. if (typeof separator === 'undefined' && limit === 0) {
  1656. return [];
  1657. }
  1658. // If `separator` is not a regex, use native split
  1659. if (!isRegex(separator)) {
  1660. return strSplit(this, separator, limit);
  1661. }
  1662. var output = [];
  1663. var flags = (separator.ignoreCase ? 'i' : '')
  1664. + (separator.multiline ? 'm' : '')
  1665. + (separator.unicode ? 'u' : '') // in ES6
  1666. + (separator.sticky ? 'y' : ''), // Firefox 3+ and ES6
  1667. lastLastIndex = 0,
  1668. // Make `global` and avoid `lastIndex` issues by working with a copy
  1669. separator2, match, lastIndex, lastLength;
  1670. var separatorCopy = new RegExp(separator.source, flags + 'g');
  1671. if (!compliantExecNpcg) {
  1672. // Doesn't need flags gy, but they don't hurt
  1673. separator2 = new RegExp('^' + separatorCopy.source + '$(?!\\s)', flags);
  1674. }
  1675. /* Values for `limit`, per the spec:
  1676. * If undefined: 4294967295 // maxSafe32BitInt
  1677. * If 0, Infinity, or NaN: 0
  1678. * If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296;
  1679. * If negative number: 4294967296 - Math.floor(Math.abs(limit))
  1680. * If other: Type-convert, then use the above rules
  1681. */
  1682. var splitLimit = typeof limit === 'undefined' ? maxSafe32BitInt : ES.ToUint32(limit);
  1683. match = separatorCopy.exec(string);
  1684. while (match) {
  1685. // `separatorCopy.lastIndex` is not reliable cross-browser
  1686. lastIndex = match.index + match[0].length;
  1687. if (lastIndex > lastLastIndex) {
  1688. pushCall(output, strSlice(string, lastLastIndex, match.index));
  1689. // Fix browsers whose `exec` methods don't consistently return `undefined` for
  1690. // nonparticipating capturing groups
  1691. if (!compliantExecNpcg && match.length > 1) {
  1692. /* eslint-disable no-loop-func */
  1693. match[0].replace(separator2, function () {
  1694. for (var i = 1; i < arguments.length - 2; i++) {
  1695. if (typeof arguments[i] === 'undefined') {
  1696. match[i] = void 0;
  1697. }
  1698. }
  1699. });
  1700. /* eslint-enable no-loop-func */
  1701. }
  1702. if (match.length > 1 && match.index < string.length) {
  1703. array_push.apply(output, arraySlice(match, 1));
  1704. }
  1705. lastLength = match[0].length;
  1706. lastLastIndex = lastIndex;
  1707. if (output.length >= splitLimit) {
  1708. break;
  1709. }
  1710. }
  1711. if (separatorCopy.lastIndex === match.index) {
  1712. separatorCopy.lastIndex++; // Avoid an infinite loop
  1713. }
  1714. match = separatorCopy.exec(string);
  1715. }
  1716. if (lastLastIndex === string.length) {
  1717. if (lastLength || !separatorCopy.test('')) {
  1718. pushCall(output, '');
  1719. }
  1720. } else {
  1721. pushCall(output, strSlice(string, lastLastIndex));
  1722. }
  1723. return output.length > splitLimit ? arraySlice(output, 0, splitLimit) : output;
  1724. };
  1725. }());
  1726. // [bugfix, chrome]
  1727. // If separator is undefined, then the result array contains just one String,
  1728. // which is the this value (converted to a String). If limit is not undefined,
  1729. // then the output array is truncated so that it contains no more than limit
  1730. // elements.
  1731. // "0".split(undefined, 0) -> []
  1732. } else if ('0'.split(void 0, 0).length) {
  1733. StringPrototype.split = function split(separator, limit) {
  1734. if (typeof separator === 'undefined' && limit === 0) {
  1735. return [];
  1736. }
  1737. return strSplit(this, separator, limit);
  1738. };
  1739. }
  1740. var str_replace = StringPrototype.replace;
  1741. var replaceReportsGroupsCorrectly = (function () {
  1742. var groups = [];
  1743. 'x'.replace(/x(.)?/g, function (match, group) {
  1744. pushCall(groups, group);
  1745. });
  1746. return groups.length === 1 && typeof groups[0] === 'undefined';
  1747. }());
  1748. if (!replaceReportsGroupsCorrectly) {
  1749. StringPrototype.replace = function replace(searchValue, replaceValue) {
  1750. var isFn = isCallable(replaceValue);
  1751. var hasCapturingGroups = isRegex(searchValue) && (/\)[*?]/).test(searchValue.source);
  1752. if (!isFn || !hasCapturingGroups) {
  1753. return str_replace.call(this, searchValue, replaceValue);
  1754. } else {
  1755. var wrappedReplaceValue = function (match) {
  1756. var length = arguments.length;
  1757. var originalLastIndex = searchValue.lastIndex;
  1758. searchValue.lastIndex = 0;
  1759. var args = searchValue.exec(match) || [];
  1760. searchValue.lastIndex = originalLastIndex;
  1761. pushCall(args, arguments[length - 2], arguments[length - 1]);
  1762. return replaceValue.apply(this, args);
  1763. };
  1764. return str_replace.call(this, searchValue, wrappedReplaceValue);
  1765. }
  1766. };
  1767. }
  1768. // ECMA-262, 3rd B.2.3
  1769. // Not an ECMAScript standard, although ECMAScript 3rd Edition has a
  1770. // non-normative section suggesting uniform semantics and it should be
  1771. // normalized across all browsers
  1772. // [bugfix, IE lt 9] IE < 9 substr() with negative value not working in IE
  1773. var string_substr = StringPrototype.substr;
  1774. var hasNegativeSubstrBug = ''.substr && '0b'.substr(-1) !== 'b';
  1775. defineProperties(StringPrototype, {
  1776. substr: function substr(start, length) {
  1777. var normalizedStart = start;
  1778. if (start < 0) {
  1779. normalizedStart = max(this.length + start, 0);
  1780. }
  1781. return string_substr.call(this, normalizedStart, length);
  1782. }
  1783. }, hasNegativeSubstrBug);
  1784. // ES5 15.5.4.20
  1785. // whitespace from: http://es5.github.io/#x15.5.4.20
  1786. var ws = '\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003'
  1787. + '\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028'
  1788. + '\u2029\uFEFF';
  1789. var zeroWidth = '\u200b';
  1790. var wsRegexChars = '[' + ws + ']';
  1791. var trimBeginRegexp = new RegExp('^' + wsRegexChars + wsRegexChars + '*');
  1792. var trimEndRegexp = new RegExp(wsRegexChars + wsRegexChars + '*$');
  1793. var hasTrimWhitespaceBug = StringPrototype.trim && (ws.trim() || !zeroWidth.trim());
  1794. defineProperties(StringPrototype, {
  1795. // http://blog.stevenlevithan.com/archives/faster-trim-javascript
  1796. // http://perfectionkills.com/whitespace-deviations/
  1797. trim: function trim() {
  1798. if (typeof this === 'undefined' || this === null) {
  1799. throw new TypeError("can't convert " + this + ' to object');
  1800. }
  1801. return $String(this).replace(trimBeginRegexp, '').replace(trimEndRegexp, '');
  1802. }
  1803. }, hasTrimWhitespaceBug);
  1804. var trim = call.bind(String.prototype.trim);
  1805. var hasLastIndexBug = StringPrototype.lastIndexOf && 'abcあい'.lastIndexOf('あい', 2) !== -1;
  1806. defineProperties(StringPrototype, {
  1807. lastIndexOf: function lastIndexOf(searchString) {
  1808. if (typeof this === 'undefined' || this === null) {
  1809. throw new TypeError("can't convert " + this + ' to object');
  1810. }
  1811. var S = $String(this);
  1812. var searchStr = $String(searchString);
  1813. var numPos = arguments.length > 1 ? $Number(arguments[1]) : NaN;
  1814. var pos = isActualNaN(numPos) ? Infinity : ES.ToInteger(numPos);
  1815. var start = min(max(pos, 0), S.length);
  1816. var searchLen = searchStr.length;
  1817. var k = start + searchLen;
  1818. while (k > 0) {
  1819. k = max(0, k - searchLen);
  1820. var index = strIndexOf(strSlice(S, k, start + searchLen), searchStr);
  1821. if (index !== -1) {
  1822. return k + index;
  1823. }
  1824. }
  1825. return -1;
  1826. }
  1827. }, hasLastIndexBug);
  1828. var originalLastIndexOf = StringPrototype.lastIndexOf;
  1829. defineProperties(StringPrototype, {
  1830. lastIndexOf: function lastIndexOf(searchString) {
  1831. return originalLastIndexOf.apply(this, arguments);
  1832. }
  1833. }, StringPrototype.lastIndexOf.length !== 1);
  1834. // ES-5 15.1.2.2
  1835. // eslint-disable-next-line radix
  1836. if (parseInt(ws + '08') !== 8 || parseInt(ws + '0x16') !== 22) {
  1837. /* global parseInt: true */
  1838. parseInt = (function (origParseInt) {
  1839. var hexRegex = /^[-+]?0[xX]/;
  1840. return function parseInt(str, radix) {
  1841. if (typeof str === 'symbol') {
  1842. // handle Symbols in node 8.3/8.4
  1843. // eslint-disable-next-line no-implicit-coercion, no-unused-expressions
  1844. '' + str; // jscs:ignore disallowImplicitTypeConversion
  1845. }
  1846. var string = trim(String(str));
  1847. var defaultedRadix = $Number(radix) || (hexRegex.test(string) ? 16 : 10);
  1848. return origParseInt(string, defaultedRadix);
  1849. };
  1850. }(parseInt));
  1851. }
  1852. // https://es5.github.io/#x15.1.2.3
  1853. if (1 / parseFloat('-0') !== -Infinity) {
  1854. /* global parseFloat: true */
  1855. parseFloat = (function (origParseFloat) {
  1856. return function parseFloat(string) {
  1857. var inputString = trim(String(string));
  1858. var result = origParseFloat(inputString);
  1859. return result === 0 && strSlice(inputString, 0, 1) === '-' ? -0 : result;
  1860. };
  1861. }(parseFloat));
  1862. }
  1863. if (String(new RangeError('test')) !== 'RangeError: test') {
  1864. var errorToStringShim = function toString() {
  1865. if (typeof this === 'undefined' || this === null) {
  1866. throw new TypeError("can't convert " + this + ' to object');
  1867. }
  1868. var name = this.name;
  1869. if (typeof name === 'undefined') {
  1870. name = 'Error';
  1871. } else if (typeof name !== 'string') {
  1872. name = $String(name);
  1873. }
  1874. var msg = this.message;
  1875. if (typeof msg === 'undefined') {
  1876. msg = '';
  1877. } else if (typeof msg !== 'string') {
  1878. msg = $String(msg);
  1879. }
  1880. if (!name) {
  1881. return msg;
  1882. }
  1883. if (!msg) {
  1884. return name;
  1885. }
  1886. return name + ': ' + msg;
  1887. };
  1888. // can't use defineProperties here because of toString enumeration issue in IE <= 8
  1889. Error.prototype.toString = errorToStringShim;
  1890. }
  1891. if (supportsDescriptors) {
  1892. var ensureNonEnumerable = function (obj, prop) {
  1893. if (isEnum(obj, prop)) {
  1894. var desc = Object.getOwnPropertyDescriptor(obj, prop);
  1895. if (desc.configurable) {
  1896. desc.enumerable = false;
  1897. Object.defineProperty(obj, prop, desc);
  1898. }
  1899. }
  1900. };
  1901. ensureNonEnumerable(Error.prototype, 'message');
  1902. if (Error.prototype.message !== '') {
  1903. Error.prototype.message = '';
  1904. }
  1905. ensureNonEnumerable(Error.prototype, 'name');
  1906. }
  1907. if (String(/a/mig) !== '/a/gim') {
  1908. var regexToString = function toString() {
  1909. var str = '/' + this.source + '/';
  1910. if (this.global) {
  1911. str += 'g';
  1912. }
  1913. if (this.ignoreCase) {
  1914. str += 'i';
  1915. }
  1916. if (this.multiline) {
  1917. str += 'm';
  1918. }
  1919. return str;
  1920. };
  1921. // can't use defineProperties here because of toString enumeration issue in IE <= 8
  1922. RegExp.prototype.toString = regexToString;
  1923. }
  1924. }));