forked from yndx-shri-reviewer/shri-2020-task-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
methods.js
78 lines (66 loc) · 1.63 KB
/
methods.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
export const getMod = (mods, modName) => {
if (mods) {
return mods.value.children.find(node => node.key.value === modName)
}
}
export const copy = (array) => JSON.parse(JSON.stringify(array))
export const createLog = ({ nodeName, loc, mods, parent }) => {
if (!nodeName) {
throw new Error('HAS_NO_NODE_NAME')
}
return {
data: {},
errors: [],
nodeName,
mods,
parent,
location: {
start: {
column: loc.start.column,
line: loc.start.line
},
end: {
column: loc.end.column,
line: loc.end.line
}
}
}
}
export const hasLog = (logs, location) => {
return logs.find(log => {
return log.location && location && deepEqual(log.location, location)
})
}
const deepEqual = (obj1, obj2) => {
if (obj1 === obj2) return true
if (isPrimitive(obj1) && isPrimitive(obj2)) return obj1 === obj2
if (Object.keys(obj1).length !== Object.keys(obj2).length) return false
for (const key in obj1) {
if (!(key in obj2)) return false
if (!deepEqual(obj1[key], obj2[key])) return false
}
return true
}
function isPrimitive (obj) {
return (obj !== Object(obj))
}
export const mergeData = (log, newData) => {
const data = { ...log.data, ...newData }
return { ...log, data }
}
export const mergeError = (log, error) => {
const errors = [...log.errors, error]
return { ...log, errors }
}
export const getLoc = node => {
return {
start: {
column: node.loc && node.loc.start.column,
line: node.loc && node.loc.start.line
},
end: {
column: node.loc && node.loc.end.column,
line: node.loc && node.loc.end.line
}
}
}