shim.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
  2. if (!Object.keys) {
  3. Object.keys = (function () {
  4. var hasOwnProperty = Object.prototype.hasOwnProperty,
  5. hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
  6. dontEnums = [
  7. 'toString',
  8. 'toLocaleString',
  9. 'valueOf',
  10. 'hasOwnProperty',
  11. 'isPrototypeOf',
  12. 'propertyIsEnumerable',
  13. 'constructor'
  14. ],
  15. dontEnumsLength = dontEnums.length;
  16. return function (obj) {
  17. if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) throw new TypeError('Object.keys called on non-object');
  18. var result = [];
  19. for (var prop in obj) {
  20. if (hasOwnProperty.call(obj, prop)) result.push(prop);
  21. }
  22. if (hasDontEnumBug) {
  23. for (var i=0; i < dontEnumsLength; i++) {
  24. if (hasOwnProperty.call(obj, dontEnums[i])) result.push(dontEnums[i]);
  25. }
  26. }
  27. return result;
  28. };
  29. })();
  30. }
  31. // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
  32. if (!Array.prototype.filter)
  33. {
  34. Array.prototype.filter = function(fun /*, thisp */)
  35. {
  36. "use strict";
  37. if (this == null)
  38. throw new TypeError();
  39. var t = Object(this);
  40. var len = t.length >>> 0;
  41. if (typeof fun != "function")
  42. throw new TypeError();
  43. var res = [];
  44. var thisp = arguments[1];
  45. for (var i = 0; i < len; i++)
  46. {
  47. if (i in t)
  48. {
  49. var val = t[i]; // in case fun mutates this
  50. if (fun.call(thisp, val, i, t))
  51. res.push(val);
  52. }
  53. }
  54. return res;
  55. };
  56. }
  57. // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim
  58. if (!String.prototype.trim) {
  59. String.prototype.trim = function () {
  60. return this.replace(/^\s+|\s+$/g, '');
  61. };
  62. }
  63. // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
  64. if (!Array.prototype.forEach)
  65. {
  66. Array.prototype.forEach = function(fun /*, thisArg */)
  67. {
  68. "use strict";
  69. if (this === void 0 || this === null)
  70. throw new TypeError();
  71. var t = Object(this);
  72. var len = t.length >>> 0;
  73. if (typeof fun !== "function")
  74. throw new TypeError();
  75. var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
  76. for (var i = 0; i < len; i++)
  77. {
  78. if (i in t)
  79. fun.call(thisArg, t[i], i, t);
  80. }
  81. };
  82. }
  83. // Production steps of ECMA-262, Edition 5, 15.4.4.19
  84. // Reference: http://es5.github.com/#x15.4.4.19
  85. if (!Array.prototype.map) {
  86. Array.prototype.map = function(callback, thisArg) {
  87. var T, A, k;
  88. if (this == null) {
  89. throw new TypeError(" this is null or not defined");
  90. }
  91. // 1. Let O be the result of calling ToObject passing the |this| value as the argument.
  92. var O = Object(this);
  93. // 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
  94. // 3. Let len be ToUint32(lenValue).
  95. var len = O.length >>> 0;
  96. // 4. If IsCallable(callback) is false, throw a TypeError exception.
  97. // See: http://es5.github.com/#x9.11
  98. if (typeof callback !== "function") {
  99. throw new TypeError(callback + " is not a function");
  100. }
  101. // 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
  102. if (thisArg) {
  103. T = thisArg;
  104. }
  105. // 6. Let A be a new array created as if by the expression new Array(len) where Array is
  106. // the standard built-in constructor with that name and len is the value of len.
  107. A = new Array(len);
  108. // 7. Let k be 0
  109. k = 0;
  110. // 8. Repeat, while k < len
  111. while(k < len) {
  112. var kValue, mappedValue;
  113. // a. Let Pk be ToString(k).
  114. // This is implicit for LHS operands of the in operator
  115. // b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
  116. // This step can be combined with c
  117. // c. If kPresent is true, then
  118. if (k in O) {
  119. // i. Let kValue be the result of calling the Get internal method of O with argument Pk.
  120. kValue = O[ k ];
  121. // ii. Let mappedValue be the result of calling the Call internal method of callback
  122. // with T as the this value and argument list containing kValue, k, and O.
  123. mappedValue = callback.call(T, kValue, k, O);
  124. // iii. Call the DefineOwnProperty internal method of A with arguments
  125. // Pk, Property Descriptor {Value: mappedValue, : true, Enumerable: true, Configurable: true},
  126. // and false.
  127. // In browsers that support Object.defineProperty, use the following:
  128. // Object.defineProperty(A, Pk, { value: mappedValue, writable: true, enumerable: true, configurable: true });
  129. // For best browser support, use the following:
  130. A[ k ] = mappedValue;
  131. }
  132. // d. Increase k by 1.
  133. k++;
  134. }
  135. // 9. return A
  136. return A;
  137. };
  138. }
  139. // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
  140. if (!Array.prototype.indexOf) {
  141. Array.prototype.indexOf = function (searchElement, fromIndex) {
  142. if ( this === undefined || this === null ) {
  143. throw new TypeError( '"this" is null or not defined' );
  144. }
  145. var length = this.length >>> 0; // Hack to convert object.length to a UInt32
  146. fromIndex = +fromIndex || 0;
  147. if (Math.abs(fromIndex) === Infinity) {
  148. fromIndex = 0;
  149. }
  150. if (fromIndex < 0) {
  151. fromIndex += length;
  152. if (fromIndex < 0) {
  153. fromIndex = 0;
  154. }
  155. }
  156. for (;fromIndex < length; fromIndex++) {
  157. if (this[fromIndex] === searchElement) {
  158. return fromIndex;
  159. }
  160. }
  161. return -1;
  162. };
  163. }
  164. // Based on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
  165. if (! Array.isArray) {
  166. Array.isArray = function(obj) {
  167. return Object.prototype.toString.call(obj) === "[object Array]";
  168. };
  169. }
  170. // https://github.com/ttaubert/node-arraybuffer-slice
  171. // (c) 2013 Tim Taubert <[email protected]>
  172. // arraybuffer-slice may be freely distributed under the MIT license.
  173. "use strict";
  174. if (typeof ArrayBuffer !== 'undefined' && !ArrayBuffer.prototype.slice) {
  175. ArrayBuffer.prototype.slice = function (begin, end) {
  176. begin = (begin|0) || 0;
  177. var num = this.byteLength;
  178. end = end === (void 0) ? num : (end|0);
  179. // Handle negative values.
  180. if (begin < 0) begin += num;
  181. if (end < 0) end += num;
  182. if (num === 0 || begin >= num || begin >= end) {
  183. return new ArrayBuffer(0);
  184. }
  185. var length = Math.min(num - begin, end - begin);
  186. var target = new ArrayBuffer(length);
  187. var targetArray = new Uint8Array(target);
  188. targetArray.set(new Uint8Array(this, begin, length));
  189. return target;
  190. };
  191. }
  192. // https://github.com/davidchambers/Base64.js
  193. // (C) 2015 David Chambers
  194. // Base64.js may be freely distributed under the Apache 2.0 License.
  195. ;(function () {
  196. var object =
  197. typeof exports != 'undefined' ? exports :
  198. typeof self != 'undefined' ? self : // #8: web workers
  199. $.global; // #31: ExtendScript
  200. var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  201. function InvalidCharacterError(message) {
  202. this.message = message;
  203. }
  204. InvalidCharacterError.prototype = new Error;
  205. InvalidCharacterError.prototype.name = 'InvalidCharacterError';
  206. // encoder
  207. // [https://gist.github.com/999166] by [https://github.com/nignag]
  208. object.btoa || (
  209. object.btoa = function (input) {
  210. var str = String(input);
  211. for (
  212. // initialize result and counter
  213. var block, charCode, idx = 0, map = chars, output = '';
  214. // if the next str index does not exist:
  215. // change the mapping table to "="
  216. // check if d has no fractional digits
  217. str.charAt(idx | 0) || (map = '=', idx % 1);
  218. // "8 - idx % 1 * 8" generates the sequence 2, 4, 6, 8
  219. output += map.charAt(63 & block >> 8 - idx % 1 * 8)
  220. ) {
  221. charCode = str.charCodeAt(idx += 3/4);
  222. if (charCode > 0xFF) {
  223. throw new InvalidCharacterError("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.");
  224. }
  225. block = block << 8 | charCode;
  226. }
  227. return output;
  228. });
  229. // decoder
  230. // [https://gist.github.com/1020396] by [https://github.com/atk]
  231. object.atob || (
  232. object.atob = function (input) {
  233. var str = String(input).replace(/[=]+$/, ''); // #31: ExtendScript bad parse of /=
  234. if (str.length % 4 == 1) {
  235. throw new InvalidCharacterError("'atob' failed: The string to be decoded is not correctly encoded.");
  236. }
  237. for (
  238. // initialize result and counters
  239. var bc = 0, bs, buffer, idx = 0, output = '';
  240. // get next character
  241. buffer = str.charAt(idx++);
  242. // character found in table? initialize bit storage and add its ascii value;
  243. ~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer,
  244. // and if not first of each 4 characters,
  245. // convert the first 8 bits to one ascii character
  246. bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0
  247. ) {
  248. // try to find character in table (0-63, not found => -1)
  249. buffer = chars.indexOf(buffer);
  250. }
  251. return output;
  252. });
  253. }());
  254. // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
  255. if (!Date.prototype.toISOString) {
  256. (function() {
  257. function pad(number) {
  258. if (number < 10) {
  259. return '0' + number;
  260. }
  261. return number;
  262. }
  263. Date.prototype.toISOString = function() {
  264. return this.getUTCFullYear() +
  265. '-' + pad(this.getUTCMonth() + 1) +
  266. '-' + pad(this.getUTCDate()) +
  267. 'T' + pad(this.getUTCHours()) +
  268. ':' + pad(this.getUTCMinutes()) +
  269. ':' + pad(this.getUTCSeconds()) +
  270. '.' + (this.getUTCMilliseconds() / 1000).toFixed(3).slice(2, 5) +
  271. 'Z';
  272. };
  273. }());
  274. }