Comparing version 1.6.4 to 1.7.0
@@ -1,2 +0,2 @@ | ||
import { Delta } from '../domain'; | ||
import { Delta, DeltaOptions } from '../domain'; | ||
export interface IObjectHelper { | ||
@@ -18,3 +18,3 @@ clone(obj: any): any; | ||
filter(obj: any, predicate: (value: any) => boolean): any[]; | ||
delta(current: any, old?: any): Delta; | ||
delta(current: any, old?: any, options?: DeltaOptions): Delta; | ||
} |
@@ -32,1 +32,4 @@ export interface NewValue { | ||
} | ||
export interface DeltaOptions { | ||
ignore?: string[]; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { Delta } from '../index'; | ||
import { Delta, DeltaOptions } from '../index'; | ||
import { IObjectHelper, IHttpHelper, IValidator } from '../application'; | ||
@@ -21,7 +21,8 @@ export declare class ObjectHelper implements IObjectHelper { | ||
filter(obj: any, predicate: (value: any) => boolean): any[]; | ||
delta(current: any, old?: any): Delta; | ||
private deltaValue; | ||
private deltaArray; | ||
private deltaArrayOfObject; | ||
private deltaChange; | ||
delta(current: any, old?: any, options?: DeltaOptions): Delta; | ||
private _delta; | ||
private _deltaValue; | ||
private _deltaArray; | ||
private _deltaArrayOfObject; | ||
private _deltaChange; | ||
} |
@@ -247,3 +247,6 @@ "use strict"; | ||
} | ||
delta(current, old) { | ||
delta(current, old, options) { | ||
return this._delta('', current, old, options); | ||
} | ||
_delta(path, current, old, options) { | ||
const delta = new index_1.Delta(); | ||
@@ -254,9 +257,17 @@ if (current === undefined || current === null) { | ||
if (typeof current !== 'object') { | ||
this.deltaValue('', current, old, delta); | ||
if (!(options && options.ignore && options.ignore.includes(path))) { | ||
this._deltaValue(path, '', current, old, delta, options || {}); | ||
} | ||
} | ||
else if (Array.isArray(current)) { | ||
this.deltaArray('', current, old, delta); | ||
if (!(options && options.ignore && options.ignore.includes(path))) { | ||
this._deltaArray(path, '', current, old, delta, options || {}); | ||
} | ||
} | ||
else { | ||
for (const name in current) { | ||
const _path = path === '' ? name : `${path}.${name}`; | ||
if (options && options.ignore && options.ignore.includes(_path)) { | ||
continue; | ||
} | ||
const currentValue = current[name]; | ||
@@ -269,3 +280,3 @@ if (old === undefined || old === null) { | ||
else { | ||
this.deltaValue(name, currentValue, old[name], delta); | ||
this._deltaValue(path === '' ? name : `${path}.${name}`, name, currentValue, old[name], delta, options || {}); | ||
} | ||
@@ -275,2 +286,6 @@ } | ||
for (const name in old) { | ||
const _path = path === '' ? name : `${path}.${name}`; | ||
if (options && options.ignore && options.ignore.includes(_path)) { | ||
continue; | ||
} | ||
if (current[name] === undefined) { | ||
@@ -286,3 +301,3 @@ if (delta.remove === undefined) | ||
} | ||
deltaValue(name, currentValue, oldValue, delta) { | ||
_deltaValue(path, name, currentValue, oldValue, delta, options) { | ||
if (oldValue === undefined) { | ||
@@ -304,7 +319,7 @@ if (delta.new === undefined) | ||
else if (Array.isArray(currentValue)) { | ||
this.deltaArray(name, currentValue, oldValue, delta); | ||
this._deltaArray(path, name, currentValue, oldValue, delta, options); | ||
} | ||
else if (this.validator.isObject(currentValue)) { | ||
const objectDelta = this.delta(currentValue, oldValue); | ||
const change = this.deltaChange(objectDelta); | ||
const objectDelta = this._delta(path, currentValue, oldValue, options); | ||
const change = this._deltaChange(objectDelta); | ||
if (change) { | ||
@@ -332,3 +347,3 @@ if (delta.changed === undefined) | ||
} | ||
deltaArray(name, current, old, delta) { | ||
_deltaArray(path, name, current, old, delta, options) { | ||
if (current && Array.isArray(current) && (!old || !Array.isArray(old))) { | ||
@@ -382,15 +397,13 @@ const arrayDelta = new index_1.Delta(); | ||
else { | ||
const arrayDelta = this.deltaArrayOfObject(name, current, old); | ||
if (arrayDelta) { | ||
const change = this.deltaChange(arrayDelta); | ||
if (delta.children === undefined) | ||
delta.children = []; | ||
delta.children.push({ name, type: 'array', change, delta: arrayDelta }); | ||
} | ||
const arrayDelta = this._deltaArrayOfObject(path, name, current, old, options); | ||
const change = this._deltaChange(arrayDelta); | ||
if (delta.children === undefined) | ||
delta.children = []; | ||
delta.children.push({ name, type: 'array', change, delta: arrayDelta }); | ||
} | ||
} | ||
deltaArrayOfObject(name, current, old) { | ||
_deltaArrayOfObject(path, name, current, old, options) { | ||
const key = Object.keys(current[0]).find(p => ['id', 'code', 'name', 'key'].includes(p.toLowerCase())); | ||
if (key === undefined) { | ||
return null; | ||
throw new Error(`key not found in ${path}`); | ||
} | ||
@@ -406,4 +419,4 @@ const arrayDelta = new index_1.Delta(); | ||
else { | ||
const objectDelta = this.delta(item, oldItem); | ||
const change = this.deltaChange(objectDelta); | ||
const objectDelta = this._delta(path, item, oldItem, options); | ||
const change = this._deltaChange(objectDelta); | ||
if (change) { | ||
@@ -430,3 +443,3 @@ if (arrayDelta.changed === undefined) | ||
} | ||
deltaChange(delta) { | ||
_deltaChange(delta) { | ||
const main = (delta.changed ? delta.changed.length : 0) + (delta.remove ? delta.remove.length : 0) + (delta.new ? delta.new.length : 0) > 0; | ||
@@ -433,0 +446,0 @@ if (main) { |
{ | ||
"name": "h3lp", | ||
"version": "1.6.4", | ||
"version": "1.7.0", | ||
"description": "Helper for nodeJs", | ||
@@ -5,0 +5,0 @@ "author": "Flavio Lionel Rita <flaviolrita@hotmail.com>", |
@@ -60,3 +60,10 @@ "use strict"; | ||
}); | ||
test('delta ignore option', () => { | ||
const a = { a: 1, b: [{ code: 'BR', name: 'Brasil', zones: [{ code: 'Belén', gmt: -3 }, { code: 'Manaos', gmt: -4 }, { code: 'Rio Branco ', gmt: -5 }] }] }; | ||
const b = { a: 1, b: [{ code: 'BR', name: 'Brasil', zones: [{ code: 'Belén', gmt: -3 }, { code: 'Manaos', gmt: -4 }, { code: 'Rio Branco ', gmt: -6 }] }] }; | ||
const expected = '{"unchanged":[{"name":"a","value":1}],"children":[{"name":"b","type":"array","change":false,"delta":{"unchanged":[{"name":"b","value":{"code":"BR","name":"Brasil","zones":[{"code":"Belén","gmt":-3},{"code":"Manaos","gmt":-4},{"code":"Rio Branco ","gmt":-6}]}}]}}]}'; | ||
const delta = __1.h3lp.obj.delta(a, b, { ignore: ['b.zones.gmt'] }); | ||
expect(JSON.stringify(delta)).toStrictEqual(expected); | ||
}); | ||
}); | ||
//# sourceMappingURL=object.test.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
217514
2810