22 lines
511 B
TypeScript
22 lines
511 B
TypeScript
import { DomainError } from '@/shared/exceptions/domain.exception';
|
|
|
|
export class PhoneNumber {
|
|
constructor(public readonly value: string) {
|
|
if (!/^1[3-9]\d{9}$/.test(value)) {
|
|
throw new DomainError('手机号格式错误');
|
|
}
|
|
}
|
|
|
|
static create(value: string): PhoneNumber {
|
|
return new PhoneNumber(value);
|
|
}
|
|
|
|
equals(other: PhoneNumber): boolean {
|
|
return this.value === other.value;
|
|
}
|
|
|
|
masked(): string {
|
|
return this.value.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
|
|
}
|
|
}
|