Skip to content

Commit 109ea23

Browse files
committed
refactor: use @nodesecure/utils & enhance author management
1 parent 8238818 commit 109ea23

File tree

9 files changed

+20
-130
lines changed

9 files changed

+20
-130
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@
7676
"@nodesecure/flags": "^1.2.0",
7777
"@nodesecure/i18n": "^1.2.0",
7878
"@nodesecure/npm-registry-sdk": "^1.3.0",
79-
"@nodesecure/scanner": "^2.0.0",
79+
"@nodesecure/scanner": "^2.0.1",
80+
"@nodesecure/utils": "^1.0.0",
8081
"@nodesecure/vuln": "^1.4.0",
8182
"@polka/send-type": "^0.5.2",
8283
"@slimio/async-cli-spinner": "^0.5.2",

public/js/master.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -553,17 +553,7 @@ document.addEventListener("DOMContentLoaded", async() => {
553553
}
554554

555555
function handleAuthor(author) {
556-
if (typeof author === "undefined" || author === null) {
557-
return;
558-
}
559-
let user = { name: null };
560-
561-
if (typeof author === "string") {
562-
user = utils.parseAuthor(author);
563-
}
564-
else if (typeof author === "object" && typeof author.name === "string") {
565-
user = author;
566-
}
556+
const user = "name" in author ? author : { name: null };
567557

568558
if (authorsList.has(user.name)) {
569559
authorsList.get(user.name).count++;

public/js/popup.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
import * as utils from "./utils.js";
1+
// Import Third-party Dependencies
2+
import { locationToString } from "@nodesecure/utils";
23
import List from "list.js";
34

4-
const loadingMessage = "Loading ...";
5+
// Import Internal Dependencies
6+
import * as utils from "./utils.js";
7+
8+
// CONSTANTS
9+
const kLoadingMessage = "Loading ...";
510

611
function licenseModal(clone, options) {
712
const { licenses, selectedNode } = options;
@@ -21,13 +26,6 @@ function licenseModal(clone, options) {
2126
}
2227
}
2328

24-
function locationToString(location) {
25-
const start = `${location[0][0]}:${location[0][1]}`;
26-
const end = `${location[1][0]}:${location[1][1]}`;
27-
28-
return `[${start}] - [${end}]`;
29-
}
30-
3129
function getLineFromFile(code, location) {
3230
const [[startLine]] = location;
3331
const lines = code.split('\n');
@@ -45,7 +43,7 @@ async function fetchCodeLine(event, url, location, cache, lineId) {
4543
return;
4644
}
4745

48-
target.innerText = loadingMessage;
46+
target.innerText = kLoadingMessage;
4947
const code = await fetch(url).then((response) => response.text());
5048

5149
target.innerText = code.length ? getLineFromFile(code, location): "Line not found ...";
@@ -55,12 +53,12 @@ async function fetchCodeLine(event, url, location, cache, lineId) {
5553

5654
function handleOutsideTooltipClick({ target }) {
5755
const tooltip = document.getElementById('tooltip');
58-
56+
5957
if (!tooltip) {
6058
return;
6159
}
62-
63-
if ((tooltip.innerHTML && tooltip.innerHTML !== loadingMessage) && !tooltip.contains(target) && tooltip.style.visibility === "visible") {
60+
61+
if ((tooltip.innerHTML && tooltip.innerHTML !== kLoadingMessage) && !tooltip.contains(target) && tooltip.style.visibility === "visible") {
6462
tooltip.style.visibility = "hidden";
6563
tooltip.innerHTML = "";
6664
}

public/js/utils.js

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -198,39 +198,6 @@ export async function getJSON(path, customHeaders = Object.create(null)) {
198198
return raw.json();
199199
}
200200

201-
export function parseAuthor(str) {
202-
if (typeof str !== "string") {
203-
throw new TypeError("expected author to be a string");
204-
}
205-
206-
if (!str || !/\w/.test(str)) {
207-
return {};
208-
}
209-
210-
const match = /^([^<(]+?)?[ \t]*(?:<([^>(]+?)>)?[ \t]*(?:\(([^)]+?)\)|$)/gm.exec(str);
211-
if (!match) {
212-
return {};
213-
}
214-
const author = Object.create(null);
215-
216-
if (match[1]) {
217-
author.name = match[1];
218-
}
219-
220-
for (let id = 2; id < match.length; id++) {
221-
const val = match[id] || "";
222-
223-
if (val.includes("@")) {
224-
author.email = val;
225-
}
226-
else if (val.includes("http")) {
227-
author.url = val;
228-
}
229-
}
230-
231-
return author;
232-
}
233-
234201
export function copyToClipboard(str) {
235202
const el = document.createElement('textarea'); // Create a <textarea> element
236203
el.value = str; // Set its value to the string that you want copied

src/commands/summary.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import path from "path";
66
import cliui from "cliui";
77
import kleur from "kleur";
88
import * as i18n from "@nodesecure/i18n";
9-
10-
// Import Internal Dependencies
11-
import { formatBytes } from "../utils.js";
9+
import { formatBytes } from "@nodesecure/utils";
1210

1311
// VARS
1412
const { yellow, gray, white, green, cyan, red } = kleur;

src/commands/verify.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// Import Third-party Dependencies
2-
import { verify } from "@nodesecure/scanner";
32
import cliui from "cliui";
43
import kleur from "kleur";
5-
6-
// Import Internal Dependencies
7-
import { formatBytes, locationToString } from "../utils.js";
4+
import { verify } from "@nodesecure/scanner";
5+
import { formatBytes, locationToString } from "@nodesecure/utils";
86

97
// VARS
108
const { yellow, grey, white, green, cyan, red, magenta } = kleur;

src/utils.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

test/utils.spec.js

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)