Skip to content

Commit db7712a

Browse files
Ryan CrosserRyan Crosser
authored andcommitted
replaced underscore with lodash, expanded querying capability to search through nested objects and arrays
1 parent 7af6e40 commit db7712a

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
"errorhandler": "^1.2.0",
1515
"express": "^4.9.5",
1616
"got": "^1.2.2",
17+
"lodash": "^3.9.1",
1718
"lowdb": "^0.7.1",
1819
"method-override": "^2.1.2",
1920
"morgan": "^1.3.1",
2021
"node-uuid": "^1.4.2",
2122
"pluralize": "^1.1.2",
22-
"underscore": "^1.5.2",
2323
"underscore-db": "^0.8.0",
2424
"update-notifier": "^0.2.2",
2525
"yargs": "^1.3.1"

src/router.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var express = require('express')
22
var methodOverride = require('method-override')
33
var bodyParser = require('body-parser')
4-
var _ = require('underscore')
4+
var _ = require('lodash')
55
var low = require('lowdb')
66
var pluralize = require('pluralize')
77
var utils = require('./utils')
@@ -78,7 +78,7 @@ module.exports = function (source) {
7878
array = db(req.params.resource).filter(function (obj) {
7979
for (var key in obj) {
8080
var value = obj[key]
81-
if (_.isString(value) && value.toLowerCase().indexOf(q) !== -1) {
81+
if(utils.deepQuery(value, q)){
8282
return true
8383
}
8484
}

src/utils.js

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
var _ = require('underscore')
1+
var _ = require('lodash')
22
var uuid = require('node-uuid')
33
var 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 (/Id$/.test(key)) {
48+
_(db).each(function(coll, collName) {
49+
_(coll).each(function(doc) {
50+
_(doc).each(function(value, key) {
51+
if(/Id$/.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+
6989
module.exports = {
7090
toNative: toNative,
7191
createId: createId,
72-
getRemovable: getRemovable
92+
getRemovable: getRemovable,
93+
deepQuery: deepQuery
7394
}

0 commit comments

Comments
 (0)