identifier-js 0.0.2 → 0.0.3

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.d.ts CHANGED
@@ -1,25 +1,43 @@
1
- export const isUUID: (string: string) => boolean;
2
- export const isUUIDv4: (string: string) => boolean;
1
+ /** @throws {Error} If the UUID is invalid. */
2
+ export const isUUID: (string: string) => true;
3
+ /** @throws {Error} If the UUID-v4 is invalid. */
4
+ export const isUUIDv4: (string: string) => true;
3
5
 
4
- export const isUri: (uri: string) => boolean;
5
- export const isUriReference: (uriReference: string) => boolean;
6
- export const isAbsoluteUri: (uri: string) => boolean;
6
+ /** @throws {Error} If the URI is invalid. */
7
+ export const isUri: (uri: string) => true;
8
+ /** @throws {Error} If the URI-reference is invalid. */
9
+ export const isUriReference: (uriReference: string) => true;
10
+ /** @throws {Error} If the absolute-URI is invalid. */
11
+ export const isAbsoluteUri: (uri: string) => true;
7
12
 
13
+ /** @throws {Error} If the URI is invalid. */
8
14
  export const parseUri: (uri: string) => IdentifierComponents;
15
+ /** @throws {Error} If the URI-reference is invalid. */
9
16
  export const parseUriReference: (uriReference: string) => RelativeIdentifierComponents;
17
+ /** @throws {Error} If the absolute-URI is invalid. */
10
18
  export const parseAbsoluteUri: (uri: string) => AbsoluteIdentifierComponents;
11
19
 
12
- export const isIri: (iri: string) => boolean;
13
- export const isIriReference: (iriReference: string) => boolean;
14
- export const isAbsoluteIri: (iri: string) => boolean;
20
+ /** @throws {Error} If the IRI is invalid. */
21
+ export const isIri: (iri: string) => true;
22
+ /** @throws {Error} If the IRI-reference is invalid. */
23
+ export const isIriReference: (iriReference: string) => true;
24
+ /** @throws {Error} If the absolute-IRI is invalid. */
25
+ export const isAbsoluteIri: (iri: string) => true;
15
26
 
27
+ /** @throws {Error} If the IRI is invalid. */
16
28
  export const parseIri: (iri: string) => IdentifierComponents;
29
+ /** @throws {Error} If the IRI-reference is invalid. */
17
30
  export const parseIriReference: (iriReference: string) => RelativeIdentifierComponents;
31
+ /** @throws {Error} If the absolute-IRI is invalid. */
18
32
  export const parseAbsoluteIri: (iri: string) => AbsoluteIdentifierComponents;
19
33
 
34
+ /** @throws {Error} If the reference is invalid. */
20
35
  export const normalizeReference: (reference: string) => string;
36
+ /** @throws {Error} If the base or the reference is invalid. */
21
37
  export const resolveReference: (reference: string, base: string, strict?: boolean, returnParts?: boolean) => string;
38
+ /** @throws {Error} If the reference is invalid. */
22
39
  export const toAbsoluteReference: (reference: string) => string;
40
+ /** @throws {Error} If the base or the reference is invalid. */
23
41
  export const toRelativeReference: (target: string, base: string) => string;
24
42
 
