Skip to content

Commit

Permalink
code review + eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
germanattanasio committed May 18, 2016
1 parent db94f03 commit af851ed
Show file tree
Hide file tree
Showing 19 changed files with 152 additions and 130 deletions.
3 changes: 1 addition & 2 deletions .cfignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
node_modules
run.sh
VCAP_SERVICES.json
coverage
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
public/js/vendors/prism.js
16 changes: 16 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"rules": {
"no-console": 0,
"func-names": 0,
"vars-on-top": 0,
"consistent-return": 0
},
"globals": {
describe: true,
it: true
},
"env": {
"node": true
},
"extends": "eslint-config-airbnb-es5",
}
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules
.DS_Store
run.sh
VCAP_SERVICES.json
.env
coverage
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: node_js
sudo: true
node_js:
- stable
node_js: stable
script:
- npm run lint
- npm test
env:
global:
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ If you want to contribute to the repository, here's a quick guide:

1. Fork the repo.
1. develop your code changes: `npm install -d`
1. test your code changes: `npm run lint && npm test`
1. Commit your changes
1. Push to your fork and submit a pull request
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ applications:
- services:
- tone-analyzer-service
name: <application-name>
command: node server.js
command: npm start
path: .
memory: 256M
```
Expand All @@ -46,7 +46,7 @@ applications:
5. Create the Tone Analyzer Service in Bluemix

```sh
$ cf create-service tone_analyzer beta tone-analyzer-service
$ cf create-service tone_analyzer standard tone-analyzer-service
```

6. Push it live!
Expand Down Expand Up @@ -79,7 +79,7 @@ See the full [Getting Started][getting_started] documentation for more details,
},
"label": "tone_analyzer",
"name": "tone-analyzer-service",
"plan": "beta"
"plan": "standard"
}]
}
}
Expand All @@ -91,7 +91,7 @@ See the full [Getting Started][getting_started] documentation for more details,
3. Go to the project folder in a terminal and run:
`npm install`
4. Start the application
5. `node server.js`
5. `npm start`
6. Go to `http://localhost:3000`

## Troubleshooting
Expand Down
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require('./config/express')(app);

// Create the service wrapper
var toneAnalyzer = watson.tone_analyzer({
url: 'https://gateway.watsonplatform.net/tone-analyzer-beta/api/',
url: 'https://gateway.watsonplatform.net/tone-analyzer/api/',
username: '<username>',
password: '<password>',
version_date: '2016-05-19',
Expand Down
8 changes: 3 additions & 5 deletions config/error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

'use strict';

module.exports = function (app) {

module.exports = function(app) {
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
Expand All @@ -33,8 +32,7 @@ module.exports = function (app) {
error: err.error || err.message
};
console.log('error:', error);

next ? '' : '';
res.status(error.code).json(error);
});

};
};
17 changes: 10 additions & 7 deletions config/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,26 @@
'use strict';

// Module dependencies
var express = require('express'),
bodyParser = require('body-parser');
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');

module.exports = function (app) {
module.exports = function(app) {
app.enable('trust proxy');

// Configure Express
app.set('view engine', 'ejs');
require('ejs').delimiter = '$';
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());

// Setup static public directory
app.use(express.static(__dirname + '/../public'));
app.use(express.static(path.join(__dirname, '..', '/public')));

// Only loaded when VCAP_APPLICATION is `true`
if (process.env.VCAP_APPLICATION)
if (process.env.VCAP_APPLICATION) {
require('./security')(app);

}
};
15 changes: 7 additions & 8 deletions config/security.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
'use strict';

// security.js
var rateLimit = require('express-rate-limit'),
helmet = require('helmet'),
csrf = require('csurf'),
cookieParser = require('cookie-parser');
var rateLimit = require('express-rate-limit');
var helmet = require('helmet');
var csrf = require('csurf');
var cookieParser = require('cookie-parser');

