-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathindex.ts
More file actions
90 lines (77 loc) · 2.23 KB
/
index.ts
File metadata and controls
90 lines (77 loc) · 2.23 KB
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
import { Result, ValueObject } from 'rich-domain';
const regexHash = /^[0-9]{5}-[0-9]{3}$|^[0-9]{8}$/;
class ZipCode extends ValueObject<string> {
protected static readonly REGEX = regexHash;
protected static readonly MESSAGE: string = 'Invalid zip code';
private constructor(prop: string) {
super(prop);
}
/**
* @returns value as string. always only numbers
* @example 75520140
*/
value(): string {
return this.props;
}
/**
* @description add hyphen and dot to cpf value.
* @example before "75520140"
* @example after "75520-140"
*/
public static addMask(zipCode: string): string {
if (typeof zipCode !== 'string') return '';
if (zipCode.includes('-')) return zipCode.slice(0, 9);
return zipCode.slice(0, 5) + '-' + zipCode.slice(5, 8);
}
/**
* @description remove hyphen and dot from cpf value.
* @example before "75520-140"
* @example after "75520140"
*/
public static removeSpecialChars(zipCode: string): string {
return this.util.string(zipCode).removeSpecialChars();
}
/**
* @description add special chars to numbers
* @returns zip code as string following pattern xxxxx-xxx
*/
toPattern(): string {
return ZipCode.addMask(this.props);
}
/**
*
* @param value PostalCode as string
* @returns true if value match with pattern and false if do not.
*/
public static isValid(value: string): boolean {
return this.isValidProps(value);
}
/**
*
* @param value PostalCode as string
* @returns true if value match with pattern and false if do not.
*/
public static isValidProps(value: string): boolean {
return this.validator.string(value).match(ZipCode.REGEX);
}
/**
*
* @param value value as string
* @returns instance of ZipCode or throw an error
*/
public static init(value: string): ZipCode {
const isValidValue = ZipCode.isValidProps(value);
if (!isValidValue) throw new Error(ZipCode.MESSAGE);
const numbers = ZipCode.removeSpecialChars(value);
return new ZipCode(numbers);
}
public static create(value: string): Result<ZipCode | null> {
if (!ZipCode.isValidProps(value)) {
return Result.fail(ZipCode.MESSAGE);
}
const numbers = ZipCode.removeSpecialChars(value);
return Result.Ok(new ZipCode(numbers));
}
}
export { ZipCode };
export default ZipCode;