identifier-js 0.0.9 → 0.0.11
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/index.js +6 -5
- package/package.json +1 -1
- package/tests/validation-scheme-specific.test.js +50 -0
package/index.js
CHANGED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
// a valid URI is always a valid IRI
|
|
3
3
|
const { recursiveCompile } = require('url-templates');
|
|
4
4
|
const patterns = new Map();
|
|
5
|
+
const implemented_schemes = '(?:[hH][tT][tT][pP][sS]?|[wW][sS][sS]?|[fF][iI][lL][eE])';
|
|
5
6
|
// RFC3986/RFC3987 common rules + https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2:~:text=DNS%29%2E-,A,of%20%5BRFC1123%5D%2E
|
|
6
7
|
const commonRules = {
|
|
7
|
-
implemented_schemes
|
|
8
|
-
scheme: '(?!{implemented_schemes})[a-zA-Z][a-zA-Z0-9+.-]*',
|
|
8
|
+
implemented_schemes,
|
|
9
|
+
scheme: '(?!{implemented_schemes}:)[a-zA-Z][a-zA-Z0-9+.-]*',
|
|
9
10
|
port: '(?:0|[1-9]\\d{0,3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])?',
|
|
10
11
|
IP_literal: '\\[(?:{IPv6address}|{IPvFuture})\\]',
|
|
11
12
|
IPv6address: '(?:(?:{h16}:){6}{ls32}|::(?:{h16}:){5}{ls32}|(?:(?:{h16})?)::(?:{h16}:){4}{ls32}|(?:(?:{h16}:)?{h16})?::(?:{h16}:){3}{ls32}|(?:(?:{h16}:){0,2}{h16})?::(?:{h16}:){2}{ls32}|(?:(?:{h16}:){0,3}{h16})?::(?:{h16}:){1}{ls32}|(?:(?:{h16}:){0,4}{h16})?::{ls32}|(?:(?:{h16}:){0,5}{h16})?::{h16}|(?:(?:{h16}:){0,6}{h16})?::)',
|
|
@@ -79,7 +80,7 @@ const iriRules = {
|
|
|
79
80
|
};
|
|
80
81
|
// scheme specific URI reg_name and IRI ireg_name
|
|
81
82
|
const schemeSpecificRules = {
|
|
82
|
-
scheme:
|
|
83
|
+
scheme: implemented_schemes,
|
|
83
84
|
reg_name: '(?:(?=.{1,255}(?:[:/?#]|$))(?:{a_label})(?:\\.{a_label})*)',
|
|
84
85
|
a_label: '(?:{alpha_digit})(?:(?:{alpha_digit}|-){0,61}(?:{alpha_digit}))?',
|
|
85
86
|
ireg_name: '(?:(?=.{1,255}(?:[:/?#]|$))(?:{u_label})(?:{u_separator}(?:{u_label}))*)',
|
|
@@ -113,8 +114,8 @@ const groupNames = {
|
|
|
113
114
|
ipath_empty: 'path',
|
|
114
115
|
};
|
|
115
116
|
// all rules into one map
|
|
116
|
-
const isSpecificScheme = (string) => new RegExp('^' +
|
|
117
|
-
const rules = (specific) => specific ? Object.assign({}, commonRules, uriRules, iriRules, schemeSpecificRules) : Object.assign({}, commonRules, uriRules, iriRules);
|
|
117
|
+
const isSpecificScheme = (string) => new RegExp('^' + implemented_schemes + ':').test(string);
|
|
118
|
+
const rules = (specific) => (specific ? Object.assign({}, commonRules, uriRules, iriRules, schemeSpecificRules) : Object.assign({}, commonRules, uriRules, iriRules));
|
|
118
119
|
// parse (slower, it uses regex.exec and includes named capture groups)
|
|
119
120
|
const parse = (string, rule) => {
|
|
120
121
|
if (typeof string !== 'string') throw new TypeError(`Invalid ${rule.replace('_', '-')} type: must be a string.`);
|
package/package.json
CHANGED
|
@@ -46,6 +46,10 @@ describe('isUri with hostnames', () => {
|
|
|
46
46
|
expect(id.isUri('uri://exa=mple')).to.equal(true);
|
|
47
47
|
});
|
|
48
48
|
|
|
49
|
+
test('Valid character - (unreserved) anywhere in uri reg_name', () => {
|
|
50
|
+
expect(id.isUri('iri://-exa-mple-')).to.equal(true);
|
|
51
|
+
});
|
|
52
|
+
|
|
49
53
|
test('Valid character . multiple times (unreserved) in uri reg_name', () => {
|
|
50
54
|
expect(id.isUri('uri://exa..mple')).to.equal(true);
|
|
51
55
|
});
|
|
@@ -269,6 +273,27 @@ describe('isUri with hostnames', () => {
|
|
|
269
273
|
test('Invalid - = character (sub-delims) in file reg_name', () => {
|
|
270
274
|
expect(() => id.isUri('file://exa=mple')).to.throw(Error, 'Invalid URI: file://exa=mple');
|
|
271
275
|
});
|
|
276
|
+
|
|
277
|
+
test('Valid case insensitive http scheme', () => {
|
|
278
|
+
expect(id.isUri('httP://example')).to.equal(true);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
test('Valid case insensitive https scheme', () => {
|
|
282
|
+
expect(id.isUri('httPs://example')).to.equal(true);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
test('Valid case insensitive ws scheme', () => {
|
|
286
|
+
expect(id.isUri('WS://example')).to.equal(true);
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
test('Valid case insensitive wss scheme', () => {
|
|
290
|
+
expect(id.isUri('WSs://example')).to.equal(true);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
test('Valid case insensitive file scheme', () => {
|
|
294
|
+
expect(id.isUri('fILE://example')).to.equal(true);
|
|
295
|
+
});
|
|
296
|
+
|
|
272
297
|
});
|
|
273
298
|
|
|
274
299
|
describe('isIri with hostnames', () => {
|
|
@@ -316,6 +341,10 @@ describe('isIri with hostnames', () => {
|
|
|
316
341
|
expect(id.isIri('iri://exa=mple')).to.equal(true);
|
|
317
342
|
});
|
|
318
343
|
|
|
344
|
+
test('Valid character - (unreserved) anywhere in iri reg_name', () => {
|
|
345
|
+
expect(id.isIri('iri://-exa-mple-')).to.equal(true);
|
|
346
|
+
});
|
|
347
|
+
|
|
319
348
|
test('Valid character . multiple times (unreserved) in iri reg_name', () => {
|
|
320
349
|
expect(id.isIri('iri://exa..mple')).to.equal(true);
|
|
321
350
|
});
|
|
@@ -551,4 +580,25 @@ describe('isIri with hostnames', () => {
|
|
|
551
580
|
test('Invalid - = character (sub-delims) in file reg_name', () => {
|
|
552
581
|
expect(() => id.isIri('file://exa=mple')).to.throw(Error, 'Invalid IRI: file://exa=mple');
|
|
553
582
|
});
|
|
583
|
+
|
|
584
|
+
test('Valid case insensitive http scheme', () => {
|
|
585
|
+
expect(id.isIri('httP://example')).to.equal(true);
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
test('Valid case insensitive https scheme', () => {
|
|
589
|
+
expect(id.isIri('httPs://example')).to.equal(true);
|
|
590
|
+
});
|
|
591
|
+
|
|
592
|
+
test('Valid case insensitive ws scheme', () => {
|
|
593
|
+
expect(id.isIri('WS://example')).to.equal(true);
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
test('Valid case insensitive wss scheme', () => {
|
|
597
|
+
expect(id.isIri('WSs://example')).to.equal(true);
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
test('Valid case insensitive file scheme', () => {
|
|
601
|
+
expect(id.isIri('fILE://example')).to.equal(true);
|
|
602
|
+
});
|
|
603
|
+
|
|
554
604
|
});
|