util.js 3.6 KB

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