diff --git a/swh/loader/package/npm/loader.py b/swh/loader/package/npm/loader.py --- a/swh/loader/package/npm/loader.py +++ b/swh/loader/package/npm/loader.py @@ -181,6 +181,23 @@ return None +def _author_str(author_data): + """Parse author from package.json author fields + + """ + if type(author_data) is dict: + author_str = "" + if "name" in author_data: + author_str += author_data["name"] + if "email" in author_data: + author_str += " <%s>" % author_data["email"] + return author_str + elif type(author_data) is list: + return _author_str(author_data[0]) if len(author_data) > 0 else "" + else: + return author_data + + def extract_npm_package_author(package_json) -> Person: """ Extract package author from a ``package.json`` file content and @@ -194,20 +211,6 @@ Person """ - - def _author_str(author_data): - if type(author_data) is dict: - author_str = "" - if "name" in author_data: - author_str += author_data["name"] - if "email" in author_data: - author_str += " <%s>" % author_data["email"] - return author_str - elif type(author_data) is list: - return _author_str(author_data[0]) if len(author_data) > 0 else "" - else: - return author_data - for author_key in ("author", "authors"): if author_key in package_json: author_str = _author_str(package_json[author_key]) diff --git a/swh/loader/package/npm/tests/test_npm.py b/swh/loader/package/npm/tests/test_npm.py --- a/swh/loader/package/npm/tests/test_npm.py +++ b/swh/loader/package/npm/tests/test_npm.py @@ -11,6 +11,7 @@ from swh.model.model import Person, Snapshot, SnapshotBranch, TargetType from swh.loader.package.npm.loader import ( + _author_str, NpmLoader, extract_npm_package_author, artifact_to_revision_id, @@ -23,6 +24,20 @@ ) +def test_npm_author_str(): + for author, expected_author in [ + ("author", "author"), + ( + ["Al from quantum leap", "hal from 2001 space odyssey"], + "Al from quantum leap", + ), + ([], ""), + ({"name": "groot", "email": "groot@galaxy.org",}, "groot "), + ({}, ""), + ]: + assert _author_str(author) == expected_author + + def test_extract_npm_package_author(datadir): package_metadata_filepath = os.path.join( datadir, "https_replicate.npmjs.com", "org_visit1"