Skip to main content
Version: 1.3.1

From a string

The constructor's friendliest overload. Pass in a string, get a Namefully.

import { Namefully } from 'namefully';

new Namefully('John Smith');
new Namefully('Mr John Joe Smith PhD');

namefully splits on whitespace by default and assigns the parts to slots in left-to-right order, biased by orderedBy. With the default config:

Inputfirstmiddlelast
'John Smith'JohnSmith
'John Joe Smith'JohnJoeSmith
'John Joe Allen Smith'[1]JoeAllenSmith

[1] When more than 3 tokens are present, additional ones considered to be prefixes or suffixes.

Prefixes and suffixes

Given the number of tokens, the library will determine which ones are prefixes and suffixes.

const name = new Namefully('Mr John Joe Smith PhD');
name.prefix; // 'Mr' (or 'Mr.' with Title.US)
name.suffix; // 'PhD'
name.birth; // 'John Joe Smith'

Switching the order

import { Namefully, NameOrder } from 'namefully';

new Namefully('Smith John Joe', {
orderedBy: NameOrder.LAST_NAME,
});

See Name order.

Non-space separators

Comma-separated inputs are common in CSVs and "Last, First" UI conventions:

import { Namefully, Separator } from 'namefully';

const name = new Namefully('Smith,John,Joe', {
separator: Separator.COMMA,
orderedBy: NameOrder.LAST_NAME,
});

name.first; // 'John'
name.last; // 'Smith'

Note separator and orderedBy are independent — the separator says how to chop the string up, the order says which end is which.

When string parsing isn't enough

If your input has a nickname in quotes, mixed separators, or your slots don't appear in a predictable order, switch to: