Skip to content

Commit ade3ba0

Browse files
committed
extract isUUID to utils.js
1 parent 0faad45 commit ade3ba0

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

lib/schemes.js

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

3-
const UUID_REG = /^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iu
3+
const { isUUID } = require('./utils')
44
const URN_REG = /([\da-z][\d\-a-z]{0,31}):((?:[\w!$'()*+,\-.:;=@]|%[\da-f]{2})+)/iu
55

66
const supportedSchemeNames = /** @type {const} */ (['http', 'https', 'ws',
@@ -162,7 +162,7 @@ function urnuuidParse (urnComponent, options) {
162162
uuidComponents.uuid = uuidComponents.nss
163163
uuidComponents.nss = undefined
164164

165-
if (!options.tolerant && (!uuidComponents.uuid || !UUID_REG.test(uuidComponents.uuid))) {
165+
if (!options.tolerant && (!uuidComponents.uuid || !isUUID(uuidComponents.uuid))) {
166166
uuidComponents.error = uuidComponents.error || 'UUID is not valid.'
167167
}
168168

lib/utils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict'
22

3-
const IPV4_REG = /^(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)$/u
3+
/** @type {(value: string) => boolean} */
4+
const isUUID = RegExp.prototype.test.bind(/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iu)
45

56
/** @type {(value: string) => boolean} */
6-
const isIPv4 = RegExp.prototype.test.bind(IPV4_REG)
7+
const isIPv4 = RegExp.prototype.test.bind(/^(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)$/u)
78

89
/**
910
* @param {Array<string>} input
@@ -316,6 +317,7 @@ module.exports = {
316317
normalizeComponentEncoding,
317318
removeDotSegments,
318319
isIPv4,
320+
isUUID,
319321
normalizeIPv6,
320322
stringArrayToHexStripped
321323
}

0 commit comments

Comments
 (0)