Skip to content

Commit b1ca61b

Browse files
committed
Fix code style
1 parent ce73c47 commit b1ca61b

File tree

8 files changed

+105
-100
lines changed

8 files changed

+105
-100
lines changed

bin/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ var argv = yargs
3333
.argv
3434

3535
// Start server function
36-
function start(object, filename) {
36+
function start (object, filename) {
3737
var port = process.env.PORT || argv.port
3838
var hostname = argv.host === '0.0.0.0' ? 'localhost' : argv.host
3939

4040
for (var prop in object) {
41-
console.log(chalk.gray(' http://' + hostname + ':' + port + '/') + chalk.cyan(prop))
41+
console.log(chalk.gray(' http://' + hostname + ':' + port + '/') + chalk.cyan(prop))
4242
}
4343

4444
console.log(
@@ -59,10 +59,11 @@ function start(object, filename) {
5959
}
6060
})
6161

62+
var router
6263
if (filename) {
63-
var router = jsonServer.router(filename)
64+
router = jsonServer.router(filename)
6465
} else {
65-
var router = jsonServer.router(object)
66+
router = jsonServer.router(object)
6667
}
6768

6869
var server = jsonServer.create()
@@ -90,7 +91,7 @@ if (/\.js$/.test(source)) {
9091
}
9192

9293
if (/^http/.test(source)) {
93-
got(source, function(err, data) {
94+
got(source, function (err, data) {
9495
if (err) throw err
9596
var object = JSON.parse(data)
9697
start(object)

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@
2727
"devDependencies": {
2828
"husky": "^0.6.1",
2929
"mocha": "^2.2.4",
30+
"standard": "^3.8.0",
3031
"supertest": "~0.8.1"
3132
},
3233
"scripts": {
33-
"test": "mocha -R spec test",
34+
"test": "standard && mocha -R spec test",
3435
"start": "node bin",
3536
"prepush": "npm t"
3637
},

src/defaults.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ var arr = []
88

99
// Logger
1010
arr.push(logger('dev', {
11-
skip: function(req, res) { return req.path === '/favicon.ico' }
11+
skip: function (req, res) { return req.path === '/favicon.ico' }
1212
}))
1313

1414
// Serve static files
1515
if (fs.existsSync(process.cwd() + '/public')) {
16-
arr.push(express.static(process.cwd() + '/public'));
16+
arr.push(express.static(process.cwd() + '/public'))
1717
} else {
18-
arr.push(express.static(__dirname + '/public'));
18+
arr.push(express.static(__dirname + '/public'))
1919
}
2020

2121
// CORS
@@ -26,4 +26,4 @@ if (process.env.NODE_ENV === 'development') {
2626
arr.push(errorhandler())
2727
}
2828

29-
module.exports = arr
29+
module.exports = arr

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ module.exports = {
88
},
99
defaults: require('./defaults'),
1010
router: require('./router')
11-
}
11+
}

src/router.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ low.mixin(require('underscore.inflections'))
1313
// utils.createId can generate incremental id or uuid
1414
low.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

src/utils.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ _.mixin(_inflections)
77
// Example:
88
// 'true' -> true
99
// '1' -> 1
10-
function toNative(value) {
10+
function toNative (value) {
1111
if (typeof value === 'string') {
1212
if (value === '' || value.trim() !== value) {
1313
return value
@@ -21,11 +21,11 @@ function toNative(value) {
2121
}
2222

2323
// Return incremented id or uuid
24-
function createId(coll) {
24+
function createId (coll) {
2525
if (_.isEmpty(coll)) {
2626
return 1
2727
} else {
28-
var id = _.max(coll, function(doc) {
28+
var id = _.max(coll, function (doc) {
2929
return doc.id
3030
}).id
3131

@@ -39,15 +39,14 @@ function createId(coll) {
3939
}
4040
}
4141

42-
4342
// Returns document ids that have unsatisfied relations
4443
// Example: a comment that references a post that doesn't exist
45-
function getRemovable(db) {
44+
function getRemovable (db) {
4645
var removable = []
4746

48-
_(db).each(function(coll, collName) {
49-
_(coll).each(function(doc) {
50-
_(doc).each(function(value, key) {
47+
_(db).each(function (coll, collName) {
48+
_(coll).each(function (doc) {
49+
_(doc).each(function (value, key) {
5150
if (/Id$/.test(key)) {
5251
var refName = _.pluralize(key.slice(0, -2))
5352
// Test if table exists
@@ -70,4 +69,4 @@ module.exports = {
7069
toNative: toNative,
7170
createId: createId,
7271
getRemovable: getRemovable
73-
}
72+
}

0 commit comments

Comments
 (0)