Skip to content

Commit

Permalink
feat(modif): add utils to find difference between arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbeau committed Aug 28, 2018
1 parent 81a7aff commit 65cb301
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
27 changes: 27 additions & 0 deletions projects/utils/src/lib/modif.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export enum ModifType {
ADDED = 'added',
DELETED = 'deleted',
MODIFIED = 'modified'
}

export interface Modif {
type: ModifType;
oldValue?: any;
newValue?: any;
keysChanged?: {
key: string;
newValue: any;
oldValue: any;
}[];
}

export interface ModifRegroupement {
added: ModifItem[];
deleted: ModifItem[];
modified: ModifItem[];
}

export interface ModifItem {
modif: Modif;
value: any;
}
113 changes: 113 additions & 0 deletions projects/utils/src/lib/modif.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { StringUtils } from './string-utils';
import { ModifRegroupement, ModifItem, ModifType } from './modif.interface';

export class ModifUtils {
static findModifs(
obj1: any[],
obj2: any[],
ignoreKeys: string[] = []
): ModifRegroupement {
const items: ModifRegroupement = {
added: [],
deleted: [],
modified: []
};

if (!obj1 || !obj2) {
return items;
}

const obj1Clone: any = [...obj1];
const obj2Clone: any = [...obj2];

for (const fromItem of obj1Clone) {
const index = obj2Clone.findIndex(s => s.id === fromItem.id);

if (index === -1) {
items.added.push({
modif: { type: ModifType.ADDED },
value: fromItem
});
continue;
}

const toItem = obj2Clone.splice(index, 1)[0];
const fromItemClone = Object.assign({}, fromItem);
const toItemClone = Object.assign({}, toItem);

const keysChanged = ModifUtils.compareObject(
fromItem,
toItem,
undefined,
ignoreKeys
);

if (keysChanged.length) {
items.modified.push({
modif: {
type: ModifType.MODIFIED,
keysChanged: keysChanged,
oldValue: fromItemClone,
newValue: toItemClone
},
value: fromItem
});
}
}

items.deleted = obj2Clone.map(itemDeleted => {
return {
modif: { type: ModifType.DELETED },
value: itemDeleted
};
});

return items;
}

private static compareObject(fromItem, toItem, baseKey?, ignoreKeys = []) {
const fromItemClone = Object.assign({}, fromItem);
const toItemClone = Object.assign({}, toItem);

const keys: any = new Set([
...Object.keys(fromItem),
...Object.keys(toItem)
]);
let keysChanged = [];
keys.forEach(key => {
const keyString = baseKey ? `${baseKey}.${key}` : key;
if (ignoreKeys.indexOf(keyString) !== -1) {
return;
}

if (Array.isArray(fromItem[key])) {
fromItem[key] = fromItem[key].join(',<br>');
}
if (Array.isArray(toItem[key])) {
toItem[key] = toItem[key].join(',<br>');
}

if (
typeof fromItem[key] === 'object' &&
typeof toItem[key] === 'object' &&
fromItem[key] !== null &&
toItem[key] !== null
) {
keysChanged = keysChanged.concat(
this.compareObject(fromItem[key], toItem[key], keyString)
);
} else {
if (fromItem[key] !== toItem[key]) {
keysChanged.push({
key: keyString,
oldValue: fromItemClone[key],
newValue: toItemClone[key]
});
fromItem[key] = StringUtils.diff(fromItem[key], toItem[key]);
}
}
});

return keysChanged;
}
}

0 comments on commit 65cb301

Please sign in to comment.