-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcommon.js
More file actions
60 lines (48 loc) · 1.98 KB
/
Copy pathcommon.js
File metadata and controls
60 lines (48 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const _ = require('lodash');
const { stripParentheses } = require('../generalHelper');
const getDifferentItems = (newItems = [], oldItems = []) => {
const intersection = _.intersectionWith(newItems, oldItems, _.isEqual);
return {
add: _.xorWith(newItems, intersection, _.isEqual),
drop: _.xorWith(oldItems, intersection, _.isEqual),
};
};
const hydrateTableProperties = ({ new: newItems, old: oldItems }, name, commentState) => {
const hydrateProperties = properties => (properties || '').split(',').map(prop => prop.trim());
const prepareProperties = properties => properties.filter(Boolean).join(', ');
const isCommentChanged = !_.isEqual(commentState?.new, commentState?.old);
const addCommentProp = isCommentChanged && commentState?.new ? `'comment'='${commentState?.new}'` : '';
const dropCommentProp = isCommentChanged && !commentState?.new ? `'comment'` : '';
const preparePropertiesName = properties =>
properties
.filter(Boolean)
.map(prop => `${prop.split('=')[0]}`)
.join(', ');
const newHydrateItems = hydrateProperties(stripParentheses(newItems));
const oldHydrateItems = hydrateProperties(stripParentheses(oldItems));
const { add, drop } = getDifferentItems(newHydrateItems, oldHydrateItems);
const dataProperties = {
add: prepareProperties([...add, addCommentProp]),
drop: preparePropertiesName([...drop, dropCommentProp]),
};
return { dataProperties, name };
};
const compareProperties = ({ new: newProperty, old: oldProperty }) => {
if (!newProperty && !oldProperty) {
return;
}
return !_.isEqual(newProperty, oldProperty);
};
const getIsChangeProperties = (compMod, properties) =>
properties.some(property => compareProperties(compMod[property] || {}));
const getItems = (entity, nameProperty, modify) =>
[]
.concat(entity.properties?.[nameProperty]?.properties?.[modify]?.items)
.filter(Boolean)
.map(items => Object.values(items.properties)[0]);
module.exports = {
hydrateTableProperties,
getDifferentItems,
getIsChangeProperties,
getItems,
};