es5-shim.js 52 KB

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