Skip to content
This repository has been archived by the owner on Aug 14, 2024. It is now read-only.

Commit

Permalink
refacto: some improvements (#20)
Browse files Browse the repository at this point in the history
* refacto: some improvements

* fix: review
  • Loading branch information
Kawacrepe authored Jan 18, 2023
1 parent 2336619 commit b8b5485
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 36 deletions.
21 changes: 15 additions & 6 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
// Import Third-party Dependencies
import { Scanner } from "@nodesecure/scanner";

export function extractAllAuthorsFromLibrary(library: Scanner.Payload, opts: options): Promise<authorResponse[]>
export function extractAllAuthors(library: Scanner.Payload, opts: options): Promise<extractionResult>

export interface options {
flags: flagAuthor[],
flags: extractedAuthor[],
domainInformations: boolean,
}

export interface authorResponse {
export interface extractionResult {
authors: author[],
flaggedAuthors: extractedAuthor[],
}

export interface author {
name?: string;
email?: string;
url?: string;
flagged: boolean,
packages: unknown[],
packages: {
homepage: string,
spec: string,
version: string,
at?: string,
}[],
domain?: {
expirationDate?: string,
mxRecords?: unknown[],
}
}
export interface flagAuthor {
export interface extractedAuthor {
name: string,
email: string,
}
21 changes: 12 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function splitAuthorNameEmail(author) {
};
}

export async function extractAllAuthorsFromLibrary(library, opts = { flags: [], domainInformations: false }) {
export async function extractAllAuthors(library, opts = { flags: [], domainInformations: false }) {
if (!("dependencies" in library)) {
return [];
}
Expand Down Expand Up @@ -49,10 +49,9 @@ export async function extractAllAuthorsFromLibrary(library, opts = { flags: [],
authors.push({
name: author.name,
email: author.email,
flagged: false,
packages: [{
...packageMeta,
isPublishers: Boolean(author.at)
...(author?.at ? { at: author.at } : {})
}]
});
}
Expand All @@ -61,13 +60,16 @@ export async function extractAllAuthorsFromLibrary(library, opts = { flags: [],
return [];
}

const authorsWithFlags = addFlagsInResponse(useLevenshtein(authors), opts.flags);
const authorsFlagged = findFlaggedAuthors(useLevenshtein(authors), opts.flags);

if (opts.domainInformations === true) {
return addDomainInformations(authorsWithFlags);
return addDomainInformations(authors);
}

return authorsWithFlags;
return {
authorsFlagged,
authors
};
}

async function addDomainInformations(authors) {
Expand Down Expand Up @@ -99,16 +101,17 @@ async function addDomainInformations(authors) {
}


function addFlagsInResponse(authors, flags) {
function findFlaggedAuthors(authors, flags) {
const res = [];
for (const author of authors) {
for (const flag of flags) {
if (flag.name === author.name || flag.email === author.email) {
author.flagged = true;
res.push({ name: author.name, email: author.email });
}
}
}

return authors;
return res;
}


Expand Down
42 changes: 21 additions & 21 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { readFile } from "fs/promises";
import test from "tape";

// Import Internal Dependencies
import { extractAllAuthorsFromLibrary } from "../src/index.js";
import { extractAllAuthors } from "../src/index.js";

const nsecureTestFile = JSON.parse(
await readFile(
Expand All @@ -17,38 +17,38 @@ const nsecureTestFile = JSON.parse(
test("All authors from library without flags involved", async(tape) => {
const packageTest = nsecureTestFile;

const authors = await extractAllAuthorsFromLibrary(packageTest, { flags: [], domainInformations: true });
const res = await extractAllAuthors(packageTest, { flags: [], domainInformations: true });

tape.isNot(authors.length, 0, "There should be authors in the response");
tape.isNot(res.authors.length, 0, "There should be authors in the response");
tape.end();
});

test("test authors from library with flag", async(tape) => {
const packageTest = nsecureTestFile;
const flaggedAuthors = [
{ name: "Blakeembrey", email: "hello@blakeembrey.com" }
{ name: "kesla", email: "david.bjorklund@gmail.com" }
];
const authors = await extractAllAuthorsFromLibrary(packageTest, {
const res = await extractAllAuthors(packageTest, {
flags: flaggedAuthors
});
tape.deepEqual(authors.slice(4, 5), [{
name: "Blake Embrey",
email: "[email protected]",
flagged: true,
packages: [
{
homepage: "https://github.com/blakeembrey/array-flatten",
spec: "array-flatten",
versions: "3.0.0",
isPublishers: false
},

tape.deepEqual(res.authorsFlagged, flaggedAuthors);
tape.deepEqual(res.authors.slice(1, 2),
[
{
homepage: "https://github.com/pillarjs/path-to-regexp#readme",
spec: "path-to-regexp",
versions: "6.2.1",
isPublishers: true
name: "kesla",
email: "[email protected]",
packages:
[
{
homepage: "https://github.com/jshttp/etag#readme",
spec: "etag",
versions: "1.8.1",
at: "2014-05-18T11:14:58.281Z"
}
]
}
]
}]);
);
tape.end();
});

0 comments on commit b8b5485

Please sign in to comment.