mirror of
https://github.com/typicode/json-server.git
synced 2025-07-28 12:43:18 +08:00
fixed spacing
This commit is contained in:
@ -78,7 +78,7 @@ module.exports = function (source) {
|
|||||||
array = db(req.params.resource).filter(function (obj) {
|
array = db(req.params.resource).filter(function (obj) {
|
||||||
for (var key in obj) {
|
for (var key in obj) {
|
||||||
var value = obj[key]
|
var value = obj[key]
|
||||||
if(utils.deepQuery(value, q)){
|
if (utils.deepQuery(value, q)) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
52
src/utils.js
52
src/utils.js
@ -6,15 +6,15 @@ var pluralize = require('pluralize')
|
|||||||
// Example:
|
// Example:
|
||||||
// 'true' -> true
|
// 'true' -> true
|
||||||
// '1' -> 1
|
// '1' -> 1
|
||||||
function toNative(value) {
|
function toNative (value) {
|
||||||
if(typeof value === 'string') {
|
if (typeof value === 'string') {
|
||||||
if(value === ''
|
if (value === ''
|
||||||
|| value.trim() !== value
|
|| value.trim() !== value
|
||||||
|| (value.length > 1 && value[0] === '0')) {
|
|| (value.length > 1 && value[0] === '0')) {
|
||||||
return value
|
return value
|
||||||
} else if(value === 'true' || value === 'false') {
|
} else if (value === 'true' || value === 'false') {
|
||||||
return value === 'true'
|
return value === 'true'
|
||||||
} else if(!isNaN(+value)) {
|
} else if (!isNaN(+value)) {
|
||||||
return +value
|
return +value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -22,15 +22,15 @@ function toNative(value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Return incremented id or uuid
|
// Return incremented id or uuid
|
||||||
function createId(coll) {
|
function createId (coll) {
|
||||||
if(_.isEmpty(coll)) {
|
if (_.isEmpty(coll)) {
|
||||||
return 1
|
return 1
|
||||||
} else {
|
} else {
|
||||||
var id = _.max(coll, function(doc) {
|
var id = _.max(coll, function (doc) {
|
||||||
return doc.id
|
return doc.id
|
||||||
}).id
|
}).id
|
||||||
|
|
||||||
if(_.isFinite(id)) {
|
if (_.isFinite(id)) {
|
||||||
// Increment integer id
|
// Increment integer id
|
||||||
return ++id
|
return ++id
|
||||||
} else {
|
} else {
|
||||||
@ -42,19 +42,19 @@ function createId(coll) {
|
|||||||
|
|
||||||
// Returns document ids that have unsatisfied relations
|
// Returns document ids that have unsatisfied relations
|
||||||
// Example: a comment that references a post that doesn't exist
|
// Example: a comment that references a post that doesn't exist
|
||||||
function getRemovable(db) {
|
function getRemovable (db) {
|
||||||
var removable = []
|
var removable = []
|
||||||
|
|
||||||
_(db).each(function(coll, collName) {
|
_(db).each(function (coll, collName) {
|
||||||
_(coll).each(function(doc) {
|
_(coll).each(function (doc) {
|
||||||
_(doc).each(function(value, key) {
|
_(doc).each(function (value, key) {
|
||||||
if(/Id$/.test(key)) {
|
if (/Id$/.test(key)) {
|
||||||
var refName = pluralize.plural(key.slice(0, -2))
|
var refName = pluralize.plural(key.slice(0, -2))
|
||||||
// Test if table exists
|
// Test if table exists
|
||||||
if(db[refName]) {
|
if (db[refName]) {
|
||||||
// Test if references is defined in table
|
// Test if references is defined in table
|
||||||
var ref = _.findWhere(db[refName], {id: value})
|
var ref = _.findWhere(db[refName], {id: value})
|
||||||
if(_.isUndefined(ref)) {
|
if (_.isUndefined(ref)) {
|
||||||
removable.push({name: collName, id: doc.id})
|
removable.push({name: collName, id: doc.id})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -66,22 +66,22 @@ function getRemovable(db) {
|
|||||||
return removable
|
return removable
|
||||||
}
|
}
|
||||||
|
|
||||||
function deepQuery(value, q) {
|
function deepQuery (value, q) {
|
||||||
if(value) {
|
if (value) {
|
||||||
if(_.isArray(value)) {
|
if (_.isArray(value)) {
|
||||||
for(var i = 0; i < value.length; i++) {
|
for (var i = 0; i < value.length; i++) {
|
||||||
if(deepQuery(value[i], q)) {
|
if (deepQuery(value[i], q)) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if(_.isPlainObject(value)) {
|
} else if (_.isPlainObject(value)) {
|
||||||
for(var k in value) {
|
for (var k in value) {
|
||||||
if(deepQuery(value[k], q)) {
|
if (deepQuery(value[k], q)) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if(value.toString().toLowerCase().indexOf(q) !== -1) {
|
} else if (value.toString().toLowerCase().indexOf(q) !== -1) {
|
||||||
return true;
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user