-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy paththing.internal.ts
213 lines (202 loc) · 7.29 KB
/
thing.internal.ts
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// Copyright Inrupt Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
// Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
import type { Quad, Quad_Object } from "@rdfjs/types";
import {
isNamedNode,
isLiteral,
xmlSchemaTypes,
deserializeBoolean,
deserializeDatetime,
deserializeDecimal,
deserializeInteger,
} from "../datatypes";
import type { Thing, SolidDataset, WithChangeLog } from "../interfaces";
import { hasChangelog } from "../interfaces";
import { ThingExpectedError, isThing } from "./thing";
import { freeze } from "../rdf.internal";
/** @hidden For internal use only. */
export function internal_getReadableValue(value: Quad_Object): string {
if (isNamedNode(value)) {
return `<${value.value}> (URL)`;
}
if (isLiteral(value)) {
/* istanbul ignore if: thingAsMarkdown always instantiates a NamedNode, so we can't hit this code path in tests. */
if (!isNamedNode(value.datatype)) {
return `[${value.value}] (RDF/JS Literal of unknown type)`;
}
let val;
switch (value.datatype.value) {
case xmlSchemaTypes.boolean:
val =
deserializeBoolean(value.value)?.valueOf() ??
`Invalid data: \`${value.value}\``;
return `${val} (boolean)`;
case xmlSchemaTypes.dateTime:
val =
deserializeDatetime(value.value)?.toUTCString() ??
`Invalid data: \`${value.value}\``;
return `${val} (datetime)`;
case xmlSchemaTypes.decimal:
val =
deserializeDecimal(value.value)?.toString() ??
`Invalid data: \`${value.value}\``;
return `${val} (decimal)`;
case xmlSchemaTypes.integer:
val =
deserializeInteger(value.value)?.toString() ??
`Invalid data: \`${value.value}\``;
return `${val} (integer)`;
case xmlSchemaTypes.langString:
return `"${value.value}" (${value.language} string)`;
case xmlSchemaTypes.string:
return `"${value.value}" (string)`;
default:
return `[${value.value}] (RDF/JS Literal of type: \`${value.datatype.value}\`)`;
}
}
/* istanbul ignore else: thingAsMarkdown doesn't generate other Nodes, so we can't hit this path in tests. */
if (value.termType === "BlankNode") {
return `[${value.value}] (RDF/JS BlankNode)`;
}
/* istanbul ignore next: thingAsMarkdown doesn't generate Quad Nodes, so we can't hit this path in tests. */
if (value.termType === "Quad") {
return `??? (nested RDF* Quad)`;
}
/* istanbul ignore else: The if statements are exhaustive; if not, TypeScript will complain. */
/* istanbul ignore next: thingAsMarkdown doesn't generate Variable Nodes, so we can't hit this path in tests. */
if (value.termType === "Variable") {
return `?${value.value} (RDF/JS Variable)`;
}
/* istanbul ignore next: The if statements are exhaustive; if not, TypeScript will complain. */
return value;
}
/**
* @hidden
*/
export function internal_throwIfNotThing(thing: Thing): void {
if (!isThing(thing)) {
throw new ThingExpectedError(thing);
}
}
/**
* @hidden
* @param solidDataset
*/
export function internal_addAdditionsToChangeLog<Dataset extends SolidDataset>(
solidDataset: Dataset,
additions: Quad[],
): Dataset & WithChangeLog {
const changeLog = hasChangelog(solidDataset)
? solidDataset.internal_changeLog
: /* istanbul ignore next: This function always gets called after addDeletionsToChangeLog, so the ChangeLog always already exists in tests: */
{ additions: [], deletions: [] };
const [newAdditions, newDeletions] = additions
.filter((addition) => !containsBlankNode(addition))
.reduce(
([additionsAcc, deletionsAcc], addition) => {
const existingDeletion = deletionsAcc.find((deletion) =>
deletion.equals(addition),
);
if (typeof existingDeletion !== "undefined") {
return [
additionsAcc,
deletionsAcc.filter((deletion) => !deletion.equals(addition)),
];
}
return [additionsAcc.concat(addition), deletionsAcc];
},
[changeLog.additions, changeLog.deletions],
);
return freeze({
...solidDataset,
internal_changeLog: {
additions: newAdditions,
deletions: newDeletions,
},
});
}
/**
* @hidden
* @param solidDataset
*/
export function internal_addDeletionsToChangeLog<Dataset extends SolidDataset>(
solidDataset: Dataset,
deletions: Quad[],
): Dataset & WithChangeLog {
const changeLog = hasChangelog(solidDataset)
? solidDataset.internal_changeLog
: { additions: [], deletions: [] };
const [newAdditions, newDeletions] = deletions
.filter((deletion) => !containsBlankNode(deletion))
.reduce(
([additionsAcc, deletionsAcc], deletion) => {
const existingAddition = additionsAcc.find((addition) =>
addition.equals(deletion),
);
if (typeof existingAddition !== "undefined") {
return [
additionsAcc.filter((addition) => !addition.equals(deletion)),
deletionsAcc,
];
}
return [additionsAcc, deletionsAcc.concat(deletion)];
},
[changeLog.additions, changeLog.deletions],
);
return freeze({
...solidDataset,
internal_changeLog: {
additions: newAdditions,
deletions: newDeletions,
},
});
}
/**
* Enforces the presence of a Changelog for a given dataset. If a changelog is
* already present, it is unchanged. Otherwise, an empty changelog is created.
* @hidden
* @param solidDataset
*/
export function internal_withChangeLog<Dataset extends SolidDataset>(
solidDataset: Dataset,
): Dataset & WithChangeLog {
const newSolidDataset: Dataset & WithChangeLog = hasChangelog(solidDataset)
? solidDataset
: freeze({
...solidDataset,
internal_changeLog: { additions: [], deletions: [] },
});
return newSolidDataset;
}
/**
* We don't currently support reading and writing Blank Nodes, so this function can be used to skip those Quads.
*
* This is needed because we cannot reconcile Blank Nodes in additions and
* deletions. Down the road, we should do a diff before saving a SolidDataset
* against a saved copy of the originally-fetched one, based on our own data
* structures, which should make it easier to reconcile.
*/
function containsBlankNode(quad: Quad): boolean {
return (
quad.subject.termType === "BlankNode" ||
quad.object.termType === "BlankNode"
);
}