module.exports = function (app) {
module.exports = function(app) {
app.enable('trust proxy');

// 1. helmet with defaults
Expand All @@ -34,9 +34,9 @@ module.exports = function (app) {
delayMs: 0,
max: 6,
message: JSON.stringify({
error:'Too many requests, please try again in 30 seconds.',
error: 'Too many requests, please try again in 30 seconds.',
code: 429
}),
})
}));

// 3. setup cookies
Expand All @@ -46,7 +46,6 @@ module.exports = function (app) {
// 4. csrf
var csrfProtection = csrf({ cookie: true });
app.get('/', csrfProtection, function(req, res, next) {
console.log(req.csrfToken());
req._csrfToken = req.csrfToken();
next();
});
Expand Down
2 changes: 1 addition & 1 deletion manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
declared-services:
tone-analyzer-service:
label: tone_analyzer
plan: beta
plan: standard
applications:
- services:
- tone-analyzer-service
Expand Down
19 changes: 12 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,34 @@
},
"scripts": {
"start": "node server.js",
"test": "npm run jshint && (npm run codecov || true)",
"jshint": "jshint --exclude ./node_modules/",
"codecov": "istanbul cover ./node_modules/mocha/bin/_mocha && codecov"
"test": "istanbul cover ./node_modules/mocha/bin/_mocha",
"lint": "eslint .",
"autofix": "eslint --fix .",
"codecov": "npm run test && (codecov || true)"
},
"dependencies": {
"body-parser": "^1.13.2",
"cf-deployment-tracker-client": "0.0.8",
"cookie-parser": "^1.4.1",
"csurf": "^1.8.3",
"dotenv": "^2.0.0",
"ejs": "^2.3.4",
"errorhandler": "^1.4.1",
"express": "^4.12.2",
"express-rate-limit": "^2.1.0",
"express-secure-only": "^0.2.1",
"helmet": "^1.3.0",
"watson-developer-cloud": "https://github.com/watson-developer-cloud/node-sdk.git"
"watson-developer-cloud": "^1.8.0"
},
"devDependencies": {
"babel-eslint": "^6.0.4",
"codecov": "^1.0.1",
"eslint": "^2.8.0",
"eslint-config-airbnb-es5": "^1.0.9",
"eslint-plugin-react": "^5.1.1",
"istanbul": "^0.4.2",
"jshint": "^2.9.1",
"mocha": "^2.4.5",
"nock": "7.7.0",
"supertest": "^1.2.0"
"supertest": "^1.2.0",
"nock": "7.7.0"
}
}
54 changes: 27 additions & 27 deletions public/js/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@
*/
function App(documentTones, sentences, thresholds, selectedSample) {
var _selectedFilter = 'Anger',
_selectedTone = 'Emotion Tone',
_selectedSample = selectedSample || 'customer-call',
_lowToHigh = false,
_currentHoveredOriginalSentence = document.querySelector('body'),
_rankedSentences = sentences,
_originalSentences,
_documentTones = documentTones,
_thresholds = thresholds,
_isHoveringOriginalText = false,
_socialToneHoverTexts,
_toneHash,
TONE_CATEGORIES_RESET = [{
_selectedTone = 'Emotion Tone',
_selectedSample = selectedSample || 'customer-call',
_lowToHigh = false,
_currentHoveredOriginalSentence = document.querySelector('body'),
_rankedSentences = sentences,
_originalSentences,
_documentTones = documentTones,
_thresholds = thresholds,
_isHoveringOriginalText = false,
_socialToneHoverTexts,
_toneHash,
TONE_CATEGORIES_RESET = [{
tones: [{
score: 0,
tone_id: 'Anger',
Expand Down Expand Up @@ -129,10 +129,10 @@ function App(documentTones, sentences, thresholds, selectedSample) {
category_id: 'social_tone',
category_name: 'Social Tone'
}], SCORE_DECIMAL_PLACE = 2,
PERCENTAGE_DECIMAL_PLACE = 1,
SOCIAL_TONE_MIN_RANGE = -1,
SOCIAL_TONE_MAX_RANGE = 1,
output = {};
PERCENTAGE_DECIMAL_PLACE = 1,
SOCIAL_TONE_MIN_RANGE = -1,
SOCIAL_TONE_MAX_RANGE = 1,
output = {};

/**
* Make sure sentences have proper tone data values.
Expand Down Expand Up @@ -165,9 +165,9 @@ function App(documentTones, sentences, thresholds, selectedSample) {
*/
function _toneLevel(toneKey, score, classNameType) {
var output,
toneValue = _toneHash[toneKey],
newScore = score,
baseThreshold = 0;
toneValue = _toneHash[toneKey],
newScore = score,
baseThreshold = 0;

if (newScore <= baseThreshold)
output = '';
Expand Down Expand Up @@ -292,15 +292,15 @@ function App(documentTones, sentences, thresholds, selectedSample) {
return b.tone_categories[_searchIndex(_selectedTone)].tones[_searchIndex(_selectedFilter)].score
- a.tone_categories[_searchIndex(_selectedTone)].tones[_searchIndex(_selectedFilter)].score;
},
map = function(item) {
map = function(item) {
var score = item.tone_categories[_searchIndex(_selectedTone)].tones[_searchIndex(_selectedFilter)].score.toFixed(SCORE_DECIMAL_PLACE);
return {
text: item.text,
score: score,
className: 'sentence-rank--score_' + normalize(_selectedFilter)
};
},
filter = (_selectedTone === 'Social Tone') ?
filter = (_selectedTone === 'Social Tone') ?
function(item) {
return item.score >= -1;
} :
Expand Down Expand Up @@ -348,17 +348,17 @@ function App(documentTones, sentences, thresholds, selectedSample) {
*/
output.selectFilterBySample = function() {
var getHighestTone = function(toneCategory) {
var highestTone = _documentTones.tone_categories[_searchIndex(toneCategory)].tones[0].tone_name,
highestScore = 0;
_documentTones.tone_categories[_searchIndex(toneCategory)].tones.forEach(function(item) {
var highestTone = _documentTones.tone_categories[_searchIndex(toneCategory)].tones[0].tone_name,
highestScore = 0;
_documentTones.tone_categories[_searchIndex(toneCategory)].tones.forEach(function(item) {
if (highestScore < item.score) {
highestScore = item.score;
highestTone = item.tone_name;
}
});
return highestTone;
},
sample = {
return highestTone;
},
sample = {
'customer-call': getHighestTone('Emotion Tone'),
'email': getHighestTone('Social Tone'),
'corporate-announcement': getHighestTone('Language Tone'),
Expand Down
20 changes: 10 additions & 10 deletions public/js/components/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,18 @@ function scrollTo($element) {
* @return {Array} arr
*/
function move(arr, old_index, new_index) {
while (old_index < 0) {
old_index += arr.length;
while (old_index < 0) {
old_index += arr.length;
}
while (new_index < 0) {
new_index += arr.length;
while (new_index < 0) {
new_index += arr.length;
}
if (new_index >= arr.length) {
var k = new_index - arr.length;
while ((k--) + 1) {
arr.push(undefined);
if (new_index >= arr.length) {
var k = new_index - arr.length;
while ((k--) + 1) {
arr.push(undefined);
}
}
arr.splice(new_index, 0, arr.splice(old_index, 1)[0]);
return arr;
arr.splice(new_index, 0, arr.splice(old_index, 1)[0]);
return arr;
}
4 changes: 2 additions & 2 deletions public/js/components/tab-panels.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Tabbed Panels js
*/
(function() {
$('.tab-panels--tab').click(function(e){
$('.tab-panels--tab').click(function(e) {
e.preventDefault();
var self = $(this);
var inputGroup = self.closest('.tab-panels');
Expand All @@ -13,4 +13,4 @@ Tabbed Panels js
idName = self.attr('href');
$(idName).addClass('active');
});
})();
})();
Loading

0 comments on commit af851ed

Please sign in to comment.