Skip to content

Commit

Permalink
refact(-): syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
seunggabi committed Nov 3, 2019
1 parent bad9a49 commit ee02665
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 38 deletions.
11 changes: 6 additions & 5 deletions extension/src/json-viewer/check-if-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
}

Expand Down
26 changes: 12 additions & 14 deletions extension/src/json-viewer/content-extractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var TOKEN = (Math.random() + 1).toString(36).slice(2, 7);
var WRAP_START = "<wrap_" + TOKEN + ">";
var WRAP_END = "</wrap_" + TOKEN +">";
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"
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -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;
6 changes: 3 additions & 3 deletions extension/src/json-viewer/highlighter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -230,6 +230,6 @@ Highlighter.prototype = {
isReadOny: function() {
return this.options.structure.readOnly;
}
}
};

module.exports = Highlighter;
5 changes: 3 additions & 2 deletions extension/src/json-viewer/jsl-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion extension/src/json-viewer/options/bind-reset-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ function bindResetButton() {

Storage.save(options);
document.location.reload();

});
}
}
Expand Down
1 change: 0 additions & 1 deletion extension/src/json-viewer/options/bind-save-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ function bindSaveButton(editors, onSaveClicked) {
}

onSaveClicked(output);

}
}

Expand Down
2 changes: 1 addition & 1 deletion extension/src/json-viewer/options/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ module.exports = {
" line-height: 1.5em;",
"}"
].join('\n')
}
};
6 changes: 3 additions & 3 deletions extension/src/json-viewer/options/render-theme-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var themeJSONExample = {
"and fake keys"
]
}
}
};

function onThemeChange(input, editor) {
var selectedTheme = input.options[input.selectedIndex].value;
Expand Down Expand Up @@ -60,7 +60,7 @@ function renderThemeList(CodeMirror, value) {

themes.onchange = function() {
onThemeChange(themesInput, themeEditor);
}
};

var optionSelected = value;
themesInput.appendChild(createOption(themeDefault, optionSelected));
Expand All @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions extension/src/json-viewer/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -60,4 +60,4 @@ module.exports = {

return optionsStr;
}
}
};
2 changes: 1 addition & 1 deletion extension/src/json-viewer/theme-darkness.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ module.exports = function(name) {
if (themes.dark.indexOf(name) !== -1) darkness = "dark";

return darkness;
}
};
2 changes: 1 addition & 1 deletion extension/src/json-viewer/viewer/render-alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function renderAlert(pre, options, content) {
closeBtn.onclick = function(e) {
e.preventDefault();
alertContainer.parentNode.removeChild(alertContainer);
}
};

alertContainer.appendChild(closeBtn);

Expand Down
6 changes: 3 additions & 3 deletions extension/src/json-viewer/viewer/render-extras.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
Expand Down

0 comments on commit ee02665

Please sign in to comment.