@@ -13,7 +13,7 @@ low.mixin(require('underscore.inflections'))
1313// utils.createId can generate incremental id or uuid
1414low . mixin ( { createId : utils . createId } )
1515
16- module . exports = function ( source ) {
16+ module . exports = function ( source ) {
1717 // Create router
1818 var router = express . Router ( )
1919
@@ -23,18 +23,19 @@ module.exports = function(source) {
2323 router . use ( methodOverride ( ) )
2424
2525 // Create database
26+ var db
2627 if ( _ . isObject ( source ) ) {
27- var db = low ( )
28+ db = low ( )
2829 db . object = source
2930 } else {
30- var db = low ( source )
31+ db = low ( source )
3132 }
3233
3334 // Expose database
3435 router . db = db
3536
3637 // GET /db
37- function showDatabase ( req , res , next ) {
38+ function showDatabase ( req , res , next ) {
3839 res . jsonp ( db . object )
3940 }
4041
@@ -44,7 +45,7 @@ module.exports = function(source) {
4445 // GET /:parent/:parentId/:resource?attr=&attr=
4546 // GET /*?*&_end=
4647 // GET /*?*&_start=&_end=
47- function list ( req , res , next ) {
48+ function list ( req , res , next ) {
4849 // Test if resource exists
4950 if ( ! db . object . hasOwnProperty ( req . params . resource ) ) {
5051 return res . sendStatus ( 404 )
@@ -74,7 +75,7 @@ module.exports = function(source) {
7475 // Full-text search
7576 var q = req . query . q . toLowerCase ( )
7677
77- array = db ( req . params . resource ) . filter ( function ( obj ) {
78+ array = db ( req . params . resource ) . filter ( function ( obj ) {
7879 for ( var key in obj ) {
7980 var value = obj [ key ]
8081 if ( _ . isString ( value ) && value . toLowerCase ( ) . indexOf ( q ) !== - 1 ) {
@@ -112,12 +113,12 @@ module.exports = function(source) {
112113 if ( _sort ) {
113114 _order = _order || 'ASC'
114115
115- array = _ . sortBy ( array , function ( element ) {
116- return element [ _sort ] ;
116+ array = _ . sortBy ( array , function ( element ) {
117+ return element [ _sort ]
117118 } )
118119
119120 if ( _order === 'DESC' ) {
120- array . reverse ( ) ;
121+ array . reverse ( )
121122 }
122123 }
123124
@@ -127,20 +128,21 @@ module.exports = function(source) {
127128 res . setHeader ( 'Access-Control-Expose-Headers' , 'X-Total-Count' )
128129 }
129130
130- _start = parseInt ( _start ) || 0
131+ _start = parseInt ( _start , 10 ) || 0
131132
132133 if ( _end ) {
133- array = array . slice ( _start , parseInt ( _end ) )
134+ _end = parseInt ( _end , 10 )
135+ array = array . slice ( _start , _end )
134136 } else if ( _limit ) {
135- // Convert strings to int and sum to get end value
136- array = array . slice ( _start , parseInt ( _start ) + parseInt ( _limit ) )
137+ _limit = parseInt ( _limit , 10 )
138+ array = array . slice ( _start , _start + _limit )
137139 }
138140
139141 res . jsonp ( array )
140142 }
141143
142144 // GET /:resource/:id
143- function show ( req , res , next ) {
145+ function show ( req , res , next ) {
144146 var resource = db ( req . params . resource )
145147 . get ( utils . toNative ( req . params . id ) )
146148
@@ -152,7 +154,7 @@ module.exports = function(source) {
152154 }
153155
154156 // POST /:resource
155- function create ( req , res , next ) {
157+ function create ( req , res , next ) {
156158 for ( var key in req . body ) {
157159 req . body [ key ] = utils . toNative ( req . body [ key ] )
158160 }
@@ -165,7 +167,7 @@ module.exports = function(source) {
165167
166168 // PUT /:resource/:id
167169 // PATCH /:resource/:id
168- function update ( req , res , next ) {
170+ function update ( req , res , next ) {
169171 for ( var key in req . body ) {
170172 req . body [ key ] = utils . toNative ( req . body [ key ] )
171173 }
@@ -181,13 +183,13 @@ module.exports = function(source) {
181183 }
182184
183185 // DELETE /:resource/:id
184- function destroy ( req , res , next ) {
186+ function destroy ( req , res , next ) {
185187 db ( req . params . resource ) . remove ( utils . toNative ( req . params . id ) )
186188
187189 // Remove dependents documents
188190 var removable = utils . getRemovable ( db . object )
189191
190- _ ( removable ) . each ( function ( item ) {
192+ _ ( removable ) . each ( function ( item ) {
191193 db ( item . name ) . remove ( item . id )
192194 } )
193195
0 commit comments