Skip to content

Commit 05ce5aa

Browse files
gkelloggdavidlehn
authored andcommitted
Changed framing defaults and handle omitGraph.
- Changed framing defaults - `embed` to "@once" and warn on "@FIRST" or "@last". - `pruneBlankNodeIdentifiers` based on processingMode. - `omitGraph` based on processingMode. - Top level `@graph` omitted if `omitGraph` is `true`.
1 parent 8589e30 commit 05ce5aa

File tree

4 files changed

+42
-34
lines changed

4 files changed

+42
-34
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
- **NOTE**: `LINK_HEADER_REL` in `lib/constants.js` has been deprecated and
2323
renamed to `LINK_HEADER_CONTEXT`. It remains for now but will be removed in a
2424
future release.
25+
- Changed framing defaults
26+
- `embed` to "@once" and warn on "@first" or "@last".
27+
- `pruneBlankNodeIdentifiers` based on processingMode.
28+
- `omitGraph` based on processingMode.
2529

2630
### Added
2731
- Support for `"@import"`.
@@ -30,6 +34,7 @@
3034
- Support for expansion and compaction of values container `"@direction"`.
3135
- Support for RDF transformation of `@direction` when `rdfDirection` is
3236
'i18n-datatype'.
37+
- Top level `@graph` omitted if `omitGraph` is `true`.
3338

3439
## 2.0.2 - 2020-01-17
3540

lib/jsonld.js

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ const {
6262
} = require('./graphTypes');
6363

6464
const {
65+
expandIri: _expandIri,
6566
getInitialContext: _getInitialContext,
66-
process: _processContext
67+
process: _processContext,
68+
processingMode: _processingMode
6769
} = require('./context');
6870

6971
const {
@@ -223,6 +225,12 @@ jsonld.compact = async function(input, ctx, options) {
223225
ctx = ctx[0];
224226
}
225227

228+
if(options.framing) {
229+
// remove @preserve from results
230+
options.link = {};
231+
compacted = _removePreserve(activeCtx, compacted, options);
232+
}
233+
226234
// add context and/or @graph
227235
if(_isArray(compacted)) {
228236
// use '@graph' keyword
@@ -244,16 +252,6 @@ jsonld.compact = async function(input, ctx, options) {
244252
}
245253
}
246254

247-
if(options.framing) {
248-
// get graph alias
249-
const graph = _compactIri({
250-
activeCtx, iri: '@graph', relativeTo: {vocab: true}
251-
});
252-
// remove @preserve from results
253-
options.link = {};
254-
compacted[graph] = _removePreserve(activeCtx, compacted[graph], options);
255-
}
256-
257255
return compacted;
258256
};
259257

@@ -435,11 +433,10 @@ jsonld.frame = async function(input, frame, options) {
435433
// set default options
436434
options = _setDefaults(options, {
437435
base: _isString(input) ? input : '',
438-
embed: '@last',
436+
embed: '@once',
439437
explicit: false,
440-
requireAll: true,
438+
requireAll: false,
441439
omitDefault: false,
442-
pruneBlankNodeIdentifiers: true,
443440
bnodesToClear: [],
444441
contextResolver: new ContextResolver(
445442
{sharedCache: _resolvedContextCache})
@@ -467,6 +464,24 @@ jsonld.frame = async function(input, frame, options) {
467464

468465
const frameContext = frame ? frame['@context'] || {} : {};
469466

467+
// process context
468+
const activeCtx = await jsonld.processContext(
469+
_getInitialContext(options), frameContext, options);
470+
471+
// mode specific defaults
472+
if(!options.hasOwnProperty('omitGraph')) {
473+
options.omitGraph = _processingMode(activeCtx, 1.1);
474+
}
475+
if(!options.hasOwnProperty('pruneBlankNodeIdentifiers')) {
476+
options.pruneBlankNodeIdentifiers = _processingMode(activeCtx, 1.1);
477+
}
478+
if((options.embed === '@first' || options.embed === '@last') &&
479+
_processingMode(activeCtx, 1.1))
480+
{
481+
console.warn(`WARNING: ${options.embed} is not a valid value ` +
482+
`of @embed in 1.1 mode.`);
483+
}
484+
470485
// expand input
471486
const expanded = await jsonld.expand(input, options);
472487

@@ -478,14 +493,13 @@ jsonld.frame = async function(input, frame, options) {
478493

479494
// if the unexpanded frame includes a key expanding to @graph, frame the
480495
// default graph, otherwise, the merged graph
481-
// FIXME should look for aliases of @graph
482-
opts.merged = !('@graph' in frame);
496+
const frameKeys = Object.keys(frame)
497+
.map(key => _expandIri(activeCtx, key, {vocab: true}));
498+
opts.merged = !frameKeys.includes('@graph');
483499
// do framing
484500
const framed = _frameMergedOrDefault(expanded, expandedFrame, opts);
485501

486-
// compact result (force @graph option to true, skip expansion,
487-
// check for linked embeds)
488-
opts.graph = true;
502+
opts.graph = !options.omitGraph;
489503
opts.skipExpansion = true;
490504
opts.link = {};
491505
opts.framing = true;

tests/misc.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ describe('link tests', () => {
2222
p.catch(e => {
2323
assert.ifError(e);
2424
}).then(output => {
25-
output = output['@graph'][0];
2625
assert.equal(output, output['a:foo']);
2726
done();
2827
});
@@ -453,13 +452,9 @@ describe('js keywords', () => {
453452
"@context": {
454453
"@vocab": "http://example.org/"
455454
},
456-
"@graph": [
457-
{
458-
"toString": {
459-
"valueOf": "thing"
460-
}
461-
}
462-
]
455+
"toString": {
456+
"valueOf": "thing"
457+
}
463458
}
464459
;
465460
const e = await jsonld.frame(d, frame);

tests/test-common.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,6 @@ const TEST_TYPES = {
126126
specVersion: ['json-ld-1.0'],
127127
// FIXME
128128
idRegex: [
129-
// ex
130-
/frame-manifest.jsonld#tg001$/,
131129
// graphs
132130
/frame-manifest.jsonld#t0011$/,
133131
/frame-manifest.jsonld#t0010$/,
@@ -177,12 +175,8 @@ const TEST_TYPES = {
177175
// lists
178176
/frame-manifest.jsonld#t0055$/,
179177
/frame-manifest.jsonld#t0058$/,
180-
// misc
181-
/frame-manifest.jsonld#tp010$/,
182-
/frame-manifest.jsonld#tp050$/,
183-
/frame-manifest.jsonld#teo01$/,
178+
// @preserve and @container: @set
184179
/frame-manifest.jsonld#t0062$/,
185-
/frame-manifest.jsonld#t0063$/,
186180
// @embed:@first
187181
/frame-manifest.jsonld#t0060$/,
188182
// requireAll

0 commit comments

Comments
 (0)