1 | var express = require('express'); |
||
2 | var extend = require('extend'); |
||
3 | |||
4 | // ------------------------------------------ |
||
5 | // CONSTRUCTOR |
||
6 | // ------------------------------------------ |
||
7 | |||
8 | var init = function(){ |
||
9 | info('Starting ProfileManager ...'); |
||
10 | |||
11 | // Expose properties to global |
||
12 | var Profile = {} |
||
13 | global.Profile = Profile; |
||
14 | |||
15 | return ProfileManager; |
||
16 | } |
||
17 | |||
18 | // ------------------------------------------ |
||
19 | // ROUTER |
||
20 | // ------------------------------------------ |
||
21 | |||
22 | |||
23 | var updateProfile = function(req, res, next) { |
||
24 | |||
25 | var name = req.params.name; |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
26 | var query = {}; |
||
27 | var profile = {}; |
||
28 | |||
29 | var keys = Object.keys(req.query); |
||
30 | for(var i=0 ; i < keys.length ; i++){ |
||
31 | var key = keys[i]; |
||
32 | var value = req.query[key]; |
||
33 | |||
34 | if (!key.startsWith('profile_')){ |
||
35 | query[key] = value; |
||
36 | continue; |
||
37 | } |
||
38 | |||
39 | profile[key.substring(8)] = parse(value); |
||
40 | } |
||
41 | |||
42 | if (profile.face){ |
||
43 | |||
44 | // Merge previous with new profile |
||
45 | var previous = Profile[profile.face]; |
||
0 ignored issues
–
show
The variable
Profile seems to be never declared. If this is a global, consider adding a /** global: Profile */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
46 | if (previous){ |
||
47 | extend(true, previous, profile); |
||
48 | profile = previous; |
||
49 | } |
||
50 | |||
51 | // Save profile for given face |
||
52 | Profile[profile.face] = profile; |
||
53 | } |
||
54 | |||
55 | // Local profile |
||
56 | req.query = query; |
||
57 | req.query.profile = profile; |
||
58 | Profile.last = profile; |
||
59 | |||
60 | next(); |
||
61 | } |
||
62 | |||
63 | var parse = function(str){ |
||
64 | if (str === 'true') return true; |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
65 | if (str === 'false') return false; |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
66 | var num = parseInt(str); |
||
67 | return isNaN(num) ? str : num; |
||
68 | } |
||
69 | |||
70 | var Router = express.Router(); |
||
71 | |||
72 | Router.all('/sarah/:name', updateProfile); |
||
73 | Router.all('/standby', updateProfile); |
||
74 | Router.all('/motion', updateProfile); |
||
75 | Router.all('/askme', updateProfile); |
||
76 | |||
77 | |||
78 | // ------------------------------------------ |
||
79 | // PUBLIC |
||
80 | // ------------------------------------------ |
||
81 | |||
82 | var ProfileManager = { |
||
83 | 'init' : init, |
||
84 | 'Router' : Router |
||
85 | } |
||
86 | |||
87 | // Exports Manager |
||
88 | exports.init = ProfileManager.init; |