Skip to content

Commit 208368a

Browse files
Merge pull request #221 from ArturHamannRonconi/fix/url-value-object
Fix/url value object
2 parents 2bd2485 + 6ae1acd commit 208368a

File tree

5 files changed

+45
-23
lines changed

5 files changed

+45
-23
lines changed

lib/utils/cpf.value-object.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ValueObject } from '../core';
22
import { Result } from '../core';
33
import isValidCpfDigit, {
44
formatValueToCpfPattern,
5+
removeSpecialCharsFromCpf,
56
} from './check-cpf-digit.util';
67
const regexCpf =
78
/^([0-9]{3})[\.]((?!\1)[0-9]{3})[\.]([0-9]{3})[-]([0-9]{2})$|^[0-9]{11}$/;
@@ -35,9 +36,11 @@ export class CPFValueObject extends ValueObject<Prop> {
3536
* @example after "52734865211"
3637
*/
3738
removeSpecialChars(): CPFValueObject {
38-
this.props.value = this.util
39-
.string(this.props.value)
40-
.removeSpecialChars();
39+
this.props.value = removeSpecialCharsFromCpf(this.props.value);
40+
41+
// this.props.value = this.util
42+
// .string(this.props.value)
43+
// .removeSpecialChars();
4144
return this;
4245
}
4346

@@ -59,10 +62,13 @@ export class CPFValueObject extends ValueObject<Prop> {
5962
* @example param "527.348.652-11"
6063
*/
6164
compare(cpf: string): boolean {
62-
const formattedCpf = this.util.string(cpf).removeSpecialChars();
63-
const instanceValue = this.util
64-
.string(this.props.value)
65-
.removeSpecialChars();
65+
// const formattedCpf = this.util.string(cpf).removeSpecialChars();
66+
// const instanceValue = this.util
67+
// .string(this.props.value)
68+
// .removeSpecialChars();
69+
70+
const formattedCpf = removeSpecialCharsFromCpf(cpf);
71+
const instanceValue = removeSpecialCharsFromCpf(this.props.value);
6672
return instanceValue === formattedCpf;
6773
}
6874

lib/utils/password.value-object.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@ class PasswordValueObject extends ValueObject<Prop> {
9797
public static isValidProps(value: string): boolean {
9898
const { string } = this.validator;
9999
if (!PasswordValueObject.isEncrypted(value)) {
100-
const passwordHasRequiredLength = string(
101-
value
102-
).hasLengthBetweenOrEqual(
100+
const passwordHasRequiredLength = string(value).hasLengthBetween(
103101
PasswordValueObject.MIN_LENGTH,
104102
PasswordValueObject.MAX_LENGTH
105103
);
@@ -108,6 +106,10 @@ class PasswordValueObject extends ValueObject<Prop> {
108106
return true;
109107
}
110108

109+
isEqual(password: PasswordValueObject) {
110+
return this.compare(password.value());
111+
}
112+
111113
/**
112114
*
113115
* @param value password to create

lib/utils/url.value-object.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ValueObject } from '../core';
22
import { Result } from '../core';
3-
const regexHash = /^\w{3,5}:\/{1,}\w{1,}\.\w*\.?\w{2,}/;
3+
const regexHash =
4+
/^(https?:\/\/)(([\da-z\.-]+))?\.?([a-z\.]{2,6})([\/\w \.-]*)*\/?(:\d{2,4})?(\/\w+)?$/;
45

56
interface Prop {
67
value: string;
@@ -29,7 +30,12 @@ class UrlValueObject extends ValueObject<Prop> {
2930
* @returns true if value is a valid url and false if does not
3031
*/
3132
public static isValidProps(value: string): boolean {
32-
return this.validator.string(value).match(UrlValueObject.REGEX);
33+
try {
34+
new URL(value);
35+
return true;
36+
} catch (error) {
37+
return false;
38+
}
3339
}
3440

3541
validation(value: string): boolean {

lib/utils/user-name.value-object.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@ export class UserNameValueObject extends ValueObject<Prop> {
3838
name[0].toUpperCase() + name.slice(1).toLowerCase();
3939
capitalized.push(lowerCaseName);
4040
}
41-
const value = this.util
42-
.string(capitalized.toString())
43-
.replace(',')
44-
.to(' ');
41+
42+
// const value = this.util
43+
// .string(capitalized.toString())
44+
// .replace(',')
45+
// .to(' ');
46+
47+
const value = capitalized.toString().replace(/,/g, ' ');
48+
4549
this.props.value = value;
4650
return this;
4751
}
@@ -98,10 +102,14 @@ export class UserNameValueObject extends ValueObject<Prop> {
98102
getInitials(): string {
99103
const names = this.props.value.split(' ');
100104
const letters = names.map((name) => name[0]);
101-
const initials = this.util
102-
.string(letters.toString())
103-
.replace(',')
104-
.to('.');
105+
106+
// const initials = this.
107+
// .string(letters.toString())
108+
// .replace(',')
109+
// .to('.');
110+
111+
const initials = letters.toString().replace(/,/g, '.');
112+
105113
return initials;
106114
}
107115

@@ -116,7 +124,7 @@ export class UserNameValueObject extends ValueObject<Prop> {
116124
*/
117125
public static isValidProps(value: string): boolean {
118126
const { string } = this.validator;
119-
return string(value).hasLengthBetweenOrEqual(
127+
return string(value).hasLengthBetween(
120128
UserNameValueObject.MIN_LENGTH,
121129
UserNameValueObject.MAX_LENGTH
122130
);

tests/utils/password.value-object.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ describe('password.value-object', () => {
7575
});
7676

7777
it('should password to be equal', () => {
78-
const passA = PasswordValueObject.random(12);
79-
const passB = passA.clone();
78+
const passA = PasswordValueObject.create('123456abc!').value();
79+
const passB = PasswordValueObject.create('123456abc!').value();
8080
const isEqual = passA.isEqual(passB);
8181
expect(isEqual).toBe(true);
8282
});

0 commit comments

Comments
 (0)