util.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "use strict";
  2. module.exports = {
  3. /**
  4. * sanitizes input string by handling escape characters eg: converts '''' to '\'\''
  5. * and trim input if required
  6. *
  7. * @param {String} inputString
  8. * @param {Boolean} [trim] - indicates whether to trim string or not
  9. * @returns {String}
  10. */
  11. sanitize: function (inputString, trim) {
  12. if (typeof inputString !== 'string') {
  13. return '';
  14. }
  15. inputString = inputString.replace(/\\/g, '\\\\')
  16. .replace(/"/g, '\\"')
  17. .replace(/\n/g, '\\n')
  18. .replace(/\r/g, '\\r');
  19. return trim ? inputString.trim() : inputString;
  20. },
  21. /**
  22. * sanitizes input string by handling escape characters eg: converts '''' to '\'\''
  23. * and trim input if required
  24. *
  25. * @param {String} inputString
  26. * @param {Boolean} [trim] - indicates whether to trim string or not
  27. * @returns {String}
  28. */
  29. sanitizeMultiline: function (inputString, trim) {
  30. if (typeof inputString !== 'string') {
  31. return '';
  32. }
  33. inputString = inputString
  34. .replace(/`/g, '`+"`"+`')
  35. .replace(/\r/g, '`+"\r"+`'); // Go discards \r from raw strings, so manually keep them
  36. return trim ? inputString.trim() : inputString;
  37. },
  38. /**
  39. * sanitizes input options
  40. *
  41. * @param {Object} options - Options provided by the user
  42. * @param {Array} optionsArray - options array received from getOptions function
  43. *
  44. * @returns {Object} - Sanitized options object
  45. */
  46. sanitizeOptions: function (options, optionsArray) {
  47. var result = {}, defaultOptions = {}, id;
  48. optionsArray.forEach((option) => {
  49. defaultOptions[option.id] = {
  50. default: option.default,
  51. type: option.type
  52. };
  53. if (option.type === 'enum') {
  54. defaultOptions[option.id].availableOptions = option.availableOptions;
  55. }
  56. });
  57. for (id in options) {
  58. if (options.hasOwnProperty(id)) {
  59. if (defaultOptions[id] === undefined) {
  60. continue;
  61. }
  62. switch (defaultOptions[id].type) {
  63. case 'boolean':
  64. if (typeof options[id] !== 'boolean') {
  65. result[id] = defaultOptions[id].default;
  66. }
  67. else {
  68. result[id] = options[id];
  69. }
  70. break;
  71. case 'positiveInteger':
  72. if (typeof options[id] !== 'number' || options[id] < 0) {
  73. result[id] = defaultOptions[id].default;
  74. }
  75. else {
  76. result[id] = options[id];
  77. }
  78. break;
  79. case 'enum':
  80. if (!defaultOptions[id].availableOptions.includes(options[id])) {
  81. result[id] = defaultOptions[id].default;
  82. }
  83. else {
  84. result[id] = options[id];
  85. }
  86. break;
  87. default:
  88. result[id] = options[id];
  89. }
  90. }
  91. }
  92. for (id in defaultOptions) {
  93. if (defaultOptions.hasOwnProperty(id)) {
  94. if (result[id] === undefined) {
  95. result[id] = defaultOptions[id].default;
  96. }
  97. }
  98. }
  99. return result;
  100. },
  101. /**
  102. *
  103. * @param {Array} array - form data array
  104. * @param {String} key - key of form data param
  105. * @param {String} type - type of form data param(file/text)
  106. * @param {String} val - value/src property of form data param
  107. * @param {String} disabled - Boolean denoting whether the param is disabled or not
  108. * @param {String} contentType - content type header of the param
  109. *
  110. * Appends a single param to form data array
  111. */
  112. addFormParam: function (array, key, type, val, disabled, contentType) {
  113. if (type === 'file') {
  114. array.push({
  115. key: key,
  116. type: type,
  117. src: val,
  118. disabled: disabled,
  119. contentType: contentType
  120. });
  121. }
  122. else {
  123. array.push({
  124. key: key,
  125. type: type,
  126. value: val,
  127. disabled: disabled,
  128. contentType: contentType
  129. });
  130. }
  131. }
  132. };