Skip to main content
Version: 2.2.0

Accessors

Once you have a Namefully, the easy way to get text back out is to read a property. They're all read-only and they're all cheap (computed lazily, cached).

For all the examples below, let:

import { Namefully } from 'namefully';
const name = new Namefully('Mr John Joe Smith PhD');

The basics

AccessorValueNotes
name.prefix'Mr'undefined if none
name.first'John'first name's primary token
name.middle'Joe'first middle name only
name.last'Smith'last name's primary token
name.suffix'PhD'undefined if none

prefix and suffix are optional and can be undefined. first, middle, and last are always strings — middle is undefined only when there is no middle name at all.

The renderings

AccessorValueDescription
name.full'Mr John Joe Smith PhD'the canonical full name
name.long'Mr John Joe Smith PhD'alias for full
name.birth'John Joe Smith'excludes prefix and suffix
name.short'John Smith'first + last only
name.public'John S'first + last initial
name.salutation'Mr Smith'prefix + last

These honour Config.orderedBy. If the instance is LAST_NAME-ordered, full reads 'Smith John Joe' and so on.

The slightly more useful ones

AccessorValueDescription
name.size5number of name parts (1–5)
name.length17character length of full
name.isMonofalsetrue if this is a mononym
name.hasMiddletruetrue if a middle name exists
name.configConfigthe active Config instance
name.partsIterable<Name>iterate the underlying Names

Methods that look like accessors

A few callable methods feel like properties — they return computed values without changing state:

name.fullName(); // 'Mr John Joe Smith PhD' (alias for .full)
name.fullName('lastName'); // 'Smith John Joe' (override order)
name.middleName(); // ['Joe']
name.initials(); // ['J', 'J', 'S']
name.initials({ only: 'firstName' }); // ['J']
name.initials({ asJson: true }); // { firstName: ['J'], middleName: ['J'], lastName: ['S'] }

For the more elaborate output forms — format(), zip(), flatten(), shorten() — see Formatting tokens and Derived forms.