From 7c82498dfb211f5a019a99807221b9acedc6cda2 Mon Sep 17 00:00:00 2001 From: Oleg Valter Date: Mon, 9 Aug 2021 11:48:34 +0300 Subject: [PATCH] moved author utils to dedicated module --- src/generators/tampermonkey/index.ts | 3 ++- src/utils.ts | 27 --------------------------- src/utils/author.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 28 deletions(-) create mode 100644 src/utils/author.ts diff --git a/src/generators/tampermonkey/index.ts b/src/generators/tampermonkey/index.ts index aa4b6d6..18db491 100644 --- a/src/generators/tampermonkey/index.ts +++ b/src/generators/tampermonkey/index.ts @@ -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"; diff --git a/src/utils.ts b/src/utils.ts index 961b7d7..43f7948 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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) => - name + (email ? ` <${email}>` : "") + (url ? ` (${url})` : ""); - -export const parseAuthor = ( - info: PackageInfo["author"] -): Exclude => { - 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 }; diff --git a/src/utils/author.ts b/src/utils/author.ts new file mode 100644 index 0000000..e7bb582 --- /dev/null +++ b/src/utils/author.ts @@ -0,0 +1,28 @@ +import { PackageInfo } from "../utils"; + +export const formatAuthor = ({ + name, + email, + url, +}: Exclude) => + name + (email ? ` <${email}>` : "") + (url ? ` (${url})` : ""); + +export const parseAuthor = ( + info: PackageInfo["author"] +): Exclude => { + 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, + }; +};