utils.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. "use strict";
  2. var fs = require('fs'); // useful stuff
  3. var inherits = function inherits(cls, superCtor, statics, prototype) {
  4. // eslint-disable-next-line no-underscore-dangle
  5. cls.super_ = superCtor;
  6. if (!prototype) {
  7. prototype = statics;
  8. statics = null;
  9. }
  10. if (statics) {
  11. Object.keys(statics).forEach(function (i) {
  12. Object.defineProperty(cls, i, Object.getOwnPropertyDescriptor(statics, i));
  13. });
  14. }
  15. var properties = {
  16. constructor: {
  17. value: cls,
  18. enumerable: false,
  19. writable: false,
  20. configurable: true
  21. }
  22. };
  23. if (prototype) {
  24. Object.keys(prototype).forEach(function (i) {
  25. properties[i] = Object.getOwnPropertyDescriptor(prototype, i);
  26. });
  27. }
  28. cls.prototype = Object.create(superCtor.prototype, properties);
  29. }; // eslint-disable-next-line no-control-regex
  30. var xmlDecodeRegex = /[<>&'"\x7F\x00-\x08\x0B-\x0C\x0E-\x1F]/;
  31. var utils = {
  32. nop: function nop() {},
  33. promiseImmediate: function promiseImmediate(value) {
  34. return new Promise(function (resolve) {
  35. if (global.setImmediate) {
  36. setImmediate(function () {
  37. resolve(value);
  38. });
  39. } else {
  40. // poorman's setImmediate - must wait at least 1ms
  41. setTimeout(function () {
  42. resolve(value);
  43. }, 1);
  44. }
  45. });
  46. },
  47. inherits: inherits,
  48. dateToExcel: function dateToExcel(d, date1904) {
  49. return 25569 + d.getTime() / (24 * 3600 * 1000) - (date1904 ? 1462 : 0);
  50. },
  51. excelToDate: function excelToDate(v, date1904) {
  52. var millisecondSinceEpoch = Math.round((v - 25569 + (date1904 ? 1462 : 0)) * 24 * 3600 * 1000);
  53. return new Date(millisecondSinceEpoch);
  54. },
  55. parsePath: function parsePath(filepath) {
  56. var last = filepath.lastIndexOf('/');
  57. return {
  58. path: filepath.substring(0, last),
  59. name: filepath.substring(last + 1)
  60. };
  61. },
  62. getRelsPath: function getRelsPath(filepath) {
  63. var path = utils.parsePath(filepath);
  64. return "".concat(path.path, "/_rels/").concat(path.name, ".rels");
  65. },
  66. xmlEncode: function xmlEncode(text) {
  67. var regexResult = xmlDecodeRegex.exec(text);
  68. if (!regexResult) return text;
  69. var result = '';
  70. var escape = '';
  71. var lastIndex = 0;
  72. var i = regexResult.index;
  73. for (; i < text.length; i++) {
  74. var charCode = text.charCodeAt(i);
  75. switch (charCode) {
  76. case 34:
  77. // "
  78. escape = '&quot;';
  79. break;
  80. case 38:
  81. // &
  82. escape = '&amp;';
  83. break;
  84. case 39:
  85. // '
  86. escape = '&apos;';
  87. break;
  88. case 60:
  89. // <
  90. escape = '&lt;';
  91. break;
  92. case 62:
  93. // >
  94. escape = '&gt;';
  95. break;
  96. case 127:
  97. escape = '';
  98. break;
  99. default:
  100. {
  101. if (charCode <= 31 && (charCode <= 8 || charCode >= 11 && charCode !== 13)) {
  102. escape = '';
  103. break;
  104. }
  105. continue;
  106. }
  107. }
  108. if (lastIndex !== i) result += text.substring(lastIndex, i);
  109. lastIndex = i + 1;
  110. if (escape) result += escape;
  111. }
  112. if (lastIndex !== i) return result + text.substring(lastIndex, i);
  113. return result;
  114. },
  115. xmlDecode: function xmlDecode(text) {
  116. return text.replace(/&([a-z]*);/g, function (c) {
  117. switch (c) {
  118. case '&lt;':
  119. return '<';
  120. case '&gt;':
  121. return '>';
  122. case '&amp;':
  123. return '&';
  124. case '&apos;':
  125. return '\'';
  126. case '&quot;':
  127. return '"';
  128. default:
  129. return c;
  130. }
  131. });
  132. },
  133. validInt: function validInt(value) {
  134. var i = parseInt(value, 10);
  135. return !Number.isNaN(i) ? i : 0;
  136. },
  137. isDateFmt: function isDateFmt(fmt) {
  138. if (!fmt) {
  139. return false;
  140. } // must remove all chars inside quotes and []
  141. fmt = fmt.replace(/\[[^\]]*]/g, '');
  142. fmt = fmt.replace(/"[^"]*"/g, ''); // then check for date formatting chars
  143. var result = fmt.match(/[ymdhMsb]+/) !== null;
  144. return result;
  145. },
  146. fs: {
  147. exists: function exists(path) {
  148. return new Promise(function (resolve) {
  149. fs.access(path, fs.constants.F_OK, function (err) {
  150. resolve(!err);
  151. });
  152. });
  153. }
  154. },
  155. toIsoDateString: function toIsoDateString(dt) {
  156. return dt.toIsoString().subsstr(0, 10);
  157. }
  158. };
  159. module.exports = utils;
  160. //# sourceMappingURL=utils.js.map