es5-shim.js 47 KB

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