This repository was archived by the owner on May 1, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +27
-3
lines changed
Expand file tree Collapse file tree 3 files changed +27
-3
lines changed Original file line number Diff line number Diff line change @@ -76,6 +76,9 @@ var MongoQS = require('mongo-querystring');
7676* ` object ` blacklist - blacklisted query params
7777* ` object ` whitelist - whitelisted query params
7878* ` object ` custom - custom query params
79+ * ` object ` string - string parsing
80+ * ` boolean ` toBoolean - parse ` "true" ` , ` "false" ` string to booleans
81+ * ` boolean ` toNumber - parse string integer and float values to numbers
7982
8083#### Bult in custom queries
8184
Original file line number Diff line number Diff line change @@ -10,6 +10,12 @@ module.exports = function MongoQS(opts) {
1010 this . whitelist = opts . whitelist || { } ;
1111 this . custom = opts . custom || { } ;
1212
13+ // String Value Parsing
14+ opts . string = opts . string || { } ;
15+ this . string = opts . string || { } ;
16+ this . string . toBoolean = opts . string . toBoolean || true ;
17+ this . string . toNumber = opts . string . toNumber || true ;
18+
1319 this . keyRegex = opts . keyRegex || / ^ [ a - z æ ø å 0 - 9 - _ .] + $ / i;
1420 this . arrRegex = opts . arrRegex || / ^ [ a - z æ ø å 0 - 9 - _ .] + ( \[ \] ) ? $ / i;
1521
@@ -101,11 +107,11 @@ module.exports.prototype.customAfter = function(field) {
101107} ;
102108
103109module . exports . prototype . parseString = function ( string ) {
104- if ( string . toLowerCase ( ) === 'true' ) {
110+ if ( this . string . toBoolean && string . toLowerCase ( ) === 'true' ) {
105111 return true ;
106- } else if ( string . toLowerCase ( ) === 'false' ) {
112+ } else if ( this . string . toBoolean && string . toLowerCase ( ) === 'false' ) {
107113 return false ;
108- } else if ( ! isNaN ( string ) ) {
114+ } else if ( this . string . toNumber && ! isNaN ( string ) ) {
109115 return parseFloat ( string , 10 ) ;
110116 } else {
111117 return string ;
Original file line number Diff line number Diff line change @@ -105,14 +105,29 @@ describe('parseString()', function() {
105105 assert . equal ( qs . parseString ( 'true' ) , true ) ;
106106 } ) ;
107107
108+ it ( 'returns string "true" when boolean parsing is disabled' , function ( ) {
109+ qs . string . toBoolean = false ;
110+ assert . equal ( qs . parseString ( 'true' ) , 'true' ) ;
111+ } ) ;
112+
108113 it ( 'returns boolean false for "flase" string' , function ( ) {
109114 assert . equal ( qs . parseString ( 'false' ) , false ) ;
110115 } ) ;
111116
117+ it ( 'returns string "false" when boolean parsing is disabled' , function ( ) {
118+ qs . string . toBoolean = false ;
119+ assert . equal ( qs . parseString ( 'false' ) , 'false' ) ;
120+ } ) ;
121+
112122 it ( 'returns number for parseable integer' , function ( ) {
113123 assert . equal ( qs . parseString ( '100' ) , 100 ) ;
114124 } ) ;
115125
126+ it ( 'returns string number when number parsing is disabled' , function ( ) {
127+ qs . string . toNumber = false ;
128+ assert . equal ( qs . parseString ( '100' ) , '100' ) ;
129+ } ) ;
130+
116131 it ( 'returns number for zero padded parseable integer' , function ( ) {
117132 assert . equal ( qs . parseString ( '000100' ) , 100 ) ;
118133 } ) ;
You can’t perform that action at this time.
0 commit comments