utils.js 4.1 KB

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