Permalink
Browse files

Reformatted code using eslintrc

  • Loading branch information...
omgimanerd committed Aug 12, 2017
1 parent 2056787 commit 00abb0b800eaca6bc958afec089c007ff0fab575
Showing with 851 additions and 107 deletions.
  1. +218 −0 .eslintrc.js
  2. +2 −1 README.md
  3. +52 −7 client/js/analytics.js
  4. +1 −0 package.json
  5. +15 −12 server.js
  6. +15 −3 server/ServerError.js
  7. +10 −9 server/analytics.js
  8. +22 −26 server/api.js
  9. +29 −30 server/formatter.js
  10. +4 −4 server/loggers.js
  11. +483 −15 yarn.lock
View
@@ -0,0 +1,218 @@
/**
* @fileoverview eslint configuration file.
* @author Alvin Lin (alvin@omgimanerd.tech)
*/
module.exports = {
'env': {
'browser': true,
'es6': true,
'node': true
},
'extends': 'eslint:recommended',
'parserOptions': {
'sourceType': 'module'
},
'rules': {
// Possible Errors
'no-await-in-loop': 'warn',
'no-extra-parens': ['error', 'all'],
'no-prototype-builtins': 'error',
'no-template-curly-in-string': 'error',
'valid-jsdoc': ['warn', {
'requireReturn': false,
'requireReturnType': true,
'requireReturnDescription': false
}],
// Best Practices
'array-callback-return': 'warn',
'block-scoped-var': 'warn',
'class-methods-use-this': 'warn',
'curly': ['warn', 'all'],
'dot-location': ['warn', 'object'],
'dot-notation': ['error', {
'allowKeywords': true
}],
'eqeqeq': ['error', 'always'],
'no-div-regex': 'warn',
'no-else-return': 'warn',
'no-empty-function': 'warn',
'no-eq-null': 'error',
'no-eval': 'warn',
'no-extend-native': 'warn',
'no-extra-bind': 'warn',
'no-extra-label': 'warn',
'no-floating-decimal': 'error',
'no-implicit-coercion': ['warn', {
'boolean': false
}],
'no-implicit-globals': 'warn',
'no-implied-eval': 'error',
'no-iterator': 'warn',
'no-labels': 'warn',
'no-lone-blocks': 'warn',
'no-loop-func': 'warn',
'no-multi-spaces': 'warn',
'no-multi-str': 'warn',
'no-new': 'warn',
'no-new-func': 'warn',
'no-new-wrappers': 'error',
'no-octal-escape': 'warn',
'no-param-reassign': 'warn',
'no-proto': 'warn',
'no-return-assign': 'error',
'no-return-await': 'warn',
'no-script-url': 'error',
'no-self-compare': 'error',
'no-sequences': 'warn',
'no-throw-literal': 'error',
'no-unmodified-loop-condition': 'error',
'no-unused-expressions': 'warn',
'no-useless-call': 'error',
'no-useless-concat': 'error',
'no-useless-return': 'error',
'no-void': 'error',
'no-with': 'error',
'prefer-promise-reject-errors': 'error',
'radix': 'warn',
'require-await': 'error',
'vars-on-top': 'warn',
'wrap-iife': ['error', 'outside'],
// Variables
'init-declarations': ['error', 'always'],
'no-catch-shadow': 'error',
'no-label-var': 'error',
'no-restricted-globals': 'warn',
'no-shadow': 'warn',
'no-shadow-restricted-names': 'error',
'no-undef-init': 'warn',
'no-undefined': 'error',
'no-use-before-define': 'error',
// Node.js and CommonJS
'global-require': 'warn',
'handle-callback-err': 'error',
'no-buffer-constructor': 'error',
'no-mixed-requires': ['warn', {
'grouping': true,
'allowCall': false
}],
'no-new-require': 'warn',
'no-path-concat': 'warn',
'no-process-exit': 'warn',
'no-sync': 'error',
// Stylistic Issues
'array-bracket-spacing': ['warn', 'never'],
'block-spacing': ['warn', 'always'],
'brace-style': ['warn', '1tbs', {
'allowSingleLine': true
}],
'camelcase': 'warn',
'capitalized-comments': ['warn', 'always'],
'comma-dangle': ['warn', 'never'],
'comma-spacing': 'warn',
'comma-style': ['warn', 'last'],
'computed-property-spacing': ['warn', 'never'],
'consistent-this': 'warn',
'eol-last': ['warn', 'always'],
'func-call-spacing': ['warn', 'never'],
'func-name-matching': ['warn', 'always'],
'func-names': ['warn', 'never'],
'func-style': ['warn', 'declaration', {
'allowArrowFunctions': true
}],
'indent': ['warn', 2],
'key-spacing': ['warn', {
'mode': 'strict'
}],
'keyword-spacing': 'warn',
'line-comment-position': ['warn', 'above'],
'linebreak-style': ['error', 'unix'],
'max-len': ['warn', {
'code': 80
}],
'multiline-ternary': ['warn', 'never'],
'new-cap': ['warn', {
'capIsNew': false,
}],
'new-parens': 'warn',
'no-array-constructor': 'warn',
'no-inline-comments': 'warn',
'no-lonely-if': 'warn',
'no-mixed-spaces-and-tabs': 'error',
'no-multiple-empty-lines': ['warn', {
'max': 2,
'maxEOF': 0,
'maxBOF': 0
}],
'no-nested-ternary': 'warn',
'no-new-object': 'warn',
'no-tabs': 'error',
'no-trailing-spaces': 'warn',
'no-underscore-dangle': 'warn',
'no-unneeded-ternary': 'warn',
'no-whitespace-before-property': 'warn',
'object-curly-newline': ['warn', {
'consistent': true,
}],
'object-curly-spacing': ['warn', 'always'],
'object-property-newline': ['warn', {
'allowMultiplePropertiesPerLine': true
}],
'operator-assignment': ['warn', 'always'],
'operator-linebreak': ['warn', 'after'],
'padded-blocks': ['warn', 'never'],
'quote-props': ['warn', 'as-needed', {
'keywords': true
}],
'quotes': ['warn', 'single'],
'require-jsdoc': ['warn', {
'require': {
'FunctionDeclaration': true,
'MethodDefinition': true,
'ClassDeclaration': true,
'ArrowFunctionExpression': true
}
}],
'semi': ['warn', 'never'],
'semi-spacing': 'warn',
'semi-style': ['warn', 'last'],
'space-before-blocks': ['warn', 'always'],
'space-before-function-paren': ['warn', 'never'],
'space-in-parens': ['warn', 'never'],
'space-infix-ops': 'warn',
'space-unary-ops': ['warn', {
'words': true,
'nonwords': false
}],
'spaced-comment': ['warn', 'always'],
'switch-colon-spacing': 'warn',
'template-tag-spacing': 'warn',
// ECMAScript 6
'arrow-parens': ['warn', 'as-needed'],
'arrow-spacing': 'warn',
'generator-star-spacing': ['warn', 'after'],
'no-confusing-arrow': ['error', {
'allowParens': false
}],
'no-duplicate-imports': 'error',
'no-useless-computed-key': 'warn',
'no-useless-rename': 'error',
'no-var': 'warn',
'object-shorthand': ['warn', 'consistent-as-needed'],
'prefer-arrow-callback': 'warn',
'prefer-const': 'warn',
'prefer-numeric-literals': 'warn',
'prefer-rest-params': 'warn',
'prefer-spread': 'warn',
'prefer-template': 'warn',
'rest-spread-spacing': 'warn',
'sort-imports': 'warn',
'symbol-description': 'warn',
'template-curly-spacing': 'warn',
'yield-star-spacing': ['warn', 'after']
}
}
View
@@ -72,7 +72,8 @@ curl getnews.tech/help
## Contributing
Fork this repository and send me a pull request with any suggestions and
changes. Use [StandardJS](https://standardjs.com/) to format your JavaScript.
changes. Use [ESLint](https://http://eslint.org/) to format your JavaScript
using the `.eslintrc.js` file included in the repository.
You will need to acquire an API Key from the [News API](https://newsapi.org/)
and a URL Shortener API Key from
View
@@ -11,36 +11,67 @@ require('../scss/analytics.scss')
const $ = require('jquery')
const c3 = require('c3')
// eslint-disable-next-line no-unused-vars
const d3 = require('d3')
const moment = require('moment')
const noUiSlider = require('nouislider')
/**
* Iterates through a date range day by day.
* @param {Date|number} min The start date.
* @param {Date|number} max The max date.
* @param {Function} callback The callback to run on each date.
*/
const iterByDay = (min, max, callback) => {
var tmp = moment(min)
for (const day = tmp; day.isBefore(max); day.add(1, 'day')) {
for (const day = moment(min); day.isBefore(max); day.add(1, 'day')) {
callback(day)
}
}
/**
* Given an analytics data series, returns the min and max date.
* @param {Object} data The analytics data series.
* @return {Object}
*/
const getDateRange = data => {
return data.length < 2 ? null : {
min: moment(data[0].timestamp).startOf('day'),
max: moment(data[data.length - 1].timestamp).endOf('day')
}
}
/**
* Returns the rounded minimum element in a list.
* @param {Array<number>} l The list to evaluate.
* @return {number}
*/
const min = l => {
return !l ? 0 : Math.round(Math.min(...l))
}
/**
* Returns the rounded average element in a list.
* @param {Array<number>} l The list to evaluate.
* @return {number}
*/
const avg = l => {
return !l ? 0 : Math.round(l.reduce((a, b) => a + b) / l.length)
}
/**
* Returns the rounded maximum element in a list.
* @param {Array<number>} l The list to evaluate.
* @return {number}
*/
const max = l => {
return !l ? 0 : Math.round(Math.max(...l))
}
/**
* Returns the parsed traffic data from the analytics data series.
* @param {Array<Object>} data The analytics data series.
* @return {Object}
*/
const getTrafficData = data => {
const hitsPerDay = new Map()
data.forEach(entry => {
@@ -52,12 +83,16 @@ const getTrafficData = data => {
const range = getDateRange(data)
iterByDay(range.min, range.max, day => {
dateColumn.push(day.format('YYYY-MM-DD'))
day = day.toString()
hitsPerDayColumn.push(hitsPerDay.get(day) || 0)
hitsPerDayColumn.push(hitsPerDay.get(day.toString()) || 0)
})
return [dateColumn, hitsPerDayColumn]
}
/**
* Returns the parsed response time data from the analytics data series.
* @param {Array<Object>} data The analytics data series.
* @return {Object}
*/
const getResponseTimeData = data => {
const timesByDay = {}
data.forEach(entry => {
@@ -82,10 +117,15 @@ const getResponseTimeData = data => {
return [dateColumn, maxColumn, avgColumn, minColumn]
}
/**
* Returns the parsed section frequency data from the analytics data series.
* @param {Array<Object>} data The analytics data series.
* @return {Object}
*/
const getSectionFrequencyData = data => {
const frequencies = new Map()
data.forEach(entry => {
const url = /\/([a-z\-]+)|$/g.exec(entry.req.url || '')[1] || 'help'
const url = /\/([a-z-]+)|$/g.exec(entry.req.url || '')[1] || 'help'
frequencies.set(url, (frequencies.get(url) || 0) + 1)
})
const sortedSlice = new Map(
@@ -97,6 +137,11 @@ const getSectionFrequencyData = data => {
]
}
/**
* Returns the parsed country frequency data from the analytics data series.
* @param {Array<Object>} data The analytics data series.
* @return {Object}
*/
const getCountryFrequencyData = data => {
const frequencies = new Map()
data.forEach(entry => {
@@ -118,7 +163,7 @@ const getCountryFrequencyData = data => {
$(document).ready(() => {
const dateSlider = document.getElementById('date-slider')
$.post('/analytics', data => {
if (data.length == 0) {
if (data.length === 0) {
window.alert('No data retrieved!')
throw new Error('No data retrieved!')
}
@@ -202,7 +247,7 @@ $(document).ready(() => {
const filteredData = data.filter(entry => {
return moment(entry.timestamp).isBetween(sliderRange[0], sliderRange[1])
})
if (filteredData.length == 0) {
if (filteredData.length === 0) {
window.alert('No data in this time segment!')
return
}
View
@@ -32,6 +32,7 @@
"winston-mail": "^1.3.0"
},
"devDependencies": {
"eslint": "^4.4.1",
"gulp": "^3.9.1",
"gulp-gjslint": "^0.1.5",
"merge-stream": "^1.0.1"
Oops, something went wrong.

0 comments on commit 00abb0b

Please sign in to comment.