1- var  _  =  require ( 'underscore ' ) 
1+ var  _  =  require ( 'lodash ' ) 
22var  uuid  =  require ( 'node-uuid' ) 
33var  pluralize  =  require ( 'pluralize' ) 
44
55// Turns string to native. 
66// Example: 
77//   'true' -> true 
88//   '1' -> 1 
9- function  toNative   ( value )  { 
10-   if   ( typeof  value  ===  'string' )  { 
11-     if   ( value  ===  '' 
12-     ||  value . trim ( )  !==  value 
13-     ||  ( value . length  >  1  &&  value [ 0 ]  ===  '0' ) )  { 
9+ function  toNative ( value )  { 
10+   if ( typeof  value  ===  'string' )  { 
11+     if ( value  ===  '' 
12+         ||  value . trim ( )  !==  value 
13+         ||  ( value . length  >  1  &&  value [ 0 ]  ===  '0' ) )  { 
1414      return  value 
15-     }  else  if   ( value  ===  'true'  ||  value  ===  'false' )  { 
15+     }  else  if ( value  ===  'true'  ||  value  ===  'false' )  { 
1616      return  value  ===  'true' 
17-     }  else  if   ( ! isNaN ( + value ) )  { 
17+     }  else  if ( ! isNaN ( + value ) )  { 
1818      return  + value 
1919    } 
2020  } 
2121  return  value 
2222} 
2323
2424// Return incremented id or uuid 
25- function  createId   ( coll )  { 
26-   if   ( _ . isEmpty ( coll ) )  { 
25+ function  createId ( coll )  { 
26+   if ( _ . isEmpty ( coll ) )  { 
2727    return  1 
2828  }  else  { 
29-     var  id  =  _ . max ( coll ,  function   ( doc )  { 
29+     var  id  =  _ . max ( coll ,  function ( doc )  { 
3030      return  doc . id 
3131    } ) . id 
3232
33-     if   ( _ . isFinite ( id ) )  { 
33+     if ( _ . isFinite ( id ) )  { 
3434      // Increment integer id 
3535      return  ++ id 
3636    }  else  { 
@@ -42,19 +42,19 @@ function createId (coll) {
4242
4343// Returns document ids that have unsatisfied relations 
4444// Example: a comment that references a post that doesn't exist 
45- function  getRemovable   ( db )  { 
45+ function  getRemovable ( db )  { 
4646  var  removable  =  [ ] 
4747
48-   _ ( db ) . each ( function   ( coll ,  collName )  { 
49-     _ ( coll ) . each ( function   ( doc )  { 
50-       _ ( doc ) . each ( function   ( value ,  key )  { 
51-         if   ( / I d $ / . test ( key ) )  { 
48+   _ ( db ) . each ( function ( coll ,  collName )  { 
49+     _ ( coll ) . each ( function ( doc )  { 
50+       _ ( doc ) . each ( function ( value ,  key )  { 
51+         if ( / I d $ / . test ( key ) )  { 
5252          var  refName  =  pluralize . plural ( key . slice ( 0 ,  - 2 ) ) 
5353          // Test if table exists 
54-           if   ( db [ refName ] )  { 
54+           if ( db [ refName ] )  { 
5555            // Test if references is defined in table 
5656            var  ref  =  _ . findWhere ( db [ refName ] ,  { id : value } ) 
57-             if   ( _ . isUndefined ( ref ) )  { 
57+             if ( _ . isUndefined ( ref ) )  { 
5858              removable . push ( { name : collName ,  id : doc . id } ) 
5959            } 
6060          } 
@@ -66,8 +66,29 @@ function getRemovable (db) {
6666  return  removable 
6767} 
6868
69+ function  deepQuery ( value ,  q )  { 
70+   if ( value )  { 
71+     if ( _ . isArray ( value ) )  { 
72+       for ( var  i  =  0 ;  i  <  value . length ;  i ++ )  { 
73+         if ( deepQuery ( value [ i ] ,  q ) )  { 
74+           return  true 
75+         } 
76+       } 
77+     }  else  if ( _ . isPlainObject ( value ) )  { 
78+       for ( var  k  in  value )  { 
79+         if ( deepQuery ( value [ k ] ,  q ) )  { 
80+           return  true 
81+         } 
82+       } 
83+     }  else  if ( value . toString ( ) . toLowerCase ( ) . indexOf ( q )  !==  - 1 )  { 
84+       return  true ; 
85+     } 
86+   } 
87+ } 
88+ 
6989module . exports  =  { 
7090  toNative : toNative , 
7191  createId : createId , 
72-   getRemovable : getRemovable 
92+   getRemovable : getRemovable , 
93+   deepQuery : deepQuery 
7394} 
0 commit comments