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 +28
-3
lines changed
Expand file tree Collapse file tree 3 files changed +28
-3
lines changed Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ module.exports = function MongoQS(opts) {
1111 this . custom = opts . custom || { } ;
1212
1313 this . keyRegex = opts . keyRegex || / ^ [ a - z æ ø å 0 - 9 - _ .] + $ / i;
14- this . arrRegex = opts . arrRegex || / ^ [ a - z æ ø å 0 - 9 - _ .] + \[ \] $ / i;
14+ this . arrRegex = opts . arrRegex || / ^ [ a - z æ ø å 0 - 9 - _ .] + ( \[ \] ) ? $ / i;
1515
1616 for ( var param in this . custom ) {
1717 switch ( param ) {
@@ -129,8 +129,9 @@ module.exports.prototype.parse = function(query) {
129129
130130 // array key
131131 if ( val instanceof Array && this . arrRegex . test ( key ) ) {
132- if ( this . ops . indexOf ( '$in' ) >= 0 ) {
133- key = key . substr ( 0 , key . length - 2 ) ;
132+ if ( this . ops . indexOf ( '$in' ) >= 0 && val . length > 0 ) {
133+ // remove [] at end of key name (unless it has already been removed)
134+ key = key . replace ( / \[ \] $ / , '' ) ;
134135
135136 // $in query
136137 if ( val [ 0 ] [ 0 ] !== '!' ) {
Original file line number Diff line number Diff line change 4343 "devDependencies" : {
4444 "jshint" : " ^2.8.0" ,
4545 "mocha" : " ^2.3.4" ,
46+ "qs" : " ^5.2.0" ,
4647 "semantic-release" : " ^4.3.5"
4748 }
4849}
Original file line number Diff line number Diff line change @@ -299,6 +299,17 @@ describe('parse()', function() {
299299 } ) ;
300300 } ) ;
301301
302+ it ( 'returns in array query with "qs" parser module (GH-06)' , function ( ) {
303+ var string = 'foo[]=10&foo[]=10.011&foo[]=bar' ;
304+ var params = require ( 'qs' ) . parse ( string ) ;
305+
306+ assert . deepEqual ( qs . parse ( params ) , {
307+ foo : {
308+ $in : [ 10 , 10.011 , 'bar' ]
309+ }
310+ } ) ;
311+ } ) ;
312+
302313 it ( 'returns in array without any not in array query' , function ( ) {
303314 var string = 'foo[]=10&foo[]=!10.011&foo[]=!bar&foo[]=baz' ;
304315 var params = require ( 'querystring' ) . parse ( string ) ;
@@ -321,6 +332,18 @@ describe('parse()', function() {
321332 } ) ;
322333 } ) ;
323334
335+ it ( 'returns not in array query with "gs" parser module (GH-06)' , function ( ) {
336+ var string = 'foo[]=!10&foo[]=!10.011&foo[]=!bar' ;
337+ var params = require ( 'qs' ) . parse ( string ) ;
338+
339+ assert . deepEqual ( qs . parse ( params ) , {
340+ foo : {
341+ $nin : [ 10 , 10.011 , 'bar' ]
342+ }
343+ } ) ;
344+ } ) ;
345+
346+
324347 it ( 'returns not in array without any in array query' , function ( ) {
325348 var string = 'foo[]=!10&foo[]=10.011&foo[]=bar&foo[]=!baz' ;
326349 var params = require ( 'querystring' ) . parse ( string ) ;
You can’t perform that action at this time.
0 commit comments