Skip to content

Commit

Permalink
Fixup: Fix format and lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
yvonnesjy committed Jun 17, 2024
1 parent 3550a2e commit f6f8158
Show file tree
Hide file tree
Showing 2 changed files with 394 additions and 453 deletions.
99 changes: 47 additions & 52 deletions src/js/common/Utilities.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define(["jquery", "underscore"], function ($, _) {
define([], () => {
"use strict";

/**
Expand All @@ -8,65 +8,58 @@ define(["jquery", "underscore"], function ($, _) {
* @type {object}
* @since 2.14.0
*/
var Utilities = /** @lends Utilities.prototype */ {
const Utilities = /** @lends Utilities.prototype */ {
/**
* HTML-encodes the given string so it can be inserted into an HTML page without running
* any embedded Javascript.
* @param {string} s
* @returns {string}
* @param {string} s String to be encoded.
* @returns {string} HTML encoded string.
*/
encodeHTML: function (s) {
try {
if (!s || typeof s !== "string") {
return "";
}

return s
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/'/g, "&apos;")
.replace(/\//g, "/")
.replace(/"/g, "&quot;");
} catch (e) {
console.error("Could not encode HTML: ", e);
encodeHTML(s) {
if (!s || typeof s !== "string") {
return "";
}

return s
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/'/g, "&apos;")
.replace(/\//g, "/")
.replace(/"/g, "&quot;");
},

/**
* Validates that the given string is a valid DOI
* @param {string} identifier
* @returns {boolean}
* @param {string} identifier String to be validated.
* @returns {boolean} True if identifier is a valid DOI.
* @since 2.15.0
*/
isValidDOI: function (identifier) {
isValidDOI(identifier) {
// generate doi regex
var doiRGEX = new RegExp(
/^\s*(http:\/\/|https:\/\/)?(doi.org\/|dx.doi.org\/)?(doi: ?|DOI: ?)?(10\.\d{4,}(\.\d)*)\/(\w+).*$/gi,
);
const doiRGEX =
/^\s*(http:\/\/|https:\/\/)?(doi.org\/|dx.doi.org\/)?(doi: ?|DOI: ?)?(10\.\d{4,}(\.\d)*)\/(\w+).*$/gi;

return doiRGEX.test(identifier);
},

/**
* Read the first part of a file
*
* @param {File} file - A reference to a file
* @param {Backbone.View} context - The View to bind `callback` to
* @param {function} callback - A function to run after the read is
* @param {Function} callback - A function to run after the read is
* complete. The function is bound to `context`.
* @param {number} bytes - The number of bytes to read from the start of the
* file
* @since 2.15.0
*/
readSlice: function (file, context, callback, bytes = 1024) {
readSlice(file, context, callback, bytes = 1024) {
if (typeof callback !== "function") {
return;
}

var reader = new FileReader(),
blob = file.slice(0, bytes);
const reader = new FileReader();
const blob = file.slice(0, bytes);

reader.onloadend = callback.bind(context);
reader.readAsBinaryString(blob);
Expand All @@ -77,19 +70,18 @@ define(["jquery", "underscore"], function ($, _) {
* Doesn't handle:
* - UTF BOM (garbles first col name)
* - Commas inside quoted headers
*
* @param {string} text - A chunk of a file
* @return {Array} A list of names
* @returns {Array} A list of names
* @since 2.15.0
*/
tryParseCSVHeader: function (text) {
tryParseCSVHeader(text) {
// The order is important here
var strategies = ["\r\\n", "\r", "\n"];
const strategies = ["\r\\n", "\r", "\n"];

var index = -1;
let index = -1;

for (var i = 1; i < strategies.length; i++) {
var result = text.indexOf(strategies[i]);
for (let i = 1; i < strategies.length; i += 1) {
const result = text.indexOf(strategies[i]);

if (result >= 0) {
index = result;
Expand All @@ -102,18 +94,14 @@ define(["jquery", "underscore"], function ($, _) {
return [];
}

var header_line = text.slice(0, index);
var names = header_line.split(",");
const headerLine = text.slice(0, index);
let names = headerLine.split(",");

// Remove surrounding parens and double-quotes
names = names.map(function (name) {
return name.replaceAll(/^["']|["']$/gm, "");
});
names = names.map((name) => name.replaceAll(/^["']|["']$/gm, ""));

// Filter out zero-length values (headers like a,b,c,,,,,)
names = names.filter(function (name) {
return name.length > 0;
});
names = names.filter((name) => name.length > 0);

return names;
},
Expand All @@ -126,7 +114,9 @@ define(["jquery", "underscore"], function ($, _) {

const roundingConstant = Utilities.getRoundingConstant(max - min);
if (roundingConstant) {
return (Math.round(value * roundingConstant) / roundingConstant).toString();
return (
Math.round(value * roundingConstant) / roundingConstant
).toString();
}
return value.toExponential(2).toString();
},
Expand All @@ -136,19 +126,24 @@ define(["jquery", "underscore"], function ($, _) {
getRoundingConstant(range) {
if (range < 0.0001 || range > 100000) {
return null; // Will use scientific notation
} else if (range < 0.001) {
}
if (range < 0.001) {
return 100000; // Allow 5 decimal places
} else if (range < 0.01) {
}
if (range < 0.01) {
return 10000; // Allow 4 decimal places
} else if (range < 0.1) {
}
if (range < 0.1) {
return 1000; // Allow 3 decimal places
} else if (range < 1) {
}
if (range < 1) {
return 100; // Allow 2 decimal places
} else if (range > 100) {
}
if (range > 100) {
return 1; // No decimal places
}
return 10; // Allow 1 decimal place by default
}
},
};

return Utilities;
Expand Down
Loading

0 comments on commit f6f8158

Please sign in to comment.