Skip to content

hey-jj/author-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

author-parser

Parse an npm "person" string into its parts.

npm lets the author, contributors, and maintainers fields of a package.json use a shorthand string of the form Name <email> (url). This crate parses that shorthand into an Author with optional name, email, and url. Every part is optional. The name, when present, is the leading text before the first bracket. The email and url tokens may appear in either order.

Installation

[dependencies]
author-parser = "0.1"

Usage

use author_parser::parse;

let a = parse("Jon Schlinkert <jon@example.com> (https://example.com)");
assert_eq!(a.name.as_deref(), Some("Jon Schlinkert"));
assert_eq!(a.email.as_deref(), Some("jon@example.com"));
assert_eq!(a.url.as_deref(), Some("https://example.com"));

A field is Some only when the matching part is present and non-empty. An input with no usable parts returns the default, where every field is None.

use author_parser::{parse, Author};

assert_eq!(parse(""), Author::default());
assert_eq!(parse("   "), Author::default());

Supported formats

Any subset of the parts works. The name, when present, leads; email and url may appear in either order:

Name
Name <email> (url)
Name <email>(url)
Name<email> (url)
Name<email>(url)
Name (url) <email>
Name (url)<email>
Name(url) <email>
Name(url)<email>
Name (url)
Name(url)
Name <email>
Name<email>
<email> (url)
<email>(url)
(url) <email>
(url)<email>
<email>
(url)

Behavior notes

  • A bracket token is <...> for email or (...) for url. The leading bracket decides the field, so mismatched pairs like <x) still read as email.
  • Empty bracket tokens (<> or ()) are dropped.
  • The grammar holds one optional first bracket token plus a repeated group that consumes adjacent tokens with no whitespace between them. Only the first bracket token and the final token in the adjacent trailing run are read. Middle tokens in a mixed run can be dropped, so <a>(u)<b> reads as email = "b" and url = None. Same-kind adjacent tokens still keep the final value, so (u1)(u2)(u3) reads as url = "u3". A token set off by whitespace past the single boundary the grammar allows breaks the match, so (u1) (u2) (u3) returns the default.
  • When two tokens set the same field, the second wins.
  • The presence gate uses an ASCII word character check. A string with no ASCII word character returns the default, even when it holds non-ASCII letters. A name that carries an ASCII word character keeps its full text.

License

Licensed under the MIT license.

About

Parse an npm person string (Name <email> (url)) into name, email, and url parts

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors

Languages