Version: 1.2.0

Resources

namefully provides some ready-made recipes with extra functionality. They work independently. That is, you don't need an instance of Namefully to work with a specific name part like a first name, for example.

They are 3 basic classes:

  1. Name
  2. FirstName
  3. LastName

Name

This class represents a piece of name in a string format with some extra capabilities, compared to a simple string utils provided by JavaScript. This class helps to define the role of a name part (e.g., prefix) before anything.

constructor
/**
* Creates augmented names by adding extra functionality to a string name.
* @param type must be indicated to categorize the name so it can be
* treated accordingly.
* @param capsRange determines how the name should be capitalized initially.
*/
constructor(value: string, readonly type: Namon, capsRange?: CapsRange)

Example:

import { Name, Namon, CapsRange } from 'namefully'
const prefix = new Name('Dr', Namon.PREFIX)
const firstname = new Name('Nick', Namon.FIRST_NAME)
const middlename = new Name('Ariana', Namon.MIDDLE_NAME)
const lastname = new Name('Clinton', Namon.LAST_NAME)
const suffix = new Name('MSc', Namon.SUFFIX)
const name = new Name('John', Namon.FIRST_NAME, CapsRange.ALL)
console.log(name.value) // => JOHN

Alternatively:

import { Name } from 'namefully'
const prefix = Name.prefix('Dr')
const firstName = Name.first('Nick')
const middleName = Name.middle('Ariana')
const lastName = Name.last('Clinton')
const suffix = Name.suffix('MSc')

As you can see, the 5 types of name can be created from that class. Additionally, you can specify which kind of capitalizations to use when manipulating that piece of name.

API:

PropertiesTypeModifierDescription
valuestringread/writeThe piece of string treated as a name.
lengthnumberread-onlyThe length of the name.
isPrefixbooleanread-onlyWhether the name is a prefix.
isFirstNamebooleanread-onlyWhether the name is a first name.
isMiddleNamebooleanread-onlyWhether the name is a middle name.
isLastNamebooleanread-onlyWhether the name is a last name.
isSuffixbooleanread-onlyWhether the name is a suffix.
MethodsReturnsDescription
toString()stringReturns a string representation of the namon.
initials()string[]Gets the initials (first character) of this name.
equal(Name)booleanReturns true if the other is equal to this name.
caps(CapsRange)NameCapitalizes a name.
decaps(CapsRange)NameDe-capitalizes a name.

FirstName

This class is an extension of Name and represents a given name. It can work with more than one name part.

constructor
/**
* Creates an extended version of `Name` and flags it as a first name `type`.
*
* Some may consider `more` additional name parts of a given name as their
* first names, but not as their middle names. Though, it may mean the same,
* `more` provides the freedom to do it as it pleases.
*/
constructor(value: string, ...more: string[])

Example:

import { FirstName } from 'namefully'
const firstName = new FirstName('Emilia', 'Isobel', 'Euphemia', 'Rose')
firstName.toString() // => Emilia
firstName.tostring(true) // => Emilia Isobel Euphemia Rose

An instance of this class handles separately the required name piece along with the optional other given names a person might have.

API:

PropertiesTypeModifierDescription
hasMorebooleanread-onlyDetermines whether a first name has more name parts.
morestring[]read-onlyThe additional name parts of the first name.
asNamesName[]read-onlyThe combined version of the value and more if any.
MethodsReturnsDescription
toString(boolean)stringReturns a string representation of the namon.
initials(boolean)string[]Gets the initials (first character) of this name.
copyWith({ first?: string; more?: string[] })FirstNameMakes a copy of the current name.

LastName

This class is an extended arm of Name and represents a surname. It handles two types of surname: a father's and a mother's.

constructor
/**
* Creates an extended version of `Name` and flags it as a last name `type`.
*
* Some people may keep their `mother`'s surname and want to keep a clear cut
* from their `father`'s surname. However, there are no clear rules about it.
*/
constructor(father: string, mother?: string, readonly format = Surname.FATHER)

Pay attention to the third argument (optional): format. It defines how the surname per se should be output. By default, the father's name is considered.

Example:

import { LastName, Surname } from 'namefully'
const lastName = new LastName('Pitt')
lastName.toString() // => Pitt
lastName.toString(Surname.FATHER) // => Pitt
lastName.toString(Surname.MOTHER) // => '' (empty string)
lastName.toString(Surname.HYPHENATED) // => Pitt
// with the mother's name
const lastnames = new LastName('Pitt', 'Jolie')
lastNames.tostring() // => Pitt Jolie
lastNames.tostring(Surname.FATHER) // => Pitt
lastNames.tostring(Surname.MOTHER) // => Jolie
lastNames.tostring(Surname.HYPHENATED) // => Pitt-Jolie
// with the mother's name and format
const format = new LastName('Pitt', 'Jolie', 'mother')
format.toString() // => Jolie
format.toString(Surname.FATHER) // => Pitt
format.toString(Surname.MOTHER) // => Jolie
format.toString(Surname.HYPHENATED) // => Pitt-Jolie

API:

PropertiesTypeModifierDescription
hasMotherbooleanread-onlyReturns true if the mother's surname is defined.
fatherstring[]read-onlyThe surname inherited from a father side.
motherstring[]read-onlyThe surname inherited from a mother side.
asNamesName[]read-onlyThe combined version of the father and mother if any.
MethodsReturnsDescription
toString(Surname)stringReturns a string representation of the namon.
initials(Surname)string[]Gets the initials (first character) of this name.
copyWith({ father?: string; mother?: string; format?: Surname })LastNameMakes a copy of the current name.

Not sure to know how to use these classes? See some examples.

Back to Top