shim.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* shim.js (C) 2013-present SheetJS -- http://sheetjs.com */
  2. /* ES3/5 Compatibility shims and other utilities for older browsers. */
  3. // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
  4. if(!Object.keys) Object.keys = (function() {
  5. var hasOwnProperty = Object.prototype.hasOwnProperty,
  6. hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
  7. dontEnums = [
  8. 'toString',
  9. 'toLocaleString',
  10. 'valueOf',
  11. 'hasOwnProperty',
  12. 'isPrototypeOf',
  13. 'propertyIsEnumerable',
  14. 'constructor'
  15. ],
  16. dontEnumsLength = dontEnums.length;
  17. return function(obj) {
  18. if(typeof obj !== 'object' && typeof obj !== 'function' || obj === null) throw new TypeError('Object.keys called on non-object');
  19. var result = [];
  20. for(var prop in obj) if(hasOwnProperty.call(obj, prop)) result.push(prop);
  21. if(hasDontEnumBug)
  22. for(var i=0; i < dontEnumsLength; ++i)
  23. if(hasOwnProperty.call(obj, dontEnums[i])) result.push(dontEnums[i]);
  24. return result;
  25. };
  26. })();
  27. if(!String.prototype.trim) String.prototype.trim = function() {
  28. var s = this.replace(/^\s+/, '');
  29. for(var i = s.length - 1; i >=0 ; --i) if(!s.charAt(i).match(/^\s/)) return s.slice(0,i+1);
  30. return "";
  31. };
  32. if(!Array.prototype.forEach) Array.prototype.forEach = function(cb) {
  33. var len = (this.length>>>0), self = (arguments[1]||void 0);
  34. for(var i=0; i<len; ++i) if(i in this) self ? cb.call(self, this[i], i, this) : cb(this[i], i, this);
  35. };
  36. if(!Array.prototype.map) Array.prototype.map = function(cb) {
  37. var len = (this.length>>>0), self = (arguments[1]||void 0), A = new Array(len);
  38. for(var i=0; i<len; ++i) if(i in this) A[i] = self ? cb.call(self, this[i], i, this) : cb(this[i], i, this);
  39. return A;
  40. };
  41. if(!Array.prototype.indexOf) Array.prototype.indexOf = function(needle) {
  42. var len = (this.length>>>0), i = ((arguments[1]|0)||0);
  43. for(i<0 && (i+=len)<0 && (i=0); i<len; ++i) if(this[i] === needle) return i;
  44. return -1;
  45. };
  46. if(!Array.prototype.lastIndexOf) Array.prototype.lastIndexOf = function(needle) {
  47. var len = (this.length>>>0), i = len - 1;
  48. for(; i>=0; --i) if(this[i] === needle) return i;
  49. return -1;
  50. };
  51. if(!Array.isArray) Array.isArray = function(obj) { return Object.prototype.toString.call(obj) === "[object Array]"; };
  52. if(!Date.prototype.toISOString) Date.prototype.toISOString = (function() {
  53. function p(n,i) { return ('0000000' + n).slice(-(i||2)); }
  54. return function _toISOString() {
  55. var y = this.getUTCFullYear(), yr = "";
  56. if(y>9999) yr = '+' + p( y, 6);
  57. else if(y<0) yr = '-' + p(-y, 6);
  58. else yr = p( y, 4);
  59. return [
  60. yr, p(this.getUTCMonth()+1), p(this.getUTCDate())
  61. ].join('-') + 'T' + [
  62. p(this.getUTCHours()), p(this.getUTCMinutes()), p(this.getUTCSeconds())
  63. ].join(':') + '.' + p(this.getUTCMilliseconds(),3) + 'Z';
  64. };
  65. }());
  66. if(typeof ArrayBuffer !== 'undefined' && !ArrayBuffer.prototype.slice) ArrayBuffer.prototype.slice = function(start, end) {
  67. if(start == null) start = 0;
  68. if(start < 0) { start += this.byteLength; if(start < 0) start = 0; }
  69. if(start >= this.byteLength) return new Uint8Array(0);
  70. if(end == null) end = this.byteLength;
  71. if(end < 0) { end += this.byteLength; if(end < 0) end = 0; }
  72. if(end > this.byteLength) end = this.byteLength;
  73. if(start > end) return new Uint8Array(0);
  74. var out = new ArrayBuffer(end - start);
  75. var view = new Uint8Array(out);
  76. var data = new Uint8Array(this, start, end - start)
  77. /* IE10 should have Uint8Array#set */
  78. if(view.set) view.set(data); else while(start <= --end) view[end - start] = data[end];
  79. return out;
  80. };
  81. if(typeof Uint8Array !== 'undefined' && !Uint8Array.prototype.slice) Uint8Array.prototype.slice = function(start, end) {
  82. if(start == null) start = 0;
  83. if(start < 0) { start += this.length; if(start < 0) start = 0; }
  84. if(start >= this.length) return new Uint8Array(0);
  85. if(end == null) end = this.length;
  86. if(end < 0) { end += this.length; if(end < 0) end = 0; }
  87. if(end > this.length) end = this.length;
  88. if(start > end) return new Uint8Array(0);
  89. var out = new Uint8Array(end - start);
  90. while(start <= --end) out[end - start] = this[end];
  91. return out;
  92. };
  93. // VBScript + ActiveX fallback for IE5+
  94. var IE_SaveFile = (function() { try {
  95. if(typeof IE_SaveFile_Impl == "undefined") document.write([
  96. '<script type="text/vbscript" language="vbscript">',
  97. 'IE_GetProfileAndPath_Key = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders\\"',
  98. 'Function IE_GetProfileAndPath(key): Set wshell = CreateObject("WScript.Shell"): IE_GetProfileAndPath = wshell.RegRead(IE_GetProfileAndPath_Key & key): IE_GetProfileAndPath = wshell.ExpandEnvironmentStrings("%USERPROFILE%") & "!" & IE_GetProfileAndPath: End Function',
  99. 'Function IE_SaveFile_Impl(FileName, payload): Dim data, plen, i, bit: data = CStr(payload): plen = Len(data): Set fso = CreateObject("Scripting.FileSystemObject"): fso.CreateTextFile FileName, True: Set f = fso.GetFile(FileName): Set stream = f.OpenAsTextStream(2, 0): For i = 1 To plen Step 3: bit = Mid(data, i, 2): stream.write Chr(CLng("&h" & bit)): Next: stream.Close: IE_SaveFile_Impl = True: End Function',
  100. '|/script>'.replace("|","<")
  101. ].join("\r\n"));
  102. if(typeof IE_SaveFile_Impl == "undefined") return void 0;
  103. var IE_GetPath = (function() {
  104. var DDP1 = "";
  105. try { DDP1 = IE_GetProfileAndPath("{374DE290-123F-4565-9164-39C4925E467B}"); } catch(e) { try { DDP1 = IE_GetProfileAndPath("Personal"); } catch(e) { try { DDP1 = IE_GetProfileAndPath("Desktop"); } catch(e) { throw e; }}}
  106. var o = DDP1.split("!");
  107. DDP = o[1].replace("%USERPROFILE%", o[0]);
  108. return function(path) { return DDP + "\\" + path; };
  109. })();
  110. function fix_data(data) {
  111. var out = [];
  112. var T = typeof data == "string";
  113. for(var i = 0; i < data.length; ++i) out.push(("00"+(T ? data.charCodeAt(i) : data[i]).toString(16)).slice(-2));
  114. var o = out.join("|");
  115. return o;
  116. }
  117. return function(data, filename) { return IE_SaveFile_Impl(IE_GetPath(filename), fix_data(data)); };
  118. } catch(e) { return void 0; }})();
  119. var IE_LoadFile = (function() { try {
  120. if(typeof IE_LoadFile_Impl == "undefined") document.write([
  121. '<script type="text/vbscript" language="vbscript">',
  122. 'Function IE_LoadFile_Impl(FileName): Dim out(), plen, i, cc: Set fso = CreateObject("Scripting.FileSystemObject"): Set f = fso.GetFile(FileName): Set stream = f.OpenAsTextStream(1, 0): plen = f.Size: ReDim out(plen): For i = 1 To plen Step 1: cc = Hex(Asc(stream.read(1))): If Len(cc) < 2 Then: cc = "0" & cc: End If: out(i) = cc: Next: IE_LoadFile_Impl = Join(out,""): End Function',
  123. '|/script>'.replace("|","<")
  124. ].join("\r\n"));
  125. if(typeof IE_LoadFile_Impl == "undefined") return void 0;
  126. function fix_data(data) {
  127. var out = [];
  128. for(var i = 0; i < data.length; i+=2) out.push(String.fromCharCode(parseInt(data.slice(i, i+2), 16)));
  129. var o = out.join("");
  130. return o;
  131. }
  132. return function(filename) { return fix_data(IE_LoadFile_Impl(filename)); };
  133. } catch(e) { return void 0; }})();