-
Notifications
You must be signed in to change notification settings - Fork 3
/
extension.js
42 lines (39 loc) · 1.16 KB
/
extension.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
function getExtension(obj, type, namespace='org.cubingusa.natshelper.v1') {
type = namespace + '.' + type
if (!obj.extensions) {
obj.extensions = []
}
var matching = obj.extensions.filter((ext) => ext.id == type)
if (matching.length > 0) {
return matching[0].data
}
return null
}
function getOrInsertExtension(obj, type, namespace='org.cubingusa.natshelper.v1') {
type = namespace + '.' + type
if (!obj.extensions) {
obj.extensions = []
}
var matching = obj.extensions.filter((ext) => ext.id == type)
if (matching.length > 0) {
return matching[0].data
}
var extension = {
id: type,
specUrl: 'https://github.com/cubingusa/natshelper/blob/main/specification.md',
data: {}
}
obj.extensions.push(extension)
return extension.data
}
function getExtensionsWithPrefix(obj, type, prefix, namespace='org.cubingusa.natshelper.v1') {
if (!obj.extensions) {
obj.extensions = []
}
return obj.extensions.filter((ext) => ext.id.startsWith(namespace + '.' + type + '.' + prefix))
}
module.exports = {
getExtension: getExtension,
getOrInsertExtension: getOrInsertExtension,
getExtensionsWithPrefix: getExtensionsWithPrefix,
}