25
43
  type IdentifierComponents = {
package/index.js CHANGED
@@ -19,8 +19,8 @@ const commonRules = {
19
19
  gen_delims: '[:/?#[\\]@]',
20
20
  sub_delims: "[!&'()*+,;=$]",
21
21
  hexdig: '[0-9A-Fa-f]',
22
- uuid: '[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}',
23
- uuid_v4: '[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}',
22
+ UUID: '[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}',
23
+ UUID_v4: '[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}',
24
24
  };
25
25
  // RFC3986 rules
26
26
  const uriRules = {
@@ -105,15 +105,18 @@ const rules = Object.assign({}, commonRules, uriRules, iriRules);
105
105
  const addNames = (key) => (groupNames[key] ? `(?<${groupNames[key]}>${rules[key]})` : rules[key]);
106
106
  // parse (slower, it uses regex.exec and includes named capture groups)
107
107
  const parse = (string, rule) => {
108
+ if (typeof string !== 'string') throw new TypeError(`Invalid ${rule.replace('_', '-')} type: must be a string.`);
108
109
  if (!patterns.has('_' + rule)) patterns.set('_' + rule, new RegExp(`^${recursiveCompile(rules, rule, addNames)}$`, 'u'));
109
110
  const match = patterns.get('_' + rule).exec(string);
110
111
  if (match) return match.groups;
111
- throw new TypeError(`Invalid ${rule.replace('_', '-')}: ${string}`);
112
+ throw new SyntaxError(`Invalid ${rule.replace('_', '-')}: ${string}`);
112
113
  };
113
114
  // validate (faster, it uses regex.test and does not include named capture groups)
114
115
  const validate = (string, rule) => {
116
+ if (typeof string !== 'string') throw new TypeError(`Invalid ${rule.replace('_', '-')} type: must be a string.`);
115
117
  if (!patterns.has(rule)) patterns.set(rule, new RegExp(`^${recursiveCompile(rules, rule)}$`, 'u'));
116
- return patterns.get(rule).test(string);
118
+ if (patterns.get(rule).test(string)) return true;
119
+ throw new SyntaxError(`Invalid ${rule.replace('_', '-')}: ${string}`);
117
120
  };
118
121
  // compose as per RFC 3986 Section 5.3 (component recomposition)
119
122
  function compose(parts = {}) {
@@ -249,8 +252,8 @@ const toRelativeReference = (target, base) => {
249
252
  };
250
253
  // export
251
254
  module.exports = {
252
- isUUID: (string) => validate(string, 'uuid'),
253
- isUUIDv4: (string) => validate(string, 'uuid_v4'),
255
+ isUUID: (string) => validate(string, 'UUID'),
256
+ isUUIDv4: (string) => validate(string, 'UUID_v4'),
254
257
  isUri: (string) => validate(string, 'URI'),
255
258
  isUriReference: (string) => validate(string, 'URI_reference'),
256
259
  isAbsoluteUri: (string) => validate(string, 'absolute_URI'),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "identifier-js",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "A RFC3986 / RFC3987 compliant fast parser/validator/resolver/composer for NodeJS and browser.",
5
5
  "keywords": [
6
6
  "IRI",
@@ -35,5 +35,9 @@
35
35
  },
36
36
  "dependencies": {
37
37
  "url-templates": "^1.0.4"
38
- }
38
+ },
39
+ "engines": {
40
+ "node": ">=18.0.0"
41
+ },
42
+ "engineStrict": true
39
43
  }
package/readme.md CHANGED
@@ -12,6 +12,10 @@ A fully RFC [3986](https://datatracker.ietf.org/doc/html/rfc3986.html)/[3897](ht
12
12
 
13
13
  ## Install
14
14
 
15
+ ### Requirements
16
+
17
+ - `regular expression with unicode support`, available in modern browsers and `NodeJS 18+`
18
+
15
19
  ```bash title="console"
16
20
  npm i identifier-js
17
21
  ```
@@ -15,54 +15,54 @@ describe('isUUID', () => {
15
15
  });
16
16
 
17
17
  test('invalid UUID missing hyphens', () => {
18
- expect(id.isUUID('123e4567e89b12d3a456426614174000')).to.equal(false);
18
+ expect(() => id.isUUID('123e4567e89b12d3a456426614174000')).to.throw(Error, 'Invalid UUID: 123e4567e89b12d3a456426614174000');
19
19
  });
20
20
 
21
21
  test('invalid UUID with extra hyphens', () => {
22
- expect(id.isUUID('123e4567-e89b-12d3-a456-426-614174000')).to.equal(false);
22
+ expect(() => id.isUUID('123e4567-e89b-12d3-a456-426-614174000')).to.throw(Error, 'Invalid UUID: 123e4567-e89b-12d3-a456-426-614174000');
23
23
  });
24
24
 
25
25
  test('invalid UUID with incorrect group lengths', () => {
26
- expect(id.isUUID('123e4567-e89b-12d3-a456426614174000')).to.equal(false);
26
+ expect(() => id.isUUID('123e4567-e89b-12d3-a456426614174000')).to.throw(Error, 'Invalid UUID: 123e4567-e89b-12d3-a456426614174000');
27
27
  });
28
28
 
29
29
  test('invalid UUID with non-hexadecimal characters', () => {
30
- expect(id.isUUID('123e4567-e89b-12d3-g456-426614174000')).to.equal(false);
30
+ expect(() => id.isUUID('123e4567-e89b-12d3-g456-426614174000')).to.throw(Error, 'Invalid UUID: 123e4567-e89b-12d3-g456-426614174000');
31
31
  });
32
32
 
33
33
  test('invalid UUID string that is too short', () => {
34
- expect(id.isUUID('123e4567-e89b-12d3-a456-42661417400')).to.equal(false);
34
+ expect(() => id.isUUID('123e4567-e89b-12d3-a456-42661417400')).to.throw(Error, 'Invalid UUID: 123e4567-e89b-12d3-a456-42661417400');
35
35
  });
36
36
 
37
37
  test('invalid UUID string that is too long', () => {
38
- expect(id.isUUID('123e4567-e89b-12d3-a456-4266141740000')).to.equal(false);
38
+ expect(() => id.isUUID('123e4567-e89b-12d3-a456-4266141740000')).to.throw(Error, 'Invalid UUID: 123e4567-e89b-12d3-a456-4266141740000');
39
39
  });
40
40
 
41
- test('valid uuid with all zeroes except version control chars', () => {
41
+ test('valid UUID with all zeroes except version control chars', () => {
42
42
  expect(id.isUUID('00000000-0000-4000-8000-000000000000')).to.equal(true);
43
43
  });
44
44
 
45
- test('invalid uuid with missing section', () => {
46
- expect(id.isUUID('2eb8aa08-aa98-11ea-73b441d16380')).to.equal(false);
45
+ test('invalid UUID with missing section', () => {
46
+ expect(() => id.isUUID('2eb8aa08-aa98-11ea-73b441d16380')).to.throw(Error, 'Invalid UUID: 2eb8aa08-aa98-11ea-73b441d16380');
47
47
  });
48
48
 
49
- test('invalid uuid with too many dashes', () => {
50
- expect(id.isUUID('2eb8-aa08-aa98-11ea-b4aa73b44-1d16380')).to.equal(false);
49
+ test('invalid UUID with too many dashes', () => {
50
+ expect(() => id.isUUID('2eb8-aa08-aa98-11ea-b4aa73b44-1d16380')).to.throw(Error, 'Invalid UUID: 2eb8-aa08-aa98-11ea-b4aa73b44-1d16380');
51
51
  });
52
52
 
53
- test('invalid uuid with dashes in the wrong spot', () => {
54
- expect(id.isUUID('2eb8aa08aa9811eab4aa73b441d16380----')).to.equal(false);
53
+ test('invalid UUID with dashes in the wrong spot', () => {
54
+ expect(() => id.isUUID('2eb8aa08aa9811eab4aa73b441d16380----')).to.throw(Error, 'Invalid UUID: 2eb8aa08aa9811eab4aa73b441d16380----');
55
55
  });
56
56
 
57
- test('valid uuid v5', () => {
57
+ test('valid UUID v5', () => {
58
58
  expect(id.isUUID('99c17cbb-656f-564a-940f-1a4568f03487')).to.equal(true);
59
59
  });
60
60
 
61
- test('valid uuid hypothetical v6', () => {
61
+ test('valid UUID hypothetical v6', () => {
62
62
  expect(id.isUUID('99c17cbb-656f-664a-940f-1a4568f03487')).to.equal(true);
63
63
  });
64
64
 
65
- test('valid uuid hypothetical v15', () => {
65
+ test('valid UUID hypothetical v15', () => {
66
66
  expect(id.isUUID('99c17cbb-656f-f64a-940f-1a4568f03487')).to.equal(true);
67
67
  });
68
68
  });
@@ -81,43 +81,43 @@ describe('isUUIDv4', () => {
81
81
  });
82
82
 
83
83
  test('invalid UUID v4 missing hyphens', () => {
84
- expect(id.isUUIDv4('123e4567e89b12d3a456426614174000')).to.equal(false);
84
+ expect(() => id.isUUIDv4('123e4567e89b12d3a456426614174000')).to.throw(Error, 'Invalid UUID-v4: 123e4567e89b12d3a456426614174000');
85
85
  });
86
86
 
87
87
  test('invalid UUID v4 with extra hyphens', () => {
88
- expect(id.isUUIDv4('123e4567-e89b-12d3-a456-426-614174000')).to.equal(false);
88
+ expect(() => id.isUUIDv4('123e4567-e89b-12d3-a456-426-614174000')).to.throw(Error, 'Invalid UUID-v4: 123e4567-e89b-12d3-a456-426-614174000');
89
89
  });
90
90
 
91
91
  test('invalid UUID v4 with incorrect group lengths', () => {
92
- expect(id.isUUIDv4('123e4567-e89b-12d3-a456426614174000')).to.equal(false);
92
+ expect(() => id.isUUIDv4('123e4567-e89b-12d3-a456426614174000')).to.throw(Error, 'Invalid UUID-v4: 123e4567-e89b-12d3-a456426614174000');
93
93
  });
94
94
 
95
95
  test('invalid UUID v4 with non-hexadecimal characters', () => {
96
- expect(id.isUUIDv4('123e4567-e89b-12d3-g456-426614174000')).to.equal(false);
96
+ expect(() => id.isUUIDv4('123e4567-e89b-12d3-g456-426614174000')).to.throw(Error, 'Invalid UUID-v4: 123e4567-e89b-12d3-g456-426614174000');
97
97
  });
98
98
 
99
99
  test('invalid UUID v4 string that is too short', () => {
100
- expect(id.isUUIDv4('123e4567-e89b-12d3-a456-42661417400')).to.equal(false);
100
+ expect(() => id.isUUIDv4('123e4567-e89b-12d3-a456-42661417400')).to.throw(Error, 'Invalid UUID-v4: 123e4567-e89b-12d3-a456-42661417400');
101
101
  });
102
102
 
103
103
  test('invalid UUID v4 string that is too long', () => {
104
- expect(id.isUUIDv4('123e4567-e89b-12d3-a456-4266141740000')).to.equal(false);
104
+ expect(() => id.isUUIDv4('123e4567-e89b-12d3-a456-4266141740000')).to.throw(Error, 'Invalid UUID-v4: 123e4567-e89b-12d3-a456-4266141740000');
105
105
  });
106
106
 
107
- test('valid uuid v4 with all zeroes except version control chars', () => {
107
+ test('valid UUID v4 with all zeroes except version control chars', () => {
108
108
  expect(id.isUUIDv4('00000000-0000-4000-8000-000000000000')).to.equal(true);
109
109
  });
110
110
 
111
- test('invalid uuid v4 with missing section', () => {
112
- expect(id.isUUIDv4('2eb8aa08-aa98-11ea-73b441d16380')).to.equal(false);
111
+ test('invalid UUID v4 with missing section', () => {
112
+ expect(() => id.isUUIDv4('2eb8aa08-aa98-11ea-73b441d16380')).to.throw(Error, 'Invalid UUID-v4: 2eb8aa08-aa98-11ea-73b441d16380');
113
113
  });
114
114
 
115
- test('invalid uuid v4 with too many dashes', () => {
116
- expect(id.isUUIDv4('2eb8-aa08-aa98-11ea-b4aa73b44-1d16380')).to.equal(false);
115
+ test('invalid UUID v4 with too many dashes', () => {
116
+ expect(() => id.isUUIDv4('2eb8-aa08-aa98-11ea-b4aa73b44-1d16380')).to.throw(Error, 'Invalid UUID-v4: 2eb8-aa08-aa98-11ea-b4aa73b44-1d16380');
117
117
  });
118
118
 
119
- test('invalid uuid v4 with dashes in the wrong spot', () => {
120
- expect(id.isUUIDv4('2eb8aa08aa9811eab4aa73b441d16380----')).to.equal(false);
119
+ test('invalid UUID v4 with dashes in the wrong spot', () => {
120
+ expect(() => id.isUUIDv4('2eb8aa08aa9811eab4aa73b441d16380----')).to.throw(Error, 'Invalid UUID-v4: 2eb8aa08aa9811eab4aa73b441d16380----');
121
121
  });
122
122
  });
123
123
 
@@ -127,7 +127,7 @@ describe('isUri', () => {
127
127
  });
128
128
 
129
129
  test('Scheme is required', () => {
130
- expect(id.isUri('//example.com/foo?bar#baz')).to.equal(false);
130
+ expect(() => id.isUri('//example.com/foo?bar#baz')).to.throw(Error, 'Invalid URI: //example.com/foo?bar#baz');
131
131
  });
132
132
 
133
133
  test('No authority', () => {
@@ -135,7 +135,7 @@ describe('isUri', () => {
135
135
  });
136
136
 
137
137
  test('No authority starting with double slash', () => {
138
- expect(id.isUri('uri://12:34:56/foo?bar#baz')).to.equal(false);
138
+ expect(() => id.isUri('uri://12:34:56/foo?bar#baz')).to.throw(Error, 'Invalid URI: uri://12:34:56/foo?bar#baz');
139
139
  });
140
140
 
141
141
  test('Rootless path', () => {
@@ -143,7 +143,7 @@ describe('isUri', () => {
143
143
  });
144
144
 
145
145
  test('Unicode is not allowed', () => {
146
- expect(id.isUri('http://examplé.org/rosé#')).to.equal(false);
146
+ expect(() => id.isUri('http://examplé.org/rosé#')).to.throw(Error, 'Invalid URI: http://examplé.org/rosé#');
147
147
  });
148
148
  });
149
149
 
@@ -177,7 +177,7 @@ describe('isUriReference', () => {
177
177
  });
178
178
 
179
179
  test('Unicode is not allowed', () => {
180
- expect(id.isUriReference('/rosé#')).to.equal(false);
180
+ expect(() => id.isUriReference('/rosé#')).to.throw(Error, 'Invalid URI-reference: /rosé#');
181
181
  });
182
182
  });
183
183
 
@@ -187,15 +187,15 @@ describe('isAbsoluteUri', () => {
187
187
  });
188
188
 
189
189
  test('Scheme is required', () => {
190
- expect(id.isAbsoluteUri('//example.com/foo?bar')).to.equal(false);
190
+ expect(() => id.isAbsoluteUri('//example.com/foo?bar')).to.throw(Error, 'Invalid absolute-URI: //example.com/foo?bar');
191
191
  });
192
192
 
193
193
  test('Fragment is not allowed', () => {
194
- expect(id.isAbsoluteUri('https://example.com/foo?bar#baz')).to.equal(false);
194
+ expect(() => id.isAbsoluteUri('https://example.com/foo?bar#baz')).to.throw(Error, 'Invalid absolute-URI: https://example.com/foo?bar#baz');
195
195
  });
196
196
 
197
197
  test('Unicode is not allowed', () => {
198
- expect(id.isAbsoluteUri('http://examplé.org/rosé')).to.equal(false);
198
+ expect(() => id.isAbsoluteUri('http://examplé.org/rosé')).to.throw(Error, 'Invalid absolute-URI: http://examplé.org/rosé');
199
199
  });
200
200
  });
201
201
 
@@ -205,7 +205,7 @@ describe('isIri', () => {
205
205
  });
206
206
 
207
207
  test('Scheme is required', () => {
208
- expect(id.isIri('//examplé.com/rosé?fóo#bár')).to.equal(false);
208
+ expect(() => id.isIri('//examplé.com/rosé?fóo#bár')).to.throw(Error, 'Invalid IRI: //examplé.com/rosé?fóo#bár');
209
209
  });
210
210
 
211
211
  test('No authority', () => {
@@ -213,7 +213,7 @@ describe('isIri', () => {
213
213
  });
214
214
 
215
215
  test('No authority starting with double slash', () => {
216
- expect(id.isIri('uri://12:23:45/rosé?fóo#bár')).to.equal(false);
216
+ expect(() => id.isIri('uri://12:23:45/rosé?fóo#bár')).to.throw(Error, 'Invalid IRI: uri://12:23:45/rosé?fóo#bár');
217
217
  });
218
218
 
219
219
  test('Rootless path', () => {
@@ -221,7 +221,7 @@ describe('isIri', () => {
221
221
  });
222
222
 
223
223
  test('Unicode is not allowed in scheme', () => {
224
- expect(id.isAbsoluteIri('examplé://examplé.org/rosé')).to.equal(false);
224
+ expect(() => id.isIri('examplé://examplé.org/rosé')).to.throw(Error, 'Invalid IRI: examplé://examplé.org/rosé');
225
225
  });
226
226
  });
227
227
 
@@ -265,14 +265,14 @@ describe('isAbsoluteIri', () => {
265
265
  });
266
266
 
267
267
  test('Scheme is required', () => {
268
- expect(id.isAbsoluteIri('//examplé.org/rosé?fóo')).to.equal(false);
268
+ expect(() => id.isAbsoluteIri('//examplé.org/rosé?fóo')).to.throw(Error, 'Invalid absolute-IRI: //examplé.org/rosé?fóo');
269
269
  });
270
270
 
271
271
  test('Fragment is not allowed', () => {
272
- expect(id.isAbsoluteIri('http://examplé.org/rosé?fóo#bár')).to.equal(false);
272
+ expect(() => id.isAbsoluteIri('http://examplé.org/rosé?fóo#bár')).to.throw(Error, 'Invalid absolute-IRI: http://examplé.org/rosé?fóo#bár');
273
273
  });
274
274
 
275
275
  test('Unicode is not allowed in scheme', () => {
276
- expect(id.isAbsoluteIri('examplé://examplé.org/rosé')).to.equal(false);
276
+ expect(() => id.isAbsoluteIri('examplé://examplé.org/rosé')).to.throw(Error, 'Invalid absolute-IRI: examplé://examplé.org/rosé');
277
277
  });
278
278
  });