123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- "use strict";
- /**
- * sanitizes input string by handling escape characters eg: converts '''' to '\'\'' and trim input if required
- *
- * @param {String} inputString - Input String to sanitize
- * @param {Boolean} [trim] - Indicates whether to trim string or not
- * @returns {String} Sanitized String handling escape characters
- */
- function sanitize(inputString, trim) {
- /* istanbul ignore next */
- if (typeof inputString !== 'string') {
- return '';
- }
- inputString = inputString.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
- return trim ? inputString.trim() : inputString;
- }
- /**
- * sanitizes input options
- *
- * @param {Object} options - Options provided by the user
- * @param {Array} optionsArray - options array received from getOptions function
- *
- * @returns {Object} - Sanitized options object
- */
- function sanitizeOptions(options, optionsArray) {
- var result = {}, defaultOptions = {}, id;
- optionsArray.forEach((option) => {
- defaultOptions[option.id] = {
- default: option.default,
- type: option.type
- };
- if (option.type === 'enum') {
- defaultOptions[option.id].availableOptions = option.availableOptions;
- }
- });
- for (id in options) {
- if (options.hasOwnProperty(id)) {
- if (defaultOptions[id] === undefined) {
- continue;
- }
- switch (defaultOptions[id].type) {
- case 'boolean':
- if (typeof options[id] !== 'boolean') {
- result[id] = defaultOptions[id].default;
- }
- else {
- result[id] = options[id];
- }
- break;
- case 'positiveInteger':
- if (typeof options[id] !== 'number' || options[id] < 0) {
- result[id] = defaultOptions[id].default;
- }
- else {
- result[id] = options[id];
- }
- break;
- case 'enum':
- if (!defaultOptions[id].availableOptions.includes(options[id])) {
- result[id] = defaultOptions[id].default;
- }
- else {
- result[id] = options[id];
- }
- break;
- default:
- result[id] = options[id];
- }
- }
- }
- for (id in defaultOptions) {
- if (defaultOptions.hasOwnProperty(id)) {
- if (result[id] === undefined) {
- result[id] = defaultOptions[id].default;
- }
- }
- }
- return result;
- }
- /**
- *
- * @param {Array} array - form data array
- * @param {String} key - key of form data param
- * @param {String} type - type of form data param(file/text)
- * @param {String} val - value/src property of form data param
- * @param {String} disabled - Boolean denoting whether the param is disabled or not
- * @param {String} contentType - content type header of the param
- * Appends a single param to form data array
- */
- function addFormParam(array, key, type, val, disabled, contentType) {
- if (type === 'file') {
- array.push({
- key: key,
- type: type,
- src: val,
- disabled: disabled,
- contentType: contentType
- });
- }
- else {
- array.push({
- key: key,
- type: type,
- value: val,
- disabled: disabled,
- contentType: contentType
- });
- }
- }
- module.exports = {
- sanitize: sanitize,
- sanitizeOptions: sanitizeOptions,
- addFormParam: addFormParam
- };
|