string.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. 'use strict';
  2. exports.__esModule = true;
  3. exports.charsMap = exports.charsRegex = undefined;
  4. var _stringify = require('babel-runtime/core-js/json/stringify');
  5. var _stringify2 = _interopRequireDefault(_stringify);
  6. var _typeof2 = require('babel-runtime/helpers/typeof');
  7. var _typeof3 = _interopRequireDefault(_typeof2);
  8. exports.makeEscape = makeEscape;
  9. exports.escapeObject = escapeObject;
  10. exports.arrayToList = arrayToList;
  11. exports.bufferToString = bufferToString;
  12. exports.escapeString = escapeString;
  13. exports.dateToString = dateToString;
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. /*eslint max-len: 0, no-var:0 */
  16. var charsRegex = exports.charsRegex = /[\0\b\t\n\r\x1a\"\'\\]/g; // eslint-disable-line no-control-regex
  17. var charsMap = exports.charsMap = {
  18. '\0': '\\0',
  19. '\b': '\\b',
  20. '\t': '\\t',
  21. '\n': '\\n',
  22. '\r': '\\r',
  23. '\x1a': '\\Z',
  24. '"': '\\"',
  25. '\'': '\\\'',
  26. '\\': '\\\\'
  27. };
  28. function wrapEscape(escapeFn) {
  29. return function finalEscape(val) {
  30. var ctx = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  31. return escapeFn(val, finalEscape, ctx);
  32. };
  33. }
  34. function makeEscape() {
  35. var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  36. var finalEscapeDate = config.escapeDate || dateToString;
  37. var finalEscapeArray = config.escapeArray || arrayToList;
  38. var finalEscapeBuffer = config.escapeBuffer || bufferToString;
  39. var finalEscapeString = config.escapeString || escapeString;
  40. var finalEscapeObject = config.escapeObject || escapeObject;
  41. var finalWrap = config.wrap || wrapEscape;
  42. function escapeFn(val, finalEscape, ctx) {
  43. if (val === undefined || val === null) {
  44. return 'NULL';
  45. }
  46. switch (typeof val === 'undefined' ? 'undefined' : (0, _typeof3.default)(val)) {
  47. case 'boolean':
  48. return val ? 'true' : 'false';
  49. case 'number':
  50. return val + '';
  51. case 'object':
  52. if (val instanceof Date) {
  53. val = finalEscapeDate(val, finalEscape, ctx);
  54. } else if (Array.isArray(val)) {
  55. return finalEscapeArray(val, finalEscape, ctx);
  56. } else if (Buffer.isBuffer(val)) {
  57. return finalEscapeBuffer(val, finalEscape, ctx);
  58. } else {
  59. return finalEscapeObject(val, finalEscape, ctx);
  60. }
  61. }
  62. return finalEscapeString(val, finalEscape, ctx);
  63. }
  64. return finalWrap ? finalWrap(escapeFn) : escapeFn;
  65. }
  66. function escapeObject(val, finalEscape, ctx) {
  67. if (typeof val.toSQL === 'function') {
  68. return val.toSQL(ctx);
  69. } else {
  70. return (0, _stringify2.default)(val);
  71. }
  72. }
  73. function arrayToList(array, finalEscape, ctx) {
  74. var sql = '';
  75. for (var i = 0; i < array.length; i++) {
  76. var val = array[i];
  77. if (Array.isArray(val)) {
  78. sql += (i === 0 ? '' : ', ') + '(' + arrayToList(val, finalEscape, ctx) + ')';
  79. } else {
  80. sql += (i === 0 ? '' : ', ') + finalEscape(val, ctx);
  81. }
  82. }
  83. return sql;
  84. }
  85. function bufferToString(buffer) {
  86. return "X" + escapeString(buffer.toString('hex'));
  87. }
  88. function escapeString(val, finalEscape, ctx) {
  89. var chunkIndex = charsRegex.lastIndex = 0;
  90. var escapedVal = '';
  91. var match;
  92. while (match = charsRegex.exec(val)) {
  93. escapedVal += val.slice(chunkIndex, match.index) + charsMap[match[0]];
  94. chunkIndex = charsRegex.lastIndex;
  95. }
  96. if (chunkIndex === 0) {
  97. // Nothing was escaped
  98. return "'" + val + "'";
  99. }
  100. if (chunkIndex < val.length) {
  101. return "'" + escapedVal + val.slice(chunkIndex) + "'";
  102. }
  103. return "'" + escapedVal + "'";
  104. }
  105. function dateToString(date, finalEscape, ctx) {
  106. var timeZone = ctx.timeZone || 'local';
  107. var dt = new Date(date);
  108. var year;
  109. var month;
  110. var day;
  111. var hour;
  112. var minute;
  113. var second;
  114. var millisecond;
  115. if (timeZone === 'local') {
  116. year = dt.getFullYear();
  117. month = dt.getMonth() + 1;
  118. day = dt.getDate();
  119. hour = dt.getHours();
  120. minute = dt.getMinutes();
  121. second = dt.getSeconds();
  122. millisecond = dt.getMilliseconds();
  123. } else {
  124. var tz = convertTimezone(timeZone);
  125. if (tz !== false && tz !== 0) {
  126. dt.setTime(dt.getTime() + tz * 60000);
  127. }
  128. year = dt.getUTCFullYear();
  129. month = dt.getUTCMonth() + 1;
  130. day = dt.getUTCDate();
  131. hour = dt.getUTCHours();
  132. minute = dt.getUTCMinutes();
  133. second = dt.getUTCSeconds();
  134. millisecond = dt.getUTCMilliseconds();
  135. }
  136. // YYYY-MM-DD HH:mm:ss.mmm
  137. return zeroPad(year, 4) + '-' + zeroPad(month, 2) + '-' + zeroPad(day, 2) + ' ' + zeroPad(hour, 2) + ':' + zeroPad(minute, 2) + ':' + zeroPad(second, 2) + '.' + zeroPad(millisecond, 3);
  138. }
  139. function zeroPad(number, length) {
  140. number = number.toString();
  141. while (number.length < length) {
  142. number = '0' + number;
  143. }
  144. return number;
  145. }
  146. function convertTimezone(tz) {
  147. if (tz === 'Z') {
  148. return 0;
  149. }
  150. var m = tz.match(/([\+\-\s])(\d\d):?(\d\d)?/);
  151. if (m) {
  152. return (m[1] == '-' ? -1 : 1) * (parseInt(m[2], 10) + (m[3] ? parseInt(m[3], 10) : 0) / 60) * 60;
  153. }
  154. return false;
  155. }