Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add scorecard utils #79

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./formatBytes.js";
export * from "./locationToString.js";
export * from "./taggedString.js";
export * from "./parseManifestAuthor.js";
export * from "./scorecard.js";
33 changes: 33 additions & 0 deletions src/scorecard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export function getScoreColor(score: number) {
if (score < 4) {
return "red";
}
if (score < 6.5) {
return "orange";
}
if (score < 8.5) {
return "blue";
}

return "green";
}

export function getVCSRepositoryPathAndPlatform(url: string | URL): [path: string, platform: string] | null {
if (!url) {
return null;
}

try {
const repo = new URL(url);

const repoPath = repo.pathname.slice(
1,
repo.pathname.includes(".git") ? -4 : repo.pathname.length
);

return [repoPath, repo.host];
}
catch {
return null;
}
}
18 changes: 9 additions & 9 deletions src/taggedString.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export function taggedString(chaines: TemplateStringsArray, ...cles: (number | string)[]) {
return function cur(...valeurs: (string | number | {[key: string]: any})[]) {
const lastVal = valeurs[valeurs.length - 1];
export function taggedString(strings: TemplateStringsArray, ...keys: (number | string)[]) {
return function cur(...values: (string | number | {[key: string]: any})[]) {
const lastVal = values[values.length - 1];
const dict = typeof lastVal === "object" && lastVal !== null ? lastVal : {};
const resultat = [chaines[0]];
cles.forEach((cle, index) => {
resultat.push(
typeof cle === "number" ? valeurs[cle] : dict[cle],
chaines[index + 1]
const result = [strings[0]];
keys.forEach((key, index) => {
result.push(
typeof key === "number" ? values[key] : dict[key],
strings[index + 1]
);
});

return resultat.join("");
return result.join("");
};
}
43 changes: 43 additions & 0 deletions test/scorecard.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Import Node.js Dependencies
import { describe, it } from "node:test";
import assert from "node:assert";

// Import Internal Dependencies
import * as utils from "../src/scorecard.js";

describe("getScoreColor", () => {
it("should return red", () => {
assert.strictEqual(utils.getScoreColor(0), "red");
assert.strictEqual(utils.getScoreColor(3), "red");
assert.strictEqual(utils.getScoreColor(3.9), "red");
});

it("should return orange", () => {
assert.strictEqual(utils.getScoreColor(4), "orange");
assert.strictEqual(utils.getScoreColor(6.4), "orange");
});

it("should return blue", () => {
assert.strictEqual(utils.getScoreColor(6.5), "blue");
assert.strictEqual(utils.getScoreColor(8.4), "blue");
});

it("should return green", () => {
assert.strictEqual(utils.getScoreColor(8.5), "green");
assert.strictEqual(utils.getScoreColor(10), "green");
});
});

describe("getVCSRepositoryPathAndPlatform", () => {
it("should return null", () => {
assert.strictEqual(utils.getVCSRepositoryPathAndPlatform(""), null);
assert.strictEqual(utils.getVCSRepositoryPathAndPlatform(null as any), null);
assert.strictEqual(utils.getVCSRepositoryPathAndPlatform(undefined as any), null);
});

it("should return path and platform", () => {
assert.deepEqual(utils.getVCSRepositoryPathAndPlatform("http://github.com/foo/bar"), ["foo/bar", "github.com"]);
assert.deepEqual(utils.getVCSRepositoryPathAndPlatform("https://github.com/foo/bar.git"), ["foo/bar", "github.com"]);
});
});

2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}
}