-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprototype-ld.js
126 lines (99 loc) · 2.93 KB
/
prototype-ld.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'use-strict'
const PLD = {}
// items, mapped by id
PLD.allItems = {}
PLD.mapClassOnType = {}
PLD.getItem = (item) => {
let attrs = {}
if (typeof item === 'string') { // it's an id !
attrs = PLD.allItems[item]
} else {
attrs = PLD.allItems[item['@id']]
}
if (!attrs) { console.warn(`item ${ item } is missing`) }
let type = attrs['@type']
if (type instanceof Array) {
for (type of attrs['@type']) {
if (PLD.mapClassOnType[type]) break
}
}
if (PLD.mapClassOnType[type]) { return new PLD.mapClassOnType[type](attrs) }
return attrs
}
PLD.isType = (item, type) => {
if (!(item && item['@type'])) { return false }
const typeProp = item['@type']
return typeProp === type || (typeProp instanceof Array && typeProp.indexOf(type) !== -1)
}
PLD.mapOnObject = (objects, fun) => {
if (objects instanceof Array) {
return objects.map(fun)
}
return fun(objects)
}
PLD.testOnObject = (objects, test) => {
if (objects && objects instanceof Array) {
return objects.some(test);
} else {
return test(objects);
}
}
PLD.forEachOnTreeOfPredicates = (fun , item, props) => {
item = PLD.getItem(item)
fun(item)
props.forEach((prop) => {
if (item[prop]) {
item[prop] = PLD.mapOnObject(item[prop], (value) => PLD.forEachOnTreeOfPredicates(fun, value, props))
}
})
return item
}
PLD.fillTreeForPredicates = (item, props) => {
return PLD.forEachOnTreeOfPredicates(i => i, item, props)
}
PLD.idList2ItemMap = (ids) => {
return ids.reduce((agg, id) => {
agg[id] = PLD.allItems[id]
return agg
}, {})
}
PLD.mapByPredicate = (prop, items) => {
return items.reduce((agg, id) => {
try {
const item = PLD.getItem(id, PLD.allItems)
agg[item[prop]] = item
return agg
} catch (e) {
console.error(`semantic_utils : Error in map by prop on id: ${id}`, e)
throw e
}
}, {})
}
PLD.where = (queries, items) => {
items = items || Object.keys(PLD.allItems).map(id => PLD.allItems[id])
return items.filter(item =>
Object.keys(queries).every(predicate => item[predicate] == queries[predicate])
)
}
PLD.isSubclassOf = (item, classId) => {
try {
item = PLD.getItem(item)
} catch(e) {
console.info(`Can't find item: ${item} locally, abort`)
// or fallback to 'schema:Thing' ?
return false
}
// if (PLD.isType(item, 'schema:Thing')) return false
if (!PLD.isType(item, classId)) {
const res = PLD.mapOnObject(item['@type'], upperClass => PLD.isSubclassOf(upperClass, classId))
return res === true || res instanceof Array && res.some(v => v)
}
return true
}
PLD.listInstanceOf = (classId, items) => {
items = items || Object.keys(PLD.allItems).map(id => PLD.allItems[id])
return items.filter((item) => PLD.isSubclassOf(item, classId))
}
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = PLD :
// typeof define === 'function' && define.aPLDd ? define(factory) :
this.PLD = PLD // put on window.