Skip to content

Commit 6412127

Browse files
davidlehndlongley
authored andcommitted
Fix clearing with @context:null.
- Use `undefined` vs `null` for getContextValue(ctx, key, '@context').
1 parent 5fc1f00 commit 6412127

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

lib/compact.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const JsonLdError = require('./JsonLdError');
88
const {
99
isArray: _isArray,
1010
isObject: _isObject,
11-
isString: _isString
11+
isString: _isString,
12+
isUndefined: _isUndefined
1213
} = require('./types');
1314

1415
const {
@@ -100,7 +101,7 @@ api.compact = ({
100101

101102
// use any scoped context on activeProperty
102103
const ctx = _getContextValue(activeCtx, activeProperty, '@context');
103-
if(ctx) {
104+
if(!_isUndefined(ctx)) {
104105
activeCtx = _processContext({activeCtx, localCtx: ctx, options});
105106
}
106107

@@ -156,7 +157,7 @@ api.compact = ({
156157

157158
// Use any scoped context defined on this value
158159
const ctx = _getContextValue(activeCtx, compactedType, '@context');
159-
if(ctx) {
160+
if(!_isUndefined(ctx)) {
160161
activeCtx = _processContext({activeCtx, localCtx: ctx, options});
161162
}
162163
}

lib/context.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -874,18 +874,21 @@ api.getInitialContext = options => {
874874

875875
/**
876876
* Gets the value for the given active context key and type, null if none is
877-
* set.
877+
* set or undefined if none is set and type is '@context'.
878878
*
879879
* @param ctx the active context.
880880
* @param key the context key.
881881
* @param [type] the type of value to get (eg: '@id', '@type'), if not
882882
* specified gets the entire entry for a key, null if not found.
883883
*
884-
* @return the value.
884+
* @return the value, null, or undefined.
885885
*/
886886
api.getContextValue = (ctx, key, type) => {
887-
// return null for invalid key
887+
// invalid key
888888
if(key === null) {
889+
if(type === '@context') {
890+
return undefined;
891+
}
889892
return null;
890893
}
891894

@@ -908,6 +911,9 @@ api.getContextValue = (ctx, key, type) => {
908911
return ctx[type];
909912
}
910913

914+
if(type === '@context') {
915+
return undefined;
916+
}
911917
return null;
912918
};
913919

lib/expand.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const {
99
isArray: _isArray,
1010
isObject: _isObject,
1111
isEmptyObject: _isEmptyObject,
12-
isString: _isString
12+
isString: _isString,
13+
isUndefined: _isUndefined
1314
} = require('./types');
1415

1516
const {
@@ -165,7 +166,7 @@ api.expand = ({
165166
(value.length > 1 ? value.slice().sort() : value) : [value];
166167
for(const type of types) {
167168
const ctx = _getContextValue(activeCtx, type, '@context');
168-
if(ctx) {
169+
if(!_isUndefined(ctx)) {
169170
activeCtx = _processContext({activeCtx, localCtx: ctx, options});
170171
}
171172
}
@@ -555,7 +556,7 @@ function _expandObject({
555556
// use potential scoped context for key
556557
let termCtx = activeCtx;
557558
const ctx = _getContextValue(activeCtx, key, '@context');
558-
if(ctx) {
559+
if(!_isUndefined(ctx)) {
559560
termCtx = _processContext({
560561
activeCtx, localCtx: ctx, options, propertyTermDefinition: true
561562
});
@@ -832,7 +833,7 @@ function _expandIndexMap(
832833
for(let key of keys) {
833834
// if indexKey is @type, there may be a context defined for it
834835
const ctx = _getContextValue(activeCtx, key, '@context');
835-
if(ctx) {
836+
if(!_isUndefined(ctx)) {
836837
activeCtx = _processContext({activeCtx, localCtx: ctx, options});
837838
}
838839

0 commit comments

Comments
 (0)