Skip to content

Commit 24f4cf5

Browse files
committed
Correções
- Melhoria no construtor do Audit - Atualizando readme com um exemplo
1 parent 5f2437d commit 24f4cf5

File tree

5 files changed

+119
-75
lines changed

5 files changed

+119
-75
lines changed

README.md

Lines changed: 93 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,60 @@
22

33
Entity diff generator.
44

5-
de:
5+
How to use:
6+
``` typescript
67

7-
``` javascript
8-
{
8+
import { Audit } from "entity-diff";
9+
10+
const audit = new Audit();
11+
12+
const before = {
13+
name: "Lucas Oliveira",
14+
age: 20
15+
}
16+
17+
const after = {
18+
name: "Lucas",
19+
age: 20
20+
}
21+
22+
const result = audit.diff(before, after);
23+
24+
// result:
25+
// [
26+
// {
27+
// key: "name",
28+
// from: "Lucas Oliveira",
29+
// to: "Lucas"
30+
// }
31+
// ]
32+
33+
```
34+
35+
36+
Audit Options:
37+
``` typescript
38+
39+
import { Audit } from "entity-diff";
40+
41+
const audit = new Audit(
42+
["id"], // ignore: list with ignored properties (optional)
43+
44+
[ // options: custom diff options for especific properties (optional)
45+
{
46+
key: // key name (required)
47+
title: // name displayed in diff (optional)
48+
customFormater: // function to customize the rendering of the `from` and `to` (optional)
49+
50+
arrayOptions: { // arrayOptions: diff array options (optional)
51+
key: // used to search for the object in the other array (required)
52+
name: // property with the name displayed in the diff (optional)
53+
}
54+
}
55+
]
56+
);
57+
58+
const before = {
959
id: 1,
1060
nome: "Fulano da silva",
1161
empresa: {
@@ -23,13 +73,9 @@ de:
2373
nome: "USUARIO"
2474
}
2575
]
26-
}
27-
```
76+
};
2877

29-
para:
30-
31-
``` javascript
32-
{
78+
const after = {
3379
id: 1,
3480
nome: "Fulano",
3581
empresa: {
@@ -47,68 +93,70 @@ para:
4793
nome: "PROGRAMADOR"
4894
}
4995
]
50-
}
96+
};
97+
98+
const diffs = audit.diff(before, after);
5199
```
52100

53-
**diff**
101+
**diffs**
54102

55-
``` javascript
103+
``` json
56104
[
57105
{
58-
key: "nome",
59-
from: "Fulano da silva",
60-
to: "Fulano"
106+
"key": "nome",
107+
"from": "Fulano da silva",
108+
"to": "Fulano"
61109
},
62110
{
63-
key: "empresa",
64-
type: "EDITADO",
65-
details: [
111+
"key": "empresa",
112+
"type": "EDITADO",
113+
"details": [
66114
{
67-
key: "nome",
68-
from: "Algum lugar",
69-
to: "Outro lugar"
115+
"key": "nome",
116+
"from": "Algum lugar",
117+
"to": "Outro lugar"
70118
},
71119
{
72-
key: "cnpj",
73-
from: "12345678910",
74-
to: "10987654321"
120+
"key": "cnpj",
121+
"from": "12345678910",
122+
"to": "10987654321"
75123
}
76124
]
77125
},
78126
{
79-
key: "permissoes",
80-
type: "LISTA",
81-
details: [
127+
"key": "permissoes",
128+
"type": "LISTA",
129+
"details": [
82130
{
83-
key: "ADMINISTRADOR",
84-
type: "EDITADO",
85-
details: [
131+
"key": "ADMINISTRADOR",
132+
"type": "EDITADO",
133+
"details": [
86134
{
87-
key: "nome",
88-
from: "ADMINISTRADOR",
89-
to: "SUPER_USER"
135+
"key": "nome",
136+
"from": "ADMINISTRADOR",
137+
"to": "SUPER_USER"
90138
}
91139
]
92140
},
93141
{
94-
key: "PROGRAMADOR",
95-
type: "NOVO",
96-
details: [
142+
"key": "PROGRAMADOR",
143+
"type": "NOVO",
144+
"details": [
97145
{
98-
key: "nome",
99-
from: null,
100-
to: "PROGRAMADOR"
146+
"key": "nome",
147+
"from": null,
148+
"to": "PROGRAMADOR"
101149
}
102150
]
103151
},
104152
{
105-
key: "USUARIO",
106-
type: "REMOVIDO",
107-
details: [
153+
"key": "USUARIO",
154+
"type": "REMOVIDO",
155+
"details": [
108156
{
109-
key: "nome",
110-
from: "USUARIO",
111-
to: null
157+
"key": "nome",
158+
"from": "USUARIO",
159+
"to": null
112160
}
113161
]
114162
},

dist/Diff.d.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ export declare enum DiffType {
44
REMOVED = "REMOVIDO",
55
ARRAY = "LISTA"
66
}
7-
interface AuditProps {
8-
ignore?: string[];
9-
options?: AuditKeyOptions[];
10-
}
117
export interface AuditKeyOptions {
128
key: string;
139
title?: string;
@@ -27,7 +23,7 @@ export interface Diff {
2723
export declare class Audit {
2824
private ignore;
2925
private options;
30-
constructor({ ignore, options }: AuditProps);
26+
constructor(ignore?: string[], options?: AuditKeyOptions[]);
3127
diff(from: any, to: any): Diff[];
3228
private deepDiffs;
3329
private addSimpleAudit;
@@ -42,4 +38,3 @@ export declare class Audit {
4238
private hasDiff;
4339
private diffTypeDiscover;
4440
}
45-
export {};

dist/Diff.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ var DiffType;
2828
DiffType["ARRAY"] = "LISTA";
2929
})(DiffType = exports.DiffType || (exports.DiffType = {}));
3030
var Audit = /** @class */ (function () {
31-
function Audit(_a) {
32-
var ignore = _a.ignore, options = _a.options;
33-
this.ignore = ignore !== null && ignore !== void 0 ? ignore : [];
34-
this.options = options !== null && options !== void 0 ? options : [];
31+
function Audit(ignore, options) {
32+
if (ignore === void 0) { ignore = []; }
33+
if (options === void 0) { options = []; }
34+
this.ignore = ignore;
35+
this.options = options;
3536
}
3637
Audit.prototype.diff = function (from, to) {
3738
var _this = this;
@@ -40,14 +41,14 @@ var Audit = /** @class */ (function () {
4041
.filter(function (key) { return _this.hasDiff(from, to, key); })
4142
.reduce(function (diffs, key) { return _this.deepDiffs(diffs, from[key], to[key], key); }, root);
4243
};
43-
Audit.prototype.deepDiffs = function (diffs, de, para, key) {
44-
if (utils_1.isArray(de, para)) {
45-
return this.addArrayDiff(diffs, de, para, key);
44+
Audit.prototype.deepDiffs = function (diffs, from, to, key) {
45+
if (utils_1.isArray(from, to)) {
46+
return this.addArrayDiff(diffs, from, to, key);
4647
}
47-
if (utils_1.isObject(de, para)) {
48-
return this.addObjectDiff(diffs, de, para, key);
48+
if (utils_1.isObject(from, to)) {
49+
return this.addObjectDiff(diffs, from, to, key);
4950
}
50-
return this.addSimpleAudit(diffs, de, para, key);
51+
return this.addSimpleAudit(diffs, from, to, key);
5152
};
5253
Audit.prototype.addSimpleAudit = function (diffs, from, to, key) {
5354
var _a;

src/Diff.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ export class Audit {
3535
private ignore: string[];
3636
private options: AuditKeyOptions[];
3737

38-
constructor({ ignore, options }: AuditProps) {
39-
this.ignore = ignore ?? [];
40-
this.options = options ?? [];
38+
constructor(ignore: string[] = [], options: AuditKeyOptions[] = []) {
39+
this.ignore = ignore;
40+
this.options = options;
4141
}
4242

4343
public diff(from: any, to: any): Diff[] {
@@ -53,17 +53,17 @@ export class Audit {
5353
key), root);
5454
}
5555

56-
private deepDiffs(diffs: Diff[], de: any, para: any, key: string) {
56+
private deepDiffs(diffs: Diff[], from: any, to: any, key: string) {
5757

58-
if (isArray(de, para)) {
59-
return this.addArrayDiff(diffs, de, para, key);
58+
if (isArray(from, to)) {
59+
return this.addArrayDiff(diffs, from, to, key);
6060
}
6161

62-
if (isObject(de, para)) {
63-
return this.addObjectDiff(diffs, de, para, key)
62+
if (isObject(from, to)) {
63+
return this.addObjectDiff(diffs, from, to, key)
6464
}
6565

66-
return this.addSimpleAudit(diffs, de, para, key);
66+
return this.addSimpleAudit(diffs, from, to, key);
6767
}
6868

6969
private addSimpleAudit(diffs: Diff[], from: any, to: any, key: string): Diff[] {

src/__tests__/diff.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe("testar auditoria", () => {
1212
}
1313
];
1414

15-
const audit = new Audit({ ignore: ["id"] });
15+
const audit = new Audit(["id"]);
1616

1717
const from = {
1818
id: 1,
@@ -94,9 +94,9 @@ describe("testar auditoria", () => {
9494
}
9595
];
9696

97-
const audit = new Audit({
98-
ignore: ["id"],
99-
options: [
97+
const audit = new Audit(
98+
["id"],
99+
[
100100
{
101101
key: "permissoes",
102102
arrayOptions: {
@@ -105,7 +105,7 @@ describe("testar auditoria", () => {
105105
}
106106
}
107107
]
108-
});
108+
);
109109

110110
const from = {
111111
id: 1,

0 commit comments

Comments
 (0)