es5-shim.js 49 KB

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