es5-shim.js 44 KB

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