-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathgraphTypes.js
121 lines (112 loc) · 3.3 KB
/
graphTypes.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
/*!
* Copyright (c) 2017-2023 Digital Bazaar, Inc. All rights reserved.
*/
'use strict';
const types = require('./types');
const api = {};
module.exports = api;
/**
* Returns true if the given value is a subject with properties.
*
* @param v the value to check.
*
* @return true if the value is a subject with properties, false if not.
*/
api.isSubject = v => {
// Note: A value is a subject if all of these hold true:
// 1. It is an Object.
// 2. It is not a @value, @set, or @list.
// 3. It has more than 1 key OR any existing key is not @id.
if(types.isObject(v) &&
!(('@value' in v) || ('@set' in v) || ('@list' in v))) {
const keyCount = Object.keys(v).length;
return (keyCount > 1 || !('@id' in v));
}
return false;
};
/**
* Returns true if the given value is a subject reference.
*
* @param v the value to check.
*
* @return true if the value is a subject reference, false if not.
*/
api.isSubjectReference = v =>
// Note: A value is a subject reference if all of these hold true:
// 1. It is an Object.
// 2. It has a single key: @id.
(types.isObject(v) && Object.keys(v).length === 1 && ('@id' in v));
/**
* Returns true if the given value is a @value.
*
* @param v the value to check.
*
* @return true if the value is a @value, false if not.
*/
api.isValue = v =>
// Note: A value is a @value if all of these hold true:
// 1. It is an Object.
// 2. It has the @value property.
types.isObject(v) && v['@value'] !== undefined;
/**
* Returns true if the given value is a @list.
*
* @param v the value to check.
*
* @return true if the value is a @list, false if not.
*/
api.isList = v =>
// Note: A value is a @list if all of these hold true:
// 1. It is an Object.
// 2. It has the @list property.
types.isObject(v) && v['@list'] !== undefined;
/**
* Returns true if the given value is a @graph.
*
* @return true if the value is a @graph, false if not.
*/
api.isGraph = v => {
// Note: A value is a graph if all of these hold true:
// 1. It is an object.
// 2. It has an `@graph` key.
// 3. It may have '@id' or '@index'
return types.isObject(v) &&
v['@graph'] !== undefined &&
Object.keys(v)
.filter(key => key !== '@id' && key !== '@index').length === 1;
};
/**
* Returns true if the given value is a simple @graph.
*
* @return true if the value is a simple @graph, false if not.
*/
api.isSimpleGraph = v => {
// Note: A value is a simple graph if all of these hold true:
// 1. It is an object.
// 2. It has an `@graph` key.
// 3. It has only 1 key or 2 keys where one of them is `@index`.
return api.isGraph(v) && v['@id'] === undefined;
};
/**
* Returns true if the given value is a blank node.
*
* @param v the value to check.
*
* @return true if the value is a blank node, false if not.
*/
api.isBlankNode = v => {
// Note: A value is a blank node if all of these hold true:
// 1. It is an Object.
// 2. If it has an @id key that is not a string OR begins with '_:'.
// 3. It has no keys OR is not a @value, @set, or @list.
if(types.isObject(v)) {
const id = v['@id'];
if(id !== undefined) {
return !types.isString(id) || id.indexOf('_:') === 0;
}
return (v['@value'] === undefined &&
v['@set'] === undefined &&
v['@list'] === undefined) || Object.keys(v).length === 0;
}
return false;
};