-
Notifications
You must be signed in to change notification settings - Fork 18
/
defaultHandlers.js
107 lines (96 loc) · 4.11 KB
/
defaultHandlers.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
import AsyncIteratorHandler from './AsyncIteratorHandler';
import { listHandler, containerHandler, collectionHandler } from './CollectionsHandler';
import DataHandler from './DataHandler';
import DeleteFunctionHandler from './DeleteFunctionHandler';
import ExecuteQueryHandler from './ExecuteQueryHandler';
import GetHandler from './GetFunctionHandler';
import InsertFunctionHandler from './InsertFunctionHandler';
import MutationExpressionsHandler from './MutationExpressionsHandler';
import PathExpressionHandler from './PathExpressionHandler';
import PredicateHandler from './PredicateHandler';
import PredicatesHandler from './PredicatesHandler';
import PreloadHandler from './PreloadHandler';
import PropertiesHandler from './PropertiesHandler';
import ReplaceFunctionHandler from './ReplaceFunctionHandler';
import SetFunctionHandler from './SetFunctionHandler';
import SortHandler from './SortHandler';
import SparqlHandler from './SparqlHandler';
import StringToLDflexHandler from './StringToLDflexHandler';
import SubjectHandler from './SubjectHandler';
import SubjectsHandler from './SubjectsHandler';
import ThenHandler from './ThenHandler';
import ToArrayHandler from './ToArrayHandler';
import { termToPrimitive } from './valueUtils';
import { handler } from './handlerUtil';
import { prefixHandler, namespaceHandler, fragmentHandler } from './URIHandler';
/**
* A map with default property handlers.
*/
export default {
// Flag to loaders that exported paths are not ES6 modules
__esModule: () => undefined,
// Add thenable and async iterable behavior
then: new ThenHandler(),
[Symbol.asyncIterator]: new AsyncIteratorHandler(),
// Add utilities for collections
list: listHandler(),
container: containerHandler(false),
containerAsSet: containerHandler(true),
collection: collectionHandler(),
// Add read and query functionality
get: new GetHandler(),
subject: new SubjectHandler(),
predicate: new PredicateHandler(),
properties: new PropertiesHandler(),
predicates: new PredicatesHandler(),
pathExpression: new PathExpressionHandler(),
sparql: new SparqlHandler(),
subjects: new SubjectsHandler(),
results: new ExecuteQueryHandler(),
sort: new SortHandler('ASC'),
sortDesc: new SortHandler('DESC'),
preload: new PreloadHandler(),
// Add write functionality
mutationExpressions: new MutationExpressionsHandler(),
add: new InsertFunctionHandler(),
set: new SetFunctionHandler(),
replace: new ReplaceFunctionHandler(),
delete: new DeleteFunctionHandler(),
// Add RDFJS term handling
termType: termPropertyHandler('termType'),
value: termPropertyHandler('value'),
datatype: termPropertyHandler('datatype'),
language: termPropertyHandler('language'),
canonical: termPropertyHandler('canonical'),
equals: DataHandler.sync('subject', 'equals'),
toString: DataHandler.syncFunction('subject', 'value'),
valueOf: subjectToPrimitiveHandler(),
toPrimitive: subjectToPrimitiveHandler(),
// URI / namedNode handling
prefix: prefixHandler,
namespace: namespaceHandler,
fragment: fragmentHandler,
// Add iteration helpers
toArray: new ToArrayHandler(),
termTypes: handler((_, path) => path.toArray(term => term.termType)),
values: handler((_, path) => path.toArray(term => term.value)),
datatypes: handler((_, path) => path.toArray(term => term.datatype)),
languages: handler((_, path) => path.toArray(term => term.language)),
// Parse a string into an LDflex object
resolve: new StringToLDflexHandler(),
};
// Creates a handler for the given RDF/JS Term property
function termPropertyHandler(property) {
// If a resolved subject is present,
// behave as an RDF/JS term and synchronously expose the property;
// otherwise, return a promise to the property value
return handler(({ subject }, path) =>
subject && (property in subject) ? subject[property] :
path.then && path.then(term => term?.[property]));
}
// Creates a handler that converts the subject into a primitive
function subjectToPrimitiveHandler() {
return handler(({ subject }) => () =>
typeof subject?.termType !== 'string' ?
undefined : termToPrimitive(subject));
}