Skip to content
Max edited this page Nov 16, 2023 · 11 revisions

Class: CPF

An immutable class to represent CPF documents.

Implements

  • Evaluable

Table of contents

Constructors

Properties

Accessors

Methods

Constructors

constructor

new CPF(numbers?): CPF

Creates a new immutable instance of CPF.

Parameters

Name Type Default value
numbers Iterable<number> []

Returns

CPF

Properties

#digits

Private #digits: number[] = []


#hashCode

Private #hashCode: null | number = null


#seed

Static Private #seed: number


Nil

Static Readonly Nil: CPF

An empty instance of CPF.

Accessors

length

get length(): number

The number of digits in the CPF.

Returns

number


size

get size(): number

The number of digits in the CPF.

Returns

number

Deprecated

Use length property instead

See

Methods

[iterator]

[iterator](): Generator<number, void, void>

Iterates over the digits of the CPF.

Returns

Generator<number, void, void>


at

at(index): undefined | number

Returns the digit located at the specified index. Negative integers count back from the last digit in the current CPF.

Parameters

Name Type
index number

Returns

undefined | number


checkValidity

checkValidity(): boolean

Returns true if the CPF is valid, false otherwise. A CPF is valid if they have 11 digits and the two last digits satisfies the validation algorithm.

Returns

boolean


equals

equals(other): boolean

Returnstrue if the given value is equal to this CPF, false otherwise. Two CPFs are equal if they have the same sequence of digits.

Parameters

Name Type
other unknown

Returns

boolean

Implementation of

Evaluable.equals


format

format(): string

Formats the CPF in the standard pattern "###.###.###-##".

Returns

string


getValidity

getValidity(): CPFValidityStateFlags

Returns an object that represents the state of validity of the CPF.

Returns

CPFValidityStateFlags

See


hashCode

hashCode(): number

Returns

number

Implementation of

Evaluable.hashCode


toArray

toArray(): number[]

Returns the CPF digits in an array.

Returns

number[]


toJSON

toJSON(): string

Serializes the digits of the CPF into JSON string.

Returns

string


toString

toString(): string

Returns a string representation of an object.

Returns

string


with

with(index, digit): CPF

Returns a copy of the CPF with the digit at the provided index overwritten with the given value. If the index is negative, then it replaces from the end of the array. If the index after normalization is out of bounds, a RangeError is thrown.

Parameters

Name Type
index number
digit number

Returns

CPF


create

create(): CPF

Creates new valid CPF instance of random numbers.

Returns

CPF


from

from(formatted): CPF

Creates a CPF instance from an string. The string can be formatted or not. If not enough digits are found on the string, an incomplete CPF will be returned.

Parameters

Name Type
formatted string

Returns

CPF


getCheckDigit

getCheckDigit(digits, start?, end?): number

Returns the CPF check digit from interval of the given digits.

Parameters

Name Type Default value
digits readonly number[] undefined
start number 0
end number digits.length

Returns

number

Type: CPFValidityStateFlags

Ƭ CPFValidityStateFlags: Object

Represents the state of validity of the CPF.

Type declaration

Name Type Description
tooShort boolean Flagged as true if the count of CPF digits between, inclusively, one and ten.
typeMismatch boolean Flagged as true if the number of digits is eleven but the check digit algorithm fails.
valueMissing boolean Flagged as true if the count of CPF digits is zero.