Skip to content

Commit 2b3240e

Browse files
committed
Move keword regex to util.js.
1 parent 2ea4613 commit 2b3240e

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

lib/context.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ const {
2525

2626
const {
2727
REGEX_BCP47,
28+
REGEX_KEYWORD,
2829
asArray: _asArray,
2930
compareShortestLeast: _compareShortestLeast
3031
} = require('./util');
3132

3233
const INITIAL_CONTEXT_CACHE = new Map();
3334
const INITIAL_CONTEXT_CACHE_MAX_SIZE = 10000;
34-
const KEYWORD_PATTERN = /^@[a-zA-Z]+$/;
3535

3636
const api = {};
3737
module.exports = api;
@@ -492,7 +492,7 @@ api.createTermDefinition = ({
492492
'Invalid JSON-LD syntax; keywords cannot be overridden.',
493493
'jsonld.SyntaxError',
494494
{code: 'keyword redefinition', context: localCtx, term});
495-
} else if(term.match(KEYWORD_PATTERN)) {
495+
} else if(term.match(REGEX_KEYWORD)) {
496496
if(options.eventHandler) {
497497
_handleEvent({
498498
event: {
@@ -587,7 +587,7 @@ api.createTermDefinition = ({
587587
'jsonld.SyntaxError', {code: 'invalid IRI mapping', context: localCtx});
588588
}
589589

590-
if(reverse.match(KEYWORD_PATTERN)) {
590+
if(reverse.match(REGEX_KEYWORD)) {
591591
if(options.eventHandler) {
592592
_handleEvent({
593593
event: {
@@ -636,7 +636,7 @@ api.createTermDefinition = ({
636636
if(id === null) {
637637
// reserve a null term, which may be protected
638638
mapping['@id'] = null;
639-
} else if(!api.isKeyword(id) && id.match(KEYWORD_PATTERN)) {
639+
} else if(!api.isKeyword(id) && id.match(REGEX_KEYWORD)) {
640640
if(options.eventHandler) {
641641
_handleEvent({
642642
event: {
@@ -1019,7 +1019,7 @@ function _expandIri(activeCtx, value, relativeTo, localCtx, defined, options) {
10191019
}
10201020

10211021
// ignore non-keyword things that look like a keyword
1022-
if(value.match(KEYWORD_PATTERN)) {
1022+
if(value.match(REGEX_KEYWORD)) {
10231023
return null;
10241024
}
10251025

lib/util.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const REGEX_LINK_HEADERS = /(?:<[^>]*?>|"[^"]*?"|[^,])+/g;
1515
const REGEX_LINK_HEADER = /\s*<([^>]*?)>\s*(?:;\s*(.*))?/;
1616
const REGEX_LINK_HEADER_PARAMS =
1717
/(.*?)=(?:(?:"([^"]*?)")|([^"]*?))\s*(?:(?:;\s*)|$)/g;
18+
const REGEX_KEYWORD = /^@[a-zA-Z]+$/;
1819

1920
const DEFAULTS = {
2021
headers: {
@@ -26,6 +27,7 @@ const api = {};
2627
module.exports = api;
2728
api.IdentifierIssuer = IdentifierIssuer;
2829
api.REGEX_BCP47 = REGEX_BCP47;
30+
api.REGEX_KEYWORD = REGEX_KEYWORD;
2931

3032
/**
3133
* Clones an object, array, Map, Set, or string/number. If a typed JavaScript

0 commit comments

Comments
 (0)