Skip to content

Commit 2cd03bf

Browse files
committed
fix: change type create method return null #194
1 parent 5c914f5 commit 2cd03bf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+263
-167
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ All notable changes to this project will be documented in this file.
1010

1111
---
1212

13+
### [4.0.5] - 2024-09-26
14+
15+
### Fix
16+
17+
- update rich-domain lib to check nullish type: now 'create' return a possibly null value in Result instance.
18+
19+
---
20+
1321
### [4.0.4] - 2024-09-26
1422

1523
### Fix

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ export default class Money extends ValueObject<Props> {
261261
}
262262

263263
// factory method to create an instance and validate value.
264-
public static create(amount: number): Result<Money> {
264+
public static create(amount: number): Result<Money | null> {
265265

266266
const isValid = this.isValidProps({ amount });
267267
if(!isValid) return Fail("Invalid amount for money");
@@ -286,7 +286,7 @@ console.log(resA.isOk());
286286

287287

288288
// money instance
289-
const moneyA = resA.value();
289+
const moneyA = resA.value() as Money;
290290

291291
moneyA.get("amount");
292292

@@ -297,7 +297,7 @@ moneyA.isGt(Money.zero());
297297

298298
// > true
299299

300-
const moneyB = Money.create(100).value();
300+
const moneyB = Money.create(100).value() as Money;
301301

302302
const moneyC = moneyA.sum(moneyB);
303303

@@ -364,12 +364,12 @@ How to use entity instance
364364
```ts
365365

366366
// operation result
367-
const total = Money.create(500).value();
367+
const total = Money.create(500).value() as Money;
368368
const discount = Money.zero();
369369
const fees = Money.zero();
370370

371371
// create a payment
372-
const payment = Payment.create({ total, discount, fees }).value();
372+
const payment = Payment.create({ total, discount, fees }).value() as Payment;
373373

374374
// create fee and discount
375375
const fee = Money.create(17.50).value();

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "type-ddd",
3-
"version": "4.0.4",
3+
"version": "4.0.5",
44
"description": "This package provide utils file and interfaces to assistant build a complex application with domain driving design",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -53,7 +53,7 @@
5353
},
5454
"homepage": "https://github.com/4lessandrodev/type-ddd/tree/main",
5555
"peerDependencies": {
56-
"rich-domain": "^1.23.4"
56+
"rich-domain": "^1.24.0"
5757
},
5858
"devDependencies": {
5959
"@types/jest": "^27.0.1",
@@ -65,7 +65,7 @@
6565
"lint-staged": "^15.0.1",
6666
"madge": "^8.0.0",
6767
"prettier": "^3.0.0",
68-
"rich-domain": "^1.23.4",
68+
"rich-domain": "^1.24.0",
6969
"rimraf": "^5.0.5",
7070
"ts-jest": "^27.1.4",
7171
"ts-node": "^10.7.0",

packages/cnpj/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ All notable changes to this project will be documented in this file.
88

99
## Released
1010

11+
---
12+
13+
### [0.0.3] - 2024-11-28
14+
15+
### Fix
16+
17+
- update rich-domain lib to check nullish type: now 'create' return a possibly null value in Result instance.
18+
1119
### [0.0.2] - 2024-05-31
1220

1321
### Docs

packages/cnpj/__tests__/cnpj.value-object.util.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ describe('CNPJ Value Object', () => {
1010
it('should create a valid CNPJ with special characters removed', () => {
1111
const valueObject = CNPJValueObject.create('43.909.299/0001-04');
1212
expect(valueObject.isOk()).toBeTruthy();
13-
expect(valueObject.value().value()).toBe('43909299000104');
13+
expect(valueObject.value()?.value()).toBe('43909299000104');
1414
});
1515

1616
it('should create a valid CNPJ with numbers only', () => {
1717
const valueObject = CNPJValueObject.create('60105617000101');
1818
expect(valueObject.isOk()).toBeTruthy();
19-
expect(valueObject.value().value()).toBe('60105617000101');
19+
expect(valueObject.value()?.value()).toBe('60105617000101');
2020
});
2121

2222
it('should initialize an instance without error', () => {
@@ -61,25 +61,25 @@ describe('CNPJ Value Object', () => {
6161
it('should format a CNPJ with special characters', () => {
6262
const valueObject =
6363
CNPJValueObject.create('20.798.751/0001-02').value();
64-
expect(valueObject.toPattern()).toBe('20.798.751/0001-02');
64+
expect(valueObject?.toPattern()).toBe('20.798.751/0001-02');
6565
});
6666

6767
it('should format a CNPJ with special characters', () => {
6868
const valueObject =
6969
CNPJValueObject.create('65.389.009/0001-81').value();
70-
expect(valueObject.toPattern()).toBe('65.389.009/0001-81');
70+
expect(valueObject?.toPattern()).toBe('65.389.009/0001-81');
7171
});
7272

7373
it('should format a CNPJ with special characters', () => {
7474
const valueObject =
7575
CNPJValueObject.create('02.470.431/0001-47').value();
76-
expect(valueObject.toPattern()).toBe('02.470.431/0001-47');
76+
expect(valueObject?.toPattern()).toBe('02.470.431/0001-47');
7777
});
7878

7979
it('should format a CNPJ with special characters and remove them later', () => {
8080
const valueObject =
8181
CNPJValueObject.create('62.412.404/0001-40').value();
82-
expect(valueObject.toPattern()).toBe('62.412.404/0001-40');
82+
expect(valueObject?.toPattern()).toBe('62.412.404/0001-40');
8383
});
8484
});
8585

@@ -89,27 +89,27 @@ describe('CNPJ Value Object', () => {
8989
const valueObject = CNPJValueObject.create(validCNPJ).value();
9090

9191
// Compare with invalid CNPJ
92-
let isEqual = valueObject.compare('invalid');
92+
let isEqual = valueObject?.compare('invalid');
9393
expect(isEqual).toBeFalsy();
9494

9595
// Compare with different valid CNPJ
96-
isEqual = valueObject.compare('22.606.062/0001-20');
96+
isEqual = valueObject?.compare('22.606.062/0001-20');
9797
expect(isEqual).toBeFalsy();
9898

9999
// Compare with the same valid CNPJ
100-
isEqual = valueObject.compare(validCNPJ);
100+
isEqual = valueObject?.compare(validCNPJ);
101101
expect(isEqual).toBeTruthy();
102102

103103
// Compare with a valid CNPJ with different format
104-
isEqual = valueObject.compare('22606062000155');
104+
isEqual = valueObject?.compare('22606062000155');
105105
expect(isEqual).toBeFalsy();
106106

107107
// Compare with a valid CNPJ with the same value but different format
108-
isEqual = valueObject.compare('22606062000184');
108+
isEqual = valueObject?.compare('22606062000184');
109109
expect(isEqual).toBeTruthy();
110110

111111
// Compare with the same valid CNPJ
112-
isEqual = valueObject.compare('22.606.062/0001-84');
112+
isEqual = valueObject?.compare('22.606.062/0001-84');
113113
expect(isEqual).toBeTruthy();
114114
});
115115

packages/cnpj/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export class CNPJ extends ValueObject<string> {
111111
* @example "22398345000188"
112112
* @summary fails if provide an invalid pattern or a cnpj with invalid digit sum
113113
*/
114-
public static create(value: string): Result<CNPJ> {
114+
public static create(value: string): Result<CNPJ | null> {
115115
const isValidValue = CNPJ.isValidProps(value);
116116

117117
if (!isValidValue) {

packages/cnpj/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@type-ddd/cnpj",
33
"description": "Library that provides TypeScript type definitions for handling CNPJ (Cadastro Nacional da Pessoa Jurídica) in Domain-Driven Design contexts. It facilitates the validation and manipulation of CNPJ numbers, ensuring they adhere to the Brazilian legal standards.",
4-
"version": "0.0.2",
4+
"version": "0.0.3",
55
"main": "index.js",
66
"types": "index.d.ts",
77
"author": "Alessandro Dev",
@@ -33,7 +33,7 @@
3333
"build": "tsc"
3434
},
3535
"peerDependencies": {
36-
"rich-domain": "^1.23.4"
36+
"rich-domain": "^1.24.0"
3737
},
3838
"files": [
3939
"index.js",

packages/cpf/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ All notable changes to this project will be documented in this file.
88

99
## Released
1010

11+
---
12+
13+
### [0.0.3] - 2024-11-28
14+
15+
### Fix
16+
17+
- update rich-domain lib to check nullish type: now 'create' return a possibly null value in Result instance.
18+
1119
### [0.0.2] - 2024-05-31
1220

1321
### Docs

packages/cpf/__tests__/cpf.value-object.util.spec.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,94 +9,94 @@ describe('cpf.value-object', () => {
99
it('should create a valid cpf with special chars and remove special chars on get value', () => {
1010
const valueObject = CPFValueObject.create('667.324.914-58');
1111
expect(valueObject.isOk()).toBeTruthy();
12-
expect(valueObject.value().value()).toBe('66732491458');
12+
expect(valueObject.value()?.value()).toBe('66732491458');
1313
});
1414

1515
it('should create a valid cpf with special chars and remove special chars on get value', () => {
1616
const valueObject = CPFValueObject.create('934.665.143-12');
1717
expect(valueObject.isOk()).toBeTruthy();
18-
expect(valueObject.value().value()).toBe('93466514312');
18+
expect(valueObject.value()?.value()).toBe('93466514312');
1919
});
2020

2121
it('should create a valid cpf with special chars and remove special chars on get value', () => {
2222
const valueObject = CPFValueObject.create('690.574.738-60');
2323
expect(valueObject.isOk()).toBeTruthy();
24-
expect(valueObject.value().value()).toBe('69057473860');
24+
expect(valueObject.value()?.value()).toBe('69057473860');
2525
});
2626

2727
it('should create a valid cpf with special chars and remove special chars on get value', () => {
2828
const valueObject = CPFValueObject.create('324.123.359-66');
2929
expect(valueObject.isOk()).toBeTruthy();
30-
expect(valueObject.value().value()).toBe('32412335966');
30+
expect(valueObject.value()?.value()).toBe('32412335966');
3131
});
3232

3333
it('should create a valid cpf with special chars and remove special chars on get value', () => {
3434
const valueObject = CPFValueObject.create('673.761.543-02');
3535
expect(valueObject.isOk()).toBeTruthy();
36-
expect(valueObject.value().value()).toBe('67376154302');
36+
expect(valueObject.value()?.value()).toBe('67376154302');
3737
});
3838

3939
it('should create a valid cpf with special chars and remove special chars on get value', () => {
4040
const valueObject = CPFValueObject.create('024.815.901-12');
4141
expect(valueObject.isOk()).toBeTruthy();
42-
expect(valueObject.value().value()).toBe('02481590112');
42+
expect(valueObject.value()?.value()).toBe('02481590112');
4343
});
4444

4545
it('should create a valid cpf with special chars and remove special chars on get value', () => {
4646
const valueObject = CPFValueObject.create('754.179.880-06');
4747
expect(valueObject.isOk()).toBeTruthy();
48-
expect(valueObject.value().value()).toBe('75417988006');
48+
expect(valueObject.value()?.value()).toBe('75417988006');
4949
});
5050

5151
it('should format a cpf to add special chars', () => {
5252
const valueObject = CPFValueObject.create('667.324.914-58').value();
53-
expect(valueObject.toPattern()).toBe('667.324.914-58');
53+
expect(valueObject?.toPattern()).toBe('667.324.914-58');
5454
});
5555

5656
it('should format a cpf to add special chars', () => {
5757
const valueObject = CPFValueObject.create('578.363.883-87').value();
58-
expect(valueObject.toPattern()).toBe('578.363.883-87');
58+
expect(valueObject?.toPattern()).toBe('578.363.883-87');
5959
});
6060

6161
it('should format a cpf to add special chars', () => {
6262
const valueObject = CPFValueObject.create('844.676.543-80').value();
63-
expect(valueObject.toPattern()).toBe('844.676.543-80');
63+
expect(valueObject?.toPattern()).toBe('844.676.543-80');
6464
});
6565

6666
it('should format a cpf to add special chars and remove it later', () => {
6767
const valueObject = CPFValueObject.create('667.324.914-58').value();
68-
expect(valueObject.toPattern()).toBe('667.324.914-58');
69-
expect(valueObject.value()).toBe('66732491458');
68+
expect(valueObject?.toPattern()).toBe('667.324.914-58');
69+
expect(valueObject?.value()).toBe('66732491458');
7070
});
7171

7272
it('should compare value on instance and provided value', () => {
7373
const valueObject = CPFValueObject.create('549.777.281-14').value();
74-
let isEqual = valueObject.compare('invalid');
74+
let isEqual = valueObject?.compare('invalid');
7575
expect(isEqual).toBeFalsy();
7676

77-
isEqual = valueObject.compare('549.777.281-15');
77+
isEqual = valueObject?.compare('549.777.281-15');
7878
expect(isEqual).toBeFalsy();
7979

80-
isEqual = valueObject.compare('549.777.281-14');
80+
isEqual = valueObject?.compare('549.777.281-14');
8181
expect(isEqual).toBeTruthy();
8282

83-
isEqual = valueObject.compare('54977728314');
83+
isEqual = valueObject?.compare('54977728314');
8484
expect(isEqual).toBeFalsy();
8585

86-
isEqual = valueObject.compare('54977728114');
86+
isEqual = valueObject?.compare('54977728114');
8787
expect(isEqual).toBeTruthy();
8888

89-
isEqual = valueObject.compare('54977728114');
89+
isEqual = valueObject?.compare('54977728114');
9090
expect(isEqual).toBeTruthy();
9191

92-
isEqual = valueObject.compare('549.777.281-14');
92+
isEqual = valueObject?.compare('549.777.281-14');
9393
expect(isEqual).toBeTruthy();
9494
});
9595

9696
it('should create a valid cpf only numbers', () => {
9797
const valueObject = CPFValueObject.create('53534317661');
9898
expect(valueObject.isOk()).toBeTruthy();
99-
expect(valueObject.value().value()).toBe('53534317661');
99+
expect(valueObject.value()?.value()).toBe('53534317661');
100100
expect(CPFValueObject.isValid('53534317661')).toBeTruthy();
101101
});
102102

@@ -118,13 +118,13 @@ describe('cpf.value-object', () => {
118118
it('should create a valid cpf only numbers', () => {
119119
const valueObject = CPFValueObject.create('53534317661');
120120
expect(valueObject.isOk()).toBeTruthy();
121-
expect(valueObject.value().value()).toBe('53534317661');
121+
expect(valueObject.value()?.value()).toBe('53534317661');
122122
});
123123

124124
it('should create a valid cpf only numbers', () => {
125125
const valueObject = CPFValueObject.create('98614591039');
126126
expect(valueObject.isOk()).toBeTruthy();
127-
expect(valueObject.value().value()).toBe('98614591039');
127+
expect(valueObject.value()?.value()).toBe('98614591039');
128128
});
129129

130130
it('should init an instance with success', () => {

packages/cpf/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class CPF extends ValueObject<string> {
107107
* @example "72725477824"
108108
* @summary fails if provide an invalid pattern or a cpf with invalid digit sum
109109
*/
110-
public static create(value: string): Result<CPF> {
110+
public static create(value: string): Result<CPF | null> {
111111
const isValidValue = CPF.isValidProps(value);
112112

113113
if (!isValidValue) {

0 commit comments

Comments
 (0)