Skip to content

Commit 0faad45

Browse files
committed
use nonSimpleDomain with regex
1 parent 04c90f4 commit 0faad45

3 files changed

Lines changed: 30 additions & 18 deletions

File tree

benchmark/non-simple-domain.mjs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Bench } from 'tinybench'
2+
import { nonSimpleDomain } from '../lib/utils.js'
3+
4+
const benchNonSimpleDomain = new Bench({ name: 'nonSimpleDomain' })
5+
6+
const exampleCom = 'example.com'
7+
const exaumlmpleCom = 'exämple.com'
8+
const longDomain = 'abc'.repeat(100) + '.com'
9+
10+
console.assert(nonSimpleDomain(exampleCom) === false, 'example.com should be a simple domain')
11+
console.assert(nonSimpleDomain(exaumlmpleCom) === true, 'exämple.com should not be a simple domain')
12+
console.assert(nonSimpleDomain(longDomain) === false, `${longDomain} should be a simple domain?`)
13+
14+
benchNonSimpleDomain.add('nonSimpleDomain', function () {
15+
nonSimpleDomain(exampleCom)
16+
nonSimpleDomain(exaumlmpleCom)
17+
nonSimpleDomain(longDomain)
18+
})
19+
20+
await benchNonSimpleDomain.run()
21+
console.log(benchNonSimpleDomain.name)
22+
console.table(benchNonSimpleDomain.table())

index.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { normalizeIPv6, removeDotSegments, recomposeAuthority, normalizeComponentEncoding, isIPv4 } = require('./lib/utils')
3+
const { normalizeIPv6, removeDotSegments, recomposeAuthority, normalizeComponentEncoding, isIPv4, nonSimpleDomain } = require('./lib/utils')
44
const { SCHEMES, getSchemeHandler } = require('./lib/schemes')
55

66
/**
@@ -211,23 +211,6 @@ function serialize (cmpts, opts) {
211211
return uriTokens.join('')
212212
}
213213

214-
const hexLookUp = Array.from({ length: 127 }, (_v, k) => /[^!"$&'()*+,\-.;=_`a-z{}~]/u.test(String.fromCharCode(k)))
215-
216-
/**
217-
* @param {string} value
218-
* @returns {boolean}
219-
*/
220-
function nonSimpleDomain (value) {
221-
let code = 0
222-
for (let i = 0, len = value.length; i < len; ++i) {
223-
code = value.charCodeAt(i)
224-
if (code > 126 || hexLookUp[code]) {
225-
return true
226-
}
227-
}
228-
return false
229-
}
230-
231214
const URI_PARSE = /^(?:([^#/:?]+):)?(?:\/\/((?:([^#/?@]*)@)?(\[[^#/?\]]+\]|[^#/:?]*)(?::(\d*))?))?([^#?]*)(?:\?([^#]*))?(?:#((?:.|[\n\r])*))?/u
232215

233216
/**

lib/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ function stringArrayToHexStripped (input) {
4343
* @property {string} [zone] - The zone identifier, if present.
4444
*/
4545

46+
/**
47+
* @param {string} value
48+
* @returns {boolean}
49+
*/
50+
const nonSimpleDomain = RegExp.prototype.test.bind(/[^!"$&'()*+,\-.;=_`a-z{}~]/u)
51+
4652
/**
4753
* @param {string} input
4854
* @returns {GetIPV6Result}
@@ -305,6 +311,7 @@ function recomposeAuthority (components) {
305311
};
306312

307313
module.exports = {
314+
nonSimpleDomain,
308315
recomposeAuthority,
309316
normalizeComponentEncoding,
310317
removeDotSegments,

0 commit comments

Comments
 (0)