Skip to content

Commit c6a5e12

Browse files
committed
refactor(core-utils): fix lint and simplify getNameFromAuthor
1 parent 5cb878c commit c6a5e12

1 file changed

Lines changed: 7 additions & 24 deletions

File tree

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { PackagePerson } from '@electron-forge/shared-types';
22

3+
type ParsedAuthor = { name?: string; email?: string; url?: string };
4+
35
/*!
46
* Inlined from parse-author <https://github.com/jonschlinkert/parse-author>
57
* and author-regex <https://github.com/jonschlinkert/author-regex>
@@ -11,9 +13,7 @@ function authorRegex(): RegExp {
1113
return /^\s*([^<(]*?)\s*([<(]([^>)]*?)[>)])?\s*([<(]([^>)]*?)[>)])*\s*$/;
1214
}
1315

14-
export function parseAuthor(
15-
str: string,
16-
): { name?: string; email?: string; url?: string } {
16+
export function parseAuthor(str: string): ParsedAuthor {
1717
if (typeof str !== 'string') {
1818
throw new TypeError('expected author to be a string');
1919
}
@@ -23,7 +23,7 @@ export function parseAuthor(
2323
}
2424

2525
const match = ([] as unknown[]).concat.apply([], authorRegex().exec(str));
26-
const author: { name?: string; email?: string; url?: string } = {};
26+
const author: ParsedAuthor = {};
2727

2828
if (match[1]) {
2929
author.name = match[1] as string;
@@ -48,29 +48,12 @@ export function parseAuthor(
4848
/**
4949
* Extracts the name from a package.json author field.
5050
*
51-
* @param author - The author object to extract the name from.
51+
* @param author - The author field to extract the name from.
5252
* @returns The name of the author.
5353
*
5454
* @see https://docs.npmjs.com/cli/configuring-npm/package-json#people-fields-author-contributors
5555
*/
5656
export function getNameFromAuthor(author: PackagePerson): string {
57-
let publisher: PackagePerson = author || '';
58-
59-
if (typeof publisher === 'string') {
60-
publisher = parseAuthor(publisher);
61-
}
62-
63-
if (
64-
typeof publisher !== 'string' &&
65-
publisher &&
66-
typeof publisher.name === 'string'
67-
) {
68-
publisher = publisher.name;
69-
}
70-
71-
if (typeof publisher !== 'string') {
72-
publisher = '';
73-
}
74-
75-
return publisher;
57+
const parsed = typeof author === 'string' ? parseAuthor(author) : author;
58+
return parsed?.name ?? '';
7659
}

0 commit comments

Comments
 (0)