Skip to content

Commit a7795b5

Browse files
committed
ajust code format to project style
Fixup the code style to fit the canned project
1 parent 897660f commit a7795b5

File tree

3 files changed

+38
-36
lines changed

3 files changed

+38
-36
lines changed

canned.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ Canned.prototype.respondWithAny = function (httpObj, files, cb) {
242242
}
243243

244244
Canned.prototype.responder = function(body, req, res) {
245+
var responseHandler
245246
var httpObj = {}
246247
var that = this
247248
var parsedurl = url.parse(req.url)
@@ -263,13 +264,10 @@ Canned.prototype.responder = function(body, req, res) {
263264
return response.send()
264265
}
265266

266-
var paths = lookup.getPaths(httpObj.pathname.join('/'), that.wildcard);
267+
var paths = lookup(httpObj.pathname.join('/'), that.wildcard);
267268
paths.splice(0,1); // The first path is the default
268269

269-
// Find a response for the first path
270-
that.findResponse(httpObj, responseHandler);
271-
272-
function responseHandler(err, resp) {
270+
responseHandler = function (err, resp) {
273271
if (err) {
274272
// Try more paths, if there are any still
275273
if (paths.length > 0) {
@@ -283,6 +281,10 @@ Canned.prototype.responder = function(body, req, res) {
283281
}
284282
return resp.send();
285283
}
284+
285+
// Find a response for the first path
286+
that.findResponse(httpObj, responseHandler);
287+
286288
}
287289

288290
Canned.prototype.findResponse = function(httpObj, cb) {
@@ -292,16 +294,16 @@ Canned.prototype.findResponse = function(httpObj, cb) {
292294
if (err) {
293295
that._responseForFile(httpObj, files, function (err, resp) {
294296
if (err) {
295-
that.respondWithAny(httpObj, files, cb);
297+
that.respondWithAny(httpObj, files, cb)
296298
} else {
297-
return cb(null, resp);
299+
cb(null, resp)
298300
}
299301
})
300302
} else {
301303
if (stats.isDirectory()) {
302-
that.respondWithDir(httpObj, cb);
304+
that.respondWithDir(httpObj, cb)
303305
} else {
304-
return cb(null, new Response('html', '', 500, httpObj.res));
306+
cb(null, new Response('html', '', 500, httpObj.res))
305307
}
306308
}
307309
})

lib/lookup.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var lookup = module.exports = {};
1+
"use strict";
2+
23

34
/***
45
* Given a path and a wildcard string, return a list of paths
@@ -28,47 +29,47 @@ var lookup = module.exports = {};
2829
* @returns {Array} - the resulting combination, ordered by specificity
2930
*/
3031

31-
lookup.getPaths = function (path, wildcard) {
32-
"use strict";
32+
var lookup = module.exports = function (path, wildcard) {
3333

3434
// Split the path and calculate how many paths will be generated
35-
var parts = path.split('/');
36-
var matches = path.match(/\/\d+(\/|$)/gi);
35+
var parts = path.split('/')
36+
var matches = path.match(/\/\d+(\/|$)/gi)
37+
var i
3738

3839
if (!matches){
39-
return [path];
40+
return [path]
4041
}
4142

4243
var lookPathsParts = [];
4344

4445
// Locate replaceable parts indexes
45-
var matchesIndexes = [];
46+
var matchesIndexes = []
4647
parts.forEach(function (p, i) {
4748
if (p.match(/^\d+$/)) {
48-
matchesIndexes.push(i);
49+
matchesIndexes.push(i)
4950
}
50-
});
51+
})
5152

5253
// Copy the original parts as a starting point for the new parts
53-
for (var i = 0; i < Math.pow(2, matches.length); i++) {
54-
lookPathsParts.push(parts.slice());
54+
for (i = 0; i < Math.pow(2, matches.length); i++) {
55+
lookPathsParts.push(parts.slice())
5556
}
5657

5758
// Generate the new paths parts
58-
for (var i = matches.length; i > 0; --i) {
59-
var skip = Math.pow(2, i) / 2;
60-
var replacePartIndex = matches.length - i;
59+
for (i = matches.length; i > 0; --i) {
60+
var skip = Math.pow(2, i) / 2
61+
var replacePartIndex = matches.length - i
6162
for (var j = skip; j < lookPathsParts.length; j += (skip * 2)) {
6263
for (var k = 0; k < skip; k++) {
63-
lookPathsParts[j + k][matchesIndexes[replacePartIndex]] = wildcard;
64+
lookPathsParts[j + k][matchesIndexes[replacePartIndex]] = wildcard
6465
}
6566
}
6667
}
6768

6869
// Build the final path strings
69-
var lookPaths = [];
70+
var lookPaths = []
7071
lookPathsParts.forEach(function (p) {
71-
lookPaths.push(p.join('/'));
72-
});
73-
return lookPaths;
74-
};
72+
lookPaths.push(p.join('/'))
73+
})
74+
return lookPaths
75+
}

spec/lookup.spec.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"use strict";
22

3-
var lookup = require('../lib/lookup');
3+
var lookup = require('../lib/lookup')
44

55
describe('lookup', function () {
66
it('should generate a list of paths in the correct order', function (done) {
7-
var testPath = '/api/2/customer/123/invoice/321/';
7+
var testPath = '/api/2/customer/123/invoice/321/'
88
var expectedPaths = [
99
'/api/2/customer/123/invoice/321/',
1010
'/api/2/customer/123/invoice/any/',
@@ -14,9 +14,8 @@ describe('lookup', function () {
1414
'/api/any/customer/123/invoice/any/',
1515
'/api/any/customer/any/invoice/321/',
1616
'/api/any/customer/any/invoice/any/'
17-
];
18-
var generatedPaths = lookup.getPaths(testPath, 'any');
19-
expect(generatedPaths).toEqual(expectedPaths);
20-
done();
17+
]
18+
expect(lookup(testPath, 'any')).toEqual(expectedPaths)
19+
done()
2120
});
22-
});
21+
});

0 commit comments

Comments
 (0)