n3 1.26.0 → 2.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/BaseIRI.js DELETED
@@ -1,93 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- var _Util = require("./Util");
8
- // Do not handle base IRIs without scheme, and currently unsupported cases:
9
- // - file: IRIs (which could also use backslashes)
10
- // - IRIs containing /. or /.. or //
11
- const BASE_UNSUPPORTED = /^:?[^:?#]*(?:[?#]|$)|^file:|^[^:]*:\/*[^?#]+?\/(?:\.\.?(?:\/|$)|\/)/i;
12
- const SUFFIX_SUPPORTED = /^(?:(?:[^/?#]{3,}|\.?[^/?#.]\.?)(?:\/[^/?#]{3,}|\.?[^/?#.]\.?)*\/?)?(?:[?#]|$)/;
13
- const CURRENT = './';
14
- const PARENT = '../';
15
- const QUERY = '?';
16
- const FRAGMENT = '#';
17
- class BaseIRI {
18
- constructor(base) {
19
- this.base = base;
20
- this._baseLength = 0;
21
- this._baseMatcher = null;
22
- this._pathReplacements = new Array(base.length + 1);
23
- }
24
- static supports(base) {
25
- return !BASE_UNSUPPORTED.test(base);
26
- }
27
- _getBaseMatcher() {
28
- if (this._baseMatcher) return this._baseMatcher;
29
- if (!BaseIRI.supports(this.base)) return this._baseMatcher = /.^/;
30
-
31
- // Extract the scheme
32
- const scheme = /^[^:]*:\/*/.exec(this.base)[0];
33
- const regexHead = ['^', (0, _Util.escapeRegex)(scheme)];
34
- const regexTail = [];
35
-
36
- // Generate a regex for every path segment
37
- const segments = [],
38
- segmenter = /[^/?#]*([/?#])/y;
39
- let segment,
40
- query = 0,
41
- fragment = 0,
42
- last = segmenter.lastIndex = scheme.length;
43
- while (!query && !fragment && (segment = segmenter.exec(this.base))) {
44
- // Truncate base resolution path at fragment start
45
- if (segment[1] === FRAGMENT) fragment = segmenter.lastIndex - 1;else {
46
- // Create regex that matches the segment
47
- regexHead.push((0, _Util.escapeRegex)(segment[0]), '(?:');
48
- regexTail.push(')?');
49
-
50
- // Create dedicated query string replacement
51
- if (segment[1] !== QUERY) segments.push(last = segmenter.lastIndex);else {
52
- query = last = segmenter.lastIndex;
53
- fragment = this.base.indexOf(FRAGMENT, query);
54
- this._pathReplacements[query] = QUERY;
55
- }
56
- }
57
- }
58
-
59
- // Precalculate parent path substitutions
60
- for (let i = 0; i < segments.length; i++) this._pathReplacements[segments[i]] = PARENT.repeat(segments.length - i - 1);
61
- this._pathReplacements[segments[segments.length - 1]] = CURRENT;
62
-
63
- // Add the remainder of the base IRI (without fragment) to the regex
64
- this._baseLength = fragment > 0 ? fragment : this.base.length;
65
- regexHead.push((0, _Util.escapeRegex)(this.base.substring(last, this._baseLength)), query ? '(?:#|$)' : '(?:[?#]|$)');
66
- return this._baseMatcher = new RegExp([...regexHead, ...regexTail].join(''));
67
- }
68
- toRelative(iri) {
69
- // Unsupported or non-matching base IRI
70
- const match = this._getBaseMatcher().exec(iri);
71
- if (!match) return iri;
72
-
73
- // Exact base IRI match
74
- const length = match[0].length;
75
- if (length === this._baseLength && length === iri.length) return '';
76
-
77
- // Parent path match
78
- const parentPath = this._pathReplacements[length];
79
- if (parentPath) {
80
- const suffix = iri.substring(length);
81
- // Don't abbreviate unsupported path
82
- if (parentPath !== QUERY && !SUFFIX_SUPPORTED.test(suffix)) return iri;
83
- // Omit ./ with fragment or query string
84
- if (parentPath === CURRENT && /^[^?#]/.test(suffix)) return suffix;
85
- // Append suffix to relative parent path
86
- return parentPath + suffix;
87
- }
88
-
89
- // Fragment or query string, so include delimiter
90
- return iri.substring(length - 1);
91
- }
92
- }
93
- exports.default = BaseIRI;
package/lib/Util.js DELETED
@@ -1,9 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.escapeRegex = escapeRegex;
7
- function escapeRegex(regex) {
8
- return regex.replace(/[\]\/\(\)\*\+\?\.\\\$]/g, '\\$&');
9
- }
package/src/BaseIRI.js DELETED
@@ -1,101 +0,0 @@
1
- import { escapeRegex } from './Util';
2
-
3
- // Do not handle base IRIs without scheme, and currently unsupported cases:
4
- // - file: IRIs (which could also use backslashes)
5
- // - IRIs containing /. or /.. or //
6
- const BASE_UNSUPPORTED = /^:?[^:?#]*(?:[?#]|$)|^file:|^[^:]*:\/*[^?#]+?\/(?:\.\.?(?:\/|$)|\/)/i;
7
- const SUFFIX_SUPPORTED = /^(?:(?:[^/?#]{3,}|\.?[^/?#.]\.?)(?:\/[^/?#]{3,}|\.?[^/?#.]\.?)*\/?)?(?:[?#]|$)/;
8
- const CURRENT = './';
9
- const PARENT = '../';
10
- const QUERY = '?';
11
- const FRAGMENT = '#';
12
-
13
- export default class BaseIRI {
14
- constructor(base) {
15
- this.base = base;
16
- this._baseLength = 0;
17
- this._baseMatcher = null;
18
- this._pathReplacements = new Array(base.length + 1);
19
- }
20
-
21
- static supports(base) {
22
- return !BASE_UNSUPPORTED.test(base);
23
- }
24
-
25
- _getBaseMatcher() {
26
- if (this._baseMatcher)
27
- return this._baseMatcher;
28
- if (!BaseIRI.supports(this.base))
29
- return this._baseMatcher = /.^/;
30
-
31
- // Extract the scheme
32
- const scheme = /^[^:]*:\/*/.exec(this.base)[0];
33
- const regexHead = ['^', escapeRegex(scheme)];
34
- const regexTail = [];
35
-
36
- // Generate a regex for every path segment
37
- const segments = [], segmenter = /[^/?#]*([/?#])/y;
38
- let segment, query = 0, fragment = 0, last = segmenter.lastIndex = scheme.length;
39
- while (!query && !fragment && (segment = segmenter.exec(this.base))) {
40
- // Truncate base resolution path at fragment start
41
- if (segment[1] === FRAGMENT)
42
- fragment = segmenter.lastIndex - 1;
43
- else {
44
- // Create regex that matches the segment
45
- regexHead.push(escapeRegex(segment[0]), '(?:');
46
- regexTail.push(')?');
47
-
48
- // Create dedicated query string replacement
49
- if (segment[1] !== QUERY)
50
- segments.push(last = segmenter.lastIndex);
51
- else {
52
- query = last = segmenter.lastIndex;
53
- fragment = this.base.indexOf(FRAGMENT, query);
54
- this._pathReplacements[query] = QUERY;
55
- }
56
- }
57
- }
58
-
59
- // Precalculate parent path substitutions
60
- for (let i = 0; i < segments.length; i++)
61
- this._pathReplacements[segments[i]] = PARENT.repeat(segments.length - i - 1);
62
- this._pathReplacements[segments[segments.length - 1]] = CURRENT;
63
-
64
- // Add the remainder of the base IRI (without fragment) to the regex
65
- this._baseLength = fragment > 0 ? fragment : this.base.length;
66
- regexHead.push(
67
- escapeRegex(this.base.substring(last, this._baseLength)),
68
- query ? '(?:#|$)' : '(?:[?#]|$)',
69
- );
70
- return this._baseMatcher = new RegExp([...regexHead, ...regexTail].join(''));
71
- }
72
-
73
- toRelative(iri) {
74
- // Unsupported or non-matching base IRI
75
- const match = this._getBaseMatcher().exec(iri);
76
- if (!match)
77
- return iri;
78
-
79
- // Exact base IRI match
80
- const length = match[0].length;
81
- if (length === this._baseLength && length === iri.length)
82
- return '';
83
-
84
- // Parent path match
85
- const parentPath = this._pathReplacements[length];
86
- if (parentPath) {
87
- const suffix = iri.substring(length);
88
- // Don't abbreviate unsupported path
89
- if (parentPath !== QUERY && !SUFFIX_SUPPORTED.test(suffix))
90
- return iri;
91
- // Omit ./ with fragment or query string
92
- if (parentPath === CURRENT && /^[^?#]/.test(suffix))
93
- return suffix;
94
- // Append suffix to relative parent path
95
- return parentPath + suffix;
96
- }
97
-
98
- // Fragment or query string, so include delimiter
99
- return iri.substring(length - 1);
100
- }
101
- }
package/src/Util.js DELETED
@@ -1,3 +0,0 @@
1
- export function escapeRegex(regex) {
2
- return regex.replace(/[\]\/\(\)\*\+\?\.\\\$]/g, '\\$&');
3
- }