-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.ts
121 lines (107 loc) · 3.37 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { Result, ValueObject } from "rich-domain";
import isValidCpfDigit, { formatValueToCpfPattern } from "./util";
export class CPF extends ValueObject<string> {
protected static readonly REGEX = /^([0-9]{3})[\.]((?!\1)[0-9]{3})[\.]([0-9]{3})[-]([0-9]{2})$|^[0-9]{11}$/;
protected static readonly MESSAGE: string = 'Invalid value for cpf';
private constructor(value: string) {
super(value);
}
/**
* @description return a cpf value (only numbers).
* @example example "52734865211".
* @summary If you want cpf as pattern use `formatToCpfPattern` before get value.
*/
value(): string {
return this.props;
}
/**
* @description add hyphen and dot to cpf value.
* @example before "52734865211"
* @example after "527.348.652-11"
*/
toPattern(): string {
return formatValueToCpfPattern(this.props);
}
/**
* @description add hyphen and dot to cpf value.
* @example before "52734865211"
* @example after "527.348.652-11"
*/
public static addMask(cpf: string): string {
return formatValueToCpfPattern(cpf);
}
/**
* @description remove hyphen and dot from cpf value.
* @example before "527.348.652-11"
* @example after "52734865211"
*/
public static removeSpecialChars(cpf: string): string {
return this.util.string(cpf).removeSpecialChars();
}
/**
*
* @param cpf value as string only number or pattern Or instance of CPF.
* @returns true if cpf match with instance value and false if not.
* @example param "52734865211"
* @example param "527.348.652-11"
*/
compare(cpf: string | CPF): boolean {
if (typeof cpf === 'string') {
const valueA = this.util.string(cpf).removeSpecialChars();
const valueB = this.util
.string(this.props)
.removeSpecialChars();
return valueA === valueB;
}
if (cpf instanceof CPF) return cpf.isEqual(this);
return false;
}
/**
* @description check if cpf value is a valid pattern and has a valid digit sum.
* @param value cpf as string
* @returns true if value is valid and false if not.
* @example "527.348.652-11"
* @example "72725477824"
*/
public static isValidProps(value: string): boolean {
const isValidPattern = CPF.REGEX.test(value);
const isValidDigits = isValidCpfDigit(value);
return isValidDigits && isValidPattern;
}
/**
* @description check if cpf value is a valid pattern and has a valid digit sum.
* @param value cpf as string
* @returns true if value is valid and false if not.
* @example "527.348.652-11"
* @example "72725477824"
*/
public static isValid(value: string): boolean {
return this.isValidProps(value);
}
/**
*
* @param value value as string
* @returns instance of CPF or throw an error
*/
public static init(value: string): CPF {
const isValidValue = CPF.isValidProps(value);
if (!isValidValue) throw new Error(CPF.MESSAGE);
return new CPF(this.util.string(value).removeSpecialChars());
}
/**
* @description create a cpf value object
* @param value cpf numbers as string
* @returns instance of Result with cpf value
* @example "527.348.652-11"
* @example "72725477824"
* @summary fails if provide an invalid pattern or a cpf with invalid digit sum
*/
public static create(value: string): Result<CPF | null> {
const isValidValue = CPF.isValidProps(value);
if (!isValidValue) {
return Result.fail(CPF.MESSAGE);
}
return Result.Ok(new CPF(this.util.string(value).removeSpecialChars()));
}
}
export default CPF;