util.js 3.9 KB

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