From fd653de0e174d07fc17a73bee01c22ef53c537b5 Mon Sep 17 00:00:00 2001 From: Balazs Gobel Date: Wed, 13 Feb 2019 14:10:25 +0100 Subject: [PATCH] Fix most of the eslint warnings --- .eslintrc.js | 14 +++--- src/definition/formatted.js | 4 +- src/definition/metric-semaphores.js | 2 +- src/excel-export.js | 26 +++++------ src/masking.js | 67 +++++++++++++++-------------- src/paint.jsx | 2 +- src/utilities.js | 22 +++++----- 7 files changed, 70 insertions(+), 67 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 2328397..92699c1 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -41,12 +41,12 @@ module.exports = { "no-cond-assign": ["warn"], "no-fallthrough": ["warn"], "no-undef": ["error"], - "no-unused-vars": ["warn"], - "no-use-before-define": ["warn", { "functions": false, "classes": false, "variables": false }], + "no-unused-vars": ["error"], + "no-use-before-define": ["error", { "functions": false, "classes": false, "variables": false }], "no-useless-escape": ["warn"], "no-useless-return": ["warn"], "no-underscore-dangle": ["warn", { "allow": ["_id"] }], - "no-redeclare": ["warn"], + "no-redeclare": ["error"], "no-restricted-syntax": ["warn"], "operator-linebreak": ["warn", "before"], "prefer-promise-reject-errors": ["warn"], @@ -63,11 +63,11 @@ module.exports = { "complexity": ["warn"], "camelcase": ["warn"], "max-statements": ["off"], // marks the entire functions, a bit too noisy - "sort-vars": ["warn"], + "sort-vars": ["off"], // not much value for the work "init-declarations": ["off"], "capitalized-comments": ["off"], "one-var": ["off"], - "no-var": ["warn"], + "no-var": ["error"], "no-plusplus": ["warn"], "vars-on-top": ["off"], "no-magic-numbers": ["off"], // useful, but also complains for reasonable checks with actual numbers @@ -80,7 +80,7 @@ module.exports = { "quote-props": ["off"], "prefer-template": ["warn"], "no-lonely-if": ["warn"], - "sort-keys": ["warn"], + "sort-keys": ["off"], // not much value for the work "no-implicit-coercion": ["warn"], "no-inline-comments": ["off"], "spaced-comment": ["warn"], @@ -107,7 +107,7 @@ module.exports = { "strict": ["warn"], "no-ternary": ["off"], "multiline-ternary": ["off"], - "no-param-reassign": ["warn"], + "no-param-reassign": ["error"], "prefer-destructuring": ["warn"], "arrow-parens": ["off"], "no-array-constructor": ["warn"], diff --git a/src/definition/formatted.js b/src/definition/formatted.js index 3e1cce6..8ca8142 100644 --- a/src/definition/formatted.js +++ b/src/definition/formatted.js @@ -78,7 +78,7 @@ const formatted = { ], defaultValue: 'Clean', show (data) { - return data.customfilebool == false; + return !data.customfilebool; } }, BodyTextColor: { @@ -130,7 +130,7 @@ const formatted = { ], defaultValue: 'Black', show (data) { - return data.customfilebool == false; + return !data.customfilebool; } }, FontFamily: { diff --git a/src/definition/metric-semaphores.js b/src/definition/metric-semaphores.js index 73bbc61..fbd1c38 100644 --- a/src/definition/metric-semaphores.js +++ b/src/definition/metric-semaphores.js @@ -25,7 +25,7 @@ const metricSemaphores = { type: 'string', defaultValue: '0', show (data) { - return data.allmetrics == false; + return !data.allmetrics; } }, MetricStatus1: { diff --git a/src/excel-export.js b/src/excel-export.js index 9734e8c..2440f53 100644 --- a/src/excel-export.js +++ b/src/excel-export.js @@ -1,11 +1,11 @@ -import $ from 'jquery'; +import $ from 'jquery'; // eslint-disable-line id-length const isIE = /* @cc_on!@*/false || Boolean(document.documentMode); const isChrome = Boolean(window.chrome) && Boolean(window.chrome.webstore); const isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0; const isFirefox = typeof InstallTrigger !== 'undefined'; -export function enableExcelExport (layout, f) { +export function enableExcelExport (layout, htmlMarkupString) { let myTitle = ''; let mySubTitle = ''; let myFootNote = ''; @@ -43,18 +43,18 @@ export function enableExcelExport (layout, f) { $.preventDefault(); } if (isIE) { - let a = ''; - a += myTitle + mySubTitle + myFootNote; - a += f; - a = a.split('>.<').join('><'); - a += ''; + let htmlMarkup = ''; + htmlMarkup += myTitle + mySubTitle + myFootNote; + htmlMarkup += htmlMarkupString; + htmlMarkup = htmlMarkup.split('>.<').join('><'); + htmlMarkup += ''; - const w = window.open(); - w.document.open(); - w.document.write(a); - w.document.close(); - w.document.execCommand('SaveAs', true, 'Analysis.xls' || 'c:\TMP'); - w.close(); + const newWindow = window.open(); + newWindow.document.open(); + newWindow.document.write(htmlMarkup); + newWindow.document.close(); + newWindow.document.execCommand('SaveAs', true, 'Analysis.xls' || 'c:\TMP'); + newWindow.close(); } if (isFirefox) { diff --git a/src/masking.js b/src/masking.js index 5d10373..5cda4af 100644 --- a/src/masking.js +++ b/src/masking.js @@ -13,7 +13,7 @@ export function ApplyPreMask (mask, value) { // aqui case '+#,##0': return (addSeparators(value, ',', '.', 0)); default: - return (ApplyMask(mask.substring(0, mask.indexOf(';')), value)); + return (applyMask(mask.substring(0, mask.indexOf(';')), value)); } } else { const vMyValue = value * -1; @@ -30,46 +30,47 @@ export function ApplyPreMask (mask, value) { // aqui case '-#,##0': return (`(${addSeparators(vMyValue, ',', '.', 0)})`); default: - return (`(${ApplyMask(vMyMask, vMyValue)})`); + return (`(${applyMask(vMyMask, vMyValue)})`); } } } else { - return (ApplyMask(mask, value)); + return (applyMask(mask, value)); } } -function ApplyMask (mask, value) { - if (!mask || isNaN(Number(value))) { - return value; // return as it is. +function applyMask (originalMask, originalValue) { + if (!originalMask || isNaN(Number(originalValue))) { + return originalValue; } - let isNegative, result, decimal, group, posLeadZero, posTrailZero, posSeparator, - part, szSep, integer, - - // find prefix/suffix - len = mask.length, - start = mask.search(/[0-9\-\+#]/), - prefix = start > 0 ? mask.substring(0, start) : '', - // reverse string: not an ideal method if there are surrogate pairs - str = mask.split('').reverse() - .join(''), - end = str.search(/[0-9\-\+#]/), - offset = len - end, - substr = mask.substring(offset, offset + 1), - indx = offset + ((substr === '.' || (substr === ',')) ? 1 : 0), - suffix = end > 0 ? mask.substring(indx, len) : ''; + let isNegative; + let result; + let integer; + // find prefix/suffix + let len = originalMask.length; + const start = originalMask.search(/[0-9\-\+#]/); + const prefix = start > 0 ? originalMask.substring(0, start) : ''; + // reverse string: not an ideal method if there are surrogate pairs + let str = originalMask.split('') + .reverse() + .join(''); + const end = str.search(/[0-9\-\+#]/); + let offset = len - end; + const substr = originalMask.substring(offset, offset + 1); + let index = offset + ((substr === '.' || (substr === ',')) ? 1 : 0); + const suffix = end > 0 ? originalMask.substring(index, len) : ''; // mask with prefix & suffix removed - mask = mask.substring(start, indx); + let mask = originalMask.substring(start, index); // convert any string to number according to formation sign. - value = mask.charAt(0) === '-' ? -value : Number(value); + let value = mask.charAt(0) === '-' ? -originalValue : Number(originalValue); isNegative = value < 0 ? value = -value : 0; // process only abs(), and turn on flag. // search for separator for grp & decimal, anything not digit, not +/- sign, not #. result = mask.match(/[^\d\-\+#]/g); - decimal = (result && result[result.length - 1]) || '.'; // treat the right most symbol as decimal - group = (result && result[1] && result[0]) || ','; // treat the left most symbol as group separator + const decimal = (result && result[result.length - 1]) || '.'; // treat the right most symbol as decimal + const group = (result && result[1] && result[0]) || ','; // treat the left most symbol as group separator // split the decimal for the format string if any. mask = mask.split(decimal); @@ -78,16 +79,16 @@ function ApplyMask (mask, value) { value = String(Number(value)); // convert number to string to trim off *all* trailing decimal zero(es) // fill back any trailing zero according to format - posTrailZero = mask[1] && mask[1].lastIndexOf('0'); // look for last zero in format - part = value.split('.'); + const posTrailZero = mask[1] && mask[1].lastIndexOf('0'); // look for last zero in format + const part = value.split('.'); // integer will get !part[1] if (!part[1] || (part[1] && part[1].length <= posTrailZero)) { value = (Number(value)).toFixed(posTrailZero + 1); } - szSep = mask[0].split(group); // look for separator + const szSep = mask[0].split(group); // look for separator mask[0] = szSep.join(''); // join back without separator for counting the pos of any leading 0. - posLeadZero = mask[0] && mask[0].indexOf('0'); + const posLeadZero = mask[0] && mask[0].indexOf('0'); if (posLeadZero > -1) { while (part[0].length < (mask[0].length - posLeadZero)) { part[0] = `0${part[0]}`; @@ -101,17 +102,17 @@ function ApplyMask (mask, value) { // process the first group separator from decimal (.) only, the rest ignore. // get the length of the last slice of split result. - posSeparator = (szSep[1] && szSep[szSep.length - 1].length); + const posSeparator = (szSep[1] && szSep[szSep.length - 1].length); if (posSeparator) { integer = value[0]; str = ''; offset = integer.length % posSeparator; len = integer.length; - for (indx = 0; indx < len; indx++) { - str += integer.charAt(indx); // ie6 only support charAt for sz. + for (index = 0; index < len; index++) { + str += integer.charAt(index); // ie6 only support charAt for sz. // -posSeparator so that won't trail separator on full length // jshint -W018 - if (!((indx - offset + 1) % posSeparator) && indx < len - posSeparator) { + if (!((index - offset + 1) % posSeparator) && index < len - posSeparator) { str += group; } } diff --git a/src/paint.jsx b/src/paint.jsx index f16a912..1cb7ce1 100644 --- a/src/paint.jsx +++ b/src/paint.jsx @@ -1,4 +1,4 @@ -import $ from 'jquery'; +import $ from 'jquery'; // eslint-disable-line id-length import initializeStore from './store'; import React from 'react'; import ReactDOM from 'react-dom'; diff --git a/src/utilities.js b/src/utilities.js index 3ad9f54..ba1b069 100644 --- a/src/utilities.js +++ b/src/utilities.js @@ -9,17 +9,19 @@ export function distinctArray (array) { .map(entry => JSON.parse(entry)); } -export function addSeparators (nStr, thousandsSep, decimalSep, numDecimals) { - let x1; - nStr = nStr.toFixed(numDecimals); - const x = nStr.split('.'); - x1 = x[0]; - const x2 = x.length > 1 ? decimalSep + x[1] : ''; - const rgx = /(\d+)(\d{3})/; - while (rgx.test(x1)) { - x1 = x1.replace(rgx, `$1${thousandsSep}$2`); +export function addSeparators (number, thousandSeparator, decimalSeparator, numberOfDecimals) { + const numberString = number.toFixed(numberOfDecimals); + const numberStringParts = numberString.split('.'); + let [ + wholeNumber, + decimal + ] = numberStringParts; + decimal = numberStringParts.length > 1 ? decimalSeparator + decimal : ''; + const regexCheckForThousand = /(\d+)(\d{3})/; + while (regexCheckForThousand.test(wholeNumber)) { + wholeNumber = wholeNumber.replace(regexCheckForThousand, `$1${thousandSeparator}$2`); } - return x1 + x2; + return wholeNumber + decimal; } export function Deferred () {