From cead5f417bb61e7da360dff6e83cbcaf79e5224b Mon Sep 17 00:00:00 2001 From: seunggabi Date: Sun, 3 Nov 2019 09:08:56 +0900 Subject: [PATCH] refact(-): syntax --- extension/src/json-viewer/check-if-json.js | 11 ++++---- .../src/json-viewer/content-extractor.js | 26 +++++++++---------- extension/src/json-viewer/highlighter.js | 6 ++--- extension/src/json-viewer/jsl-format.js | 5 ++-- .../json-viewer/options/bind-reset-button.js | 1 - .../json-viewer/options/bind-save-button.js | 1 - extension/src/json-viewer/options/defaults.js | 2 +- .../json-viewer/options/render-theme-list.js | 6 ++--- extension/src/json-viewer/storage.js | 6 ++--- extension/src/json-viewer/theme-darkness.js | 2 +- .../src/json-viewer/viewer/render-alert.js | 2 +- .../src/json-viewer/viewer/render-extras.js | 8 +++--- 12 files changed, 37 insertions(+), 39 deletions(-) diff --git a/extension/src/json-viewer/check-if-json.js b/extension/src/json-viewer/check-if-json.js index 5160fde7..2ddd7aa6 100644 --- a/extension/src/json-viewer/check-if-json.js +++ b/extension/src/json-viewer/check-if-json.js @@ -23,8 +23,8 @@ function getPreWithSource() { } var childNode = childNodes[0]; - var nodeName = childNode.nodeName - var textContent = childNode.textContent + var nodeName = childNode.nodeName; + var textContent = childNode.textContent; if (nodeName === "PRE") { return childNode; @@ -61,9 +61,10 @@ function isJSON(jsonStr) { return false } - str = str.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@') - str = str.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') - str = str.replace(/(?:^|:|,)(?:\s*\[)+/g, '') + str = str + .replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@') + .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') + .replace(/(?:^|:|,)(?:\s*\[)+/g, ''); return (/^[\],:{}\s]*$/).test(str) } diff --git a/extension/src/json-viewer/content-extractor.js b/extension/src/json-viewer/content-extractor.js index 59a6f8f1..063192dd 100644 --- a/extension/src/json-viewer/content-extractor.js +++ b/extension/src/json-viewer/content-extractor.js @@ -6,7 +6,7 @@ var TOKEN = (Math.random() + 1).toString(36).slice(2, 7); var WRAP_START = ""; var WRAP_END = ""; var NUM_REGEX = /^-?\d+\.?\d*([eE]\+)?\d*$/g; -var ESCAPED_REGEX = "(-?\\d+\\.?\\d*([eE]\\+)?\\d*)" +var ESCAPED_REGEX = "(-?\\d+\\.?\\d*([eE]\\+)?\\d*)"; var WRAP_REGEX = new RegExp( "^" + WRAP_START + ESCAPED_REGEX + WRAP_END + "$", "g" @@ -80,7 +80,7 @@ function wrapNumbers(text) { for (var i = 0, len = text.length; i < len; i++) { var char = text[i]; - if (char == '"' && !charIsEscaped) { + if (char === '"' && !charIsEscaped) { isInString = !isInString; } @@ -102,11 +102,10 @@ function wrapNumbers(text) { } // this applies to the _next_ character - the one used in the next iteration - charIsEscaped = (char == '\\') ? !charIsEscaped : false + charIsEscaped = (char === '\\') ? !charIsEscaped : false; if (isInNumber) { numberBuffer += char; - } else { buffer += char; beforePrevious = previous; @@ -119,19 +118,18 @@ function wrapNumbers(text) { function isCharInNumber(char, previous) { return ('0' <= char && char <= '9') || - ('0' <= previous && previous <= '9' && (char == 'e' || char == 'E')) || - (('e' == previous || 'E' == previous) && char == '+') || - char == '.' || - char == '-'; + ('0' <= previous && previous <= '9' && char.toUpperCase() === 'E') || + (char.toUpperCase() === 'E' && char === '+') || + char === '.' || + char === '-'; } -function isCharInString(char, previous) { +function isCharInString(char) { return ('0' > char || char > '9') && - char != 'e' && - char != 'E' && - char != '+' && - char != '.' && - char != '-'; + char.toUpperCase() !== 'E' && + char !== '+' && + char !== '.' && + char !== '-'; } module.exports = contentExtractor; diff --git a/extension/src/json-viewer/highlighter.js b/extension/src/json-viewer/highlighter.js index 77f60b53..7a6cec0c 100644 --- a/extension/src/json-viewer/highlighter.js +++ b/extension/src/json-viewer/highlighter.js @@ -92,7 +92,7 @@ Highlighter.prototype = { if (self.wrapLinkWithAnchorTag()) { var linkTag = document.createElement("a"); linkTag.href = decodedText; - linkTag.setAttribute('target', '_blank') + linkTag.setAttribute('target', '_blank'); linkTag.classList.add("cm-string"); // reparent the child nodes to preserve the cursor when editing @@ -121,7 +121,7 @@ Highlighter.prototype = { this.editor.on("mousedown", function(cm, event) { var element = event.target; if (element.classList.contains("cm-string-link")) { - var url = element.getAttribute("data-url") + var url = element.getAttribute("data-url"); var target = "_self"; if (self.openLinksInNewWindow()) { target = "_blank"; @@ -230,6 +230,6 @@ Highlighter.prototype = { isReadOny: function() { return this.options.structure.readOnly; } -} +}; module.exports = Highlighter; diff --git a/extension/src/json-viewer/jsl-format.js b/extension/src/json-viewer/jsl-format.js index ac38ab56..033b078c 100644 --- a/extension/src/json-viewer/jsl-format.js +++ b/extension/src/json-viewer/jsl-format.js @@ -16,7 +16,7 @@ jsl.format = (function () { var numOpened = 1; try{ while (numOpened > 0 && currentPosition < jsonString.length) { - var currentChar = jsonString.charAt(currentPosition) + var currentChar = jsonString.charAt(currentPosition); switch (currentChar) { case '[': if(!inString){ @@ -114,7 +114,8 @@ jsl.format = (function () { if (i === 0) { inString = true; } - else if (json.charAt(i - 1) !== '\\' || (json.charAt(i - 1) == '\\' && json.charAt(i - 2) == '\\')) { + else if (json.charAt(i - 1) !== '\\' || + (json.charAt(i - 1) === '\\' && json.charAt(i - 2) === '\\')) { inString = !inString; } newJson += currentChar; diff --git a/extension/src/json-viewer/options/bind-reset-button.js b/extension/src/json-viewer/options/bind-reset-button.js index fc353eb2..d94ce0bf 100644 --- a/extension/src/json-viewer/options/bind-reset-button.js +++ b/extension/src/json-viewer/options/bind-reset-button.js @@ -25,7 +25,6 @@ function bindResetButton() { Storage.save(options); document.location.reload(); - }); } } diff --git a/extension/src/json-viewer/options/bind-save-button.js b/extension/src/json-viewer/options/bind-save-button.js index 55631349..82c56205 100644 --- a/extension/src/json-viewer/options/bind-save-button.js +++ b/extension/src/json-viewer/options/bind-save-button.js @@ -19,7 +19,6 @@ function bindSaveButton(editors, onSaveClicked) { } onSaveClicked(output); - } } diff --git a/extension/src/json-viewer/options/defaults.js b/extension/src/json-viewer/options/defaults.js index 88c3e291..82afaf38 100644 --- a/extension/src/json-viewer/options/defaults.js +++ b/extension/src/json-viewer/options/defaults.js @@ -27,4 +27,4 @@ module.exports = { " line-height: 1.5em;", "}" ].join('\n') -} +}; diff --git a/extension/src/json-viewer/options/render-theme-list.js b/extension/src/json-viewer/options/render-theme-list.js index 9ef97067..ad4207b7 100644 --- a/extension/src/json-viewer/options/render-theme-list.js +++ b/extension/src/json-viewer/options/render-theme-list.js @@ -15,7 +15,7 @@ var themeJSONExample = { "and fake keys" ] } -} +}; function onThemeChange(input, editor) { var selectedTheme = input.options[input.selectedIndex].value; @@ -60,7 +60,7 @@ function renderThemeList(CodeMirror, value) { themes.onchange = function() { onThemeChange(themesInput, themeEditor); - } + }; var optionSelected = value; themesInput.appendChild(createOption(themeDefault, optionSelected)); @@ -74,7 +74,7 @@ function renderThemeList(CodeMirror, value) { function createOption(theme, optionSelected) { var option = document.createElement("option"); - option.value = theme + option.value = theme; option.text = theme; if (theme === optionSelected) { diff --git a/extension/src/json-viewer/storage.js b/extension/src/json-viewer/storage.js index 6f3382f6..a07775c4 100644 --- a/extension/src/json-viewer/storage.js +++ b/extension/src/json-viewer/storage.js @@ -16,7 +16,7 @@ module.exports = { options = optionsStr ? JSON.parse(optionsStr) : {}; options.theme = options.theme || defaults.theme; options.addons = options.addons ? JSON.parse(options.addons) : {}; - options.addons = merge({}, defaults.addons, options.addons) + options.addons = merge({}, defaults.addons, options.addons); options.structure = options.structure ? JSON.parse(options.structure) : defaults.structure; options.style = options.style && options.style.length > 0 ? options.style : defaults.style; return options; @@ -36,7 +36,7 @@ module.exports = { options.addons = { prependHeader: JSON.parse(oldOptions.prependHeader || defaults.addons.prependHeader), maxJsonSize: parseInt(oldOptions.maxJsonSize || defaults.addons.maxJsonSize, 10) - } + }; // Update to at least the new max value if (options.addons.maxJsonSize < defaults.addons.maxJsonSize) { @@ -60,4 +60,4 @@ module.exports = { return optionsStr; } -} +}; diff --git a/extension/src/json-viewer/theme-darkness.js b/extension/src/json-viewer/theme-darkness.js index 974deb4b..4a03090b 100644 --- a/extension/src/json-viewer/theme-darkness.js +++ b/extension/src/json-viewer/theme-darkness.js @@ -4,4 +4,4 @@ module.exports = function(name) { if (themes.dark.indexOf(name) !== -1) darkness = "dark"; return darkness; -} +}; diff --git a/extension/src/json-viewer/viewer/render-alert.js b/extension/src/json-viewer/viewer/render-alert.js index 4009e622..99e53fc3 100644 --- a/extension/src/json-viewer/viewer/render-alert.js +++ b/extension/src/json-viewer/viewer/render-alert.js @@ -13,7 +13,7 @@ function renderAlert(pre, options, content) { closeBtn.onclick = function(e) { e.preventDefault(); alertContainer.parentNode.removeChild(alertContainer); - } + }; alertContainer.appendChild(closeBtn); diff --git a/extension/src/json-viewer/viewer/render-extras.js b/extension/src/json-viewer/viewer/render-extras.js index 14c173fb..d2663438 100644 --- a/extension/src/json-viewer/viewer/render-extras.js +++ b/extension/src/json-viewer/viewer/render-extras.js @@ -39,7 +39,7 @@ function renderExtras(pre, options, highlighter) { pre.hidden = true; extras.className = extras.className.replace(/\s+auto-highlight-off/, ''); } - } + }; var unfoldLink = document.createElement("a"); unfoldLink.className = "json_viewer icon unfold"; @@ -48,7 +48,7 @@ function renderExtras(pre, options, highlighter) { unfoldLink.innerHTML = svgUnfold; unfoldLink.onclick = function(e) { e.preventDefault(); - var value = pre.getAttribute('data-folded') + var value = pre.getAttribute('data-folded'); if (value === 'true' || value === true) { highlighter.unfoldAll(); @@ -58,14 +58,14 @@ function renderExtras(pre, options, highlighter) { highlighter.fold(); pre.setAttribute('data-folded', true) } - } + }; extras.appendChild(optionsLink); extras.appendChild(rawLink); // "awaysFold" was a typo but to avoid any problems I'll keep it // a while - pre.setAttribute('data-folded', options.addons.alwaysFold || options.addons.awaysFold) + pre.setAttribute('data-folded', options.addons.alwaysFold || options.addons.awaysFold); extras.appendChild(unfoldLink); document.body.appendChild(extras);