@@ -11,6 +11,7 @@ function sanitize (inputString, trim) {
1111 if ( typeof inputString !== 'string' ) {
1212 return '' ;
1313 }
14+
1415 ( trim ) && ( inputString = inputString . trim ( ) ) ;
1516 return inputString . replace ( / \\ / g, '\\\\' ) . replace ( / ' / g, '\\\'' )
1617 . replace ( / \n / g, '\\n' )
@@ -30,49 +31,46 @@ function sanitizeOptions (options, optionsArray) {
3031 var result = { } ,
3132 defaultOptions = { } ,
3233 id ;
34+
3335 optionsArray . forEach ( ( option ) => {
34- defaultOptions [ option . id ] = {
35- default : option . default ,
36- type : option . type
37- } ;
38- if ( option . type === 'enum' ) {
39- defaultOptions [ option . id ] . availableOptions = option . availableOptions ;
36+ const { id, default : defaultValue , type, availableOptions } = option ;
37+ defaultOptions [ id ] = { default : defaultValue , type } ;
38+
39+ if ( type === 'enum' ) {
40+ defaultOptions [ id ] . availableOptions = availableOptions ;
4041 }
4142 } ) ;
4243
43- for ( id in options ) {
44- if ( options . hasOwnProperty ( id ) ) {
45- if ( defaultOptions [ id ] === undefined ) {
46- continue ;
47- }
48- switch ( defaultOptions [ id ] . type ) {
49- case 'boolean' :
50- if ( typeof options [ id ] !== 'boolean' ) {
51- result [ id ] = defaultOptions [ id ] . default ;
52- }
53- else {
54- result [ id ] = options [ id ] ;
55- }
56- break ;
57- case 'positiveInteger' :
58- if ( typeof options [ id ] !== 'number' || options [ id ] < 0 ) {
59- result [ id ] = defaultOptions [ id ] . default ;
60- }
61- else {
62- result [ id ] = options [ id ] ;
63- }
64- break ;
65- case 'enum' :
66- if ( ! defaultOptions [ id ] . availableOptions . includes ( options [ id ] ) ) {
67- result [ id ] = defaultOptions [ id ] . default ;
68- }
69- else {
70- result [ id ] = options [ id ] ;
71- }
72- break ;
73- default :
74- result [ id ] = options [ id ] ;
75- }
44+ /**
45+ * A type checker object that checks the type of an option value
46+ *
47+ * @typedef {Object } typeCheckers
48+ *
49+ * @property {function(Object, string): boolean } boolean - checks if the option value is a boolean
50+ * @property {function(Object, string): number } positiveInteger - checks if the option value is a positive integer
51+ * @property {function(Object, string): string } enum - checks if the option value is one of the available options
52+ * @property {function(Object, string): * } default - returns the option value without any type checking
53+ *
54+ */
55+ const typeCheckers = {
56+ boolean : ( options , id ) => {
57+ return typeof options [ id ] === 'boolean' ? options [ id ] : defaultOptions [ id ] . default ;
58+ } ,
59+ positiveInteger : ( options , id ) => {
60+ return typeof options [ id ] === 'number' && options [ id ] >= 0 ? options [ id ] : defaultOptions [ id ] . default ;
61+ } ,
62+ enum : ( options , id ) => {
63+ return defaultOptions [ id ] . availableOptions . includes ( options [ id ] ) ? options [ id ] : defaultOptions [ id ] . default ;
64+ } ,
65+ default : ( options , id ) => {
66+ return options [ id ] ;
67+ }
68+ } ;
69+
70+ for ( const id in options ) {
71+ if ( options . hasOwnProperty ( id ) && defaultOptions [ id ] !== undefined ) {
72+ const typeChecker = typeCheckers [ defaultOptions [ id ] . type ] || typeCheckers . default ;
73+ result [ id ] = typeChecker ( options , id ) ;
7674 }
7775 }
7876
@@ -98,24 +96,21 @@ function sanitizeOptions (options, optionsArray) {
9896 * Appends a single param to form data array
9997 */
10098function addFormParam ( array , key , type , val , disabled , contentType ) {
99+ const formParam = {
100+ key,
101+ type,
102+ disabled,
103+ contentType
104+ } ;
105+
101106 if ( type === 'file' ) {
102- array . push ( {
103- key : key ,
104- type : type ,
105- src : val ,
106- disabled : disabled ,
107- contentType : contentType
108- } ) ;
107+ formParam . src = val ;
109108 }
110109 else {
111- array . push ( {
112- key : key ,
113- type : type ,
114- value : val ,
115- disabled : disabled ,
116- contentType : contentType
117- } ) ;
110+ formParam . value = val ;
118111 }
112+
113+ array . push ( formParam ) ;
119114}
120115
121116module . exports = {
0 commit comments