Skip to content

Commit

Permalink
moved author utils to dedicated module
Browse files Browse the repository at this point in the history
  • Loading branch information
Oaphi committed Aug 9, 2021
1 parent 513c68d commit 7c82498
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
3 changes: 2 additions & 1 deletion src/generators/tampermonkey/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
HeaderEntries,
HeaderGenerator,
} from "..";
import { formatAuthor, parseAuthor, parseName } from "../../utils";
import { parseName } from "../../utils";
import { formatAuthor, parseAuthor } from "../../utils/author";
import { finalizeMonkeyHeaders } from "../common/monkey";
import { TampermonkeyGrants, TampermonkeyHeaders } from "./types";

Expand Down
27 changes: 0 additions & 27 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,6 @@ export const scase = (text: string) =>

export const mdLink = (lbl: string, href: string) => `[${lbl}](${href})`;

export const formatAuthor = ({
name,
email,
url,
}: Exclude<PackageInfo["author"], string>) =>
name + (email ? ` <${email}>` : "") + (url ? ` (${url})` : "");

export const parseAuthor = (
info: PackageInfo["author"]
): Exclude<PackageInfo["author"], string> => {
if (typeof info === "object") return info;

const authorRegex = /(\w+(?:\s\w+)?)(?:\s<(.+?)>)?(?:\s\((.+?)\))?$/i;

const match = authorRegex.exec(info);

if (!match) throw new Error(`unable to parse author field: ${info}`);

const [_full, name, email, url] = match;

return {
name,
email,
url,
};
};

export const parseName = (name: string) => {
const [, scope, packageName] = name.match(/(?:@([\w-]+)\/)?([\w-]+)/) || [];
return { scope, packageName };
Expand Down
28 changes: 28 additions & 0 deletions src/utils/author.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { PackageInfo } from "../utils";

export const formatAuthor = ({
name,
email,
url,
}: Exclude<PackageInfo["author"], string>) =>
name + (email ? ` <${email}>` : "") + (url ? ` (${url})` : "");

export const parseAuthor = (
info: PackageInfo["author"]
): Exclude<PackageInfo["author"], string> => {
if (typeof info === "object") return info;

const authorRegex = /(\w+(?:\s\w+)?)(?:\s<(.+?)>)?(?:\s\((.+?)\))?$/i;

const match = authorRegex.exec(info);

if (!match) throw new Error(`unable to parse author field: ${info}`);

const [_full, name, email, url] = match;

return {
name,
email,
url,
};
};

0 comments on commit 7c82498

Please # to comment.