diff --git a/src/router.js b/src/router.js index 2272f74..fb5d65f 100644 --- a/src/router.js +++ b/src/router.js @@ -78,7 +78,7 @@ module.exports = function (source) { array = db(req.params.resource).filter(function (obj) { for (var key in obj) { var value = obj[key] - if(utils.deepQuery(value, q)){ + if (utils.deepQuery(value, q)) { return true } } diff --git a/src/utils.js b/src/utils.js index 69060b7..65daaf6 100644 --- a/src/utils.js +++ b/src/utils.js @@ -6,15 +6,15 @@ var pluralize = require('pluralize') // Example: // 'true' -> true // '1' -> 1 -function toNative(value) { - if(typeof value === 'string') { - if(value === '' +function toNative (value) { + if (typeof value === 'string') { + if (value === '' || value.trim() !== value || (value.length > 1 && value[0] === '0')) { return value - } else if(value === 'true' || value === 'false') { + } else if (value === 'true' || value === 'false') { return value === 'true' - } else if(!isNaN(+value)) { + } else if (!isNaN(+value)) { return +value } } @@ -22,15 +22,15 @@ function toNative(value) { } // Return incremented id or uuid -function createId(coll) { - if(_.isEmpty(coll)) { +function createId (coll) { + if (_.isEmpty(coll)) { return 1 } else { - var id = _.max(coll, function(doc) { + var id = _.max(coll, function (doc) { return doc.id }).id - if(_.isFinite(id)) { + if (_.isFinite(id)) { // Increment integer id return ++id } else { @@ -42,19 +42,19 @@ function createId(coll) { // Returns document ids that have unsatisfied relations // Example: a comment that references a post that doesn't exist -function getRemovable(db) { +function getRemovable (db) { var removable = [] - _(db).each(function(coll, collName) { - _(coll).each(function(doc) { - _(doc).each(function(value, key) { - if(/Id$/.test(key)) { + _(db).each(function (coll, collName) { + _(coll).each(function (doc) { + _(doc).each(function (value, key) { + if (/Id$/.test(key)) { var refName = pluralize.plural(key.slice(0, -2)) // Test if table exists - if(db[refName]) { + if (db[refName]) { // Test if references is defined in table var ref = _.findWhere(db[refName], {id: value}) - if(_.isUndefined(ref)) { + if (_.isUndefined(ref)) { removable.push({name: collName, id: doc.id}) } } @@ -66,22 +66,22 @@ function getRemovable(db) { return removable } -function deepQuery(value, q) { - if(value) { - if(_.isArray(value)) { - for(var i = 0; i < value.length; i++) { - if(deepQuery(value[i], q)) { +function deepQuery (value, q) { + if (value) { + if (_.isArray(value)) { + for (var i = 0; i < value.length; i++) { + if (deepQuery(value[i], q)) { return true } } - } else if(_.isPlainObject(value)) { - for(var k in value) { - if(deepQuery(value[k], q)) { + } else if (_.isPlainObject(value)) { + for (var k in value) { + if (deepQuery(value[k], q)) { return true } } - } else if(value.toString().toLowerCase().indexOf(q) !== -1) { - return true; + } else if (value.toString().toLowerCase().indexOf(q) !== -1) { + return true } } }