Skip to content

Commit

Permalink
feat: minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
zekth committed Dec 2, 2021
1 parent c38fa05 commit 96ef89d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 27 deletions.
5 changes: 1 addition & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ function parse (uri, opts) {
if (parsed.host) {
const ipv4result = normalizeIPv4(parsed.host)
if (ipv4result.isIPV4 === false) {
parsed.host = normalizeIPv6(ipv4result.host, { isIPV4: false }).host
parsed.host = normalizeIPv6(ipv4result.host, { isIPV4: false }).host.toLowerCase()
} else {
parsed.host = ipv4result.host
}
Expand Down Expand Up @@ -288,9 +288,6 @@ function parse (uri, opts) {
if (schemeHandler && schemeHandler.parse) {
schemeHandler.parse(parsed, options)
}
if (parsed.host && parsed.host.length && parsed.host[0] === '[' && parsed.host[parsed.host.length - 1] === ']') {
parsed.host = parsed.host.substr(1).slice(0, -1)
}
} else {
parsed.error = parsed.error || 'URI can not be parsed.'
}
Expand Down
23 changes: 2 additions & 21 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,46 +107,27 @@ function normalizeIPv6 (host, opts = {}) {
}
}

function stripLeadingZeros (str, token, outputArray = false) {
const outArray = []
function stripLeadingZeros (str, token) {
let out = ''
let temp = ''
let skip = true
const l = str.length
for (let i = 0; i < l; i++) {
const c = str[i]
if (c === '0' && skip) {
if ((i + 1 <= l && str[i + 1] === token) || i + 1 === l) {
if (outputArray) {
temp += c
} else {
out += c
}
skip = false
}
} else {
if (c === token) {
skip = true
if (temp.length) {
outArray.push(temp)
temp = ''
}
} else {
skip = false
}
if (outputArray) {
if (c !== token) {
temp += c
}
} else {
out += c
}
}
}
if (temp.length) {
outArray.push(temp)
}
return outputArray ? outArray : out
return out
}

function findToken (str, token) {
Expand Down
8 changes: 6 additions & 2 deletions test/compatibility.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ test('compatibility Parse', (t) => {
'//10.10.10.10',
'//10.10.000.10',
'//[2001:db8::7%en0]',
'//[2001:dbZ::1]:80',
'//[2001:db8::1]:80',
'//[2001:db8::001]:80',
'uri://user:pass@example.com:123/one/two.three?q1=a1&q2=a2#body',
Expand All @@ -27,8 +28,11 @@ test('compatibility Parse', (t) => {
'wss://example.com/?bar=baz',
'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
'urn:uuid:notauuid-7dec-11d0-a765-00a0c91e6bf6',
'urn:example:%D0%B0123,z456'
// 'mailto:chris@example.com',
'urn:example:%D0%B0123,z456',
'//[2606:2800:220:1:248:1893:25c8:1946:43209]',
'http://foo.bar',
'http://'
// 'mailto:chris@example.com'-203845,
// 'mailto:infobot@example.com?subject=current-issue',
// 'mailto:infobot@example.com?body=send%20current-issue',
// 'mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index',
Expand Down
9 changes: 9 additions & 0 deletions test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ test('URI parse', (t) => {
t.equal(components.query, undefined, 'query')
t.equal(components.fragment, undefined, 'fragment')

// invalid IPv6
components = URI.parse('//[2001:dbZ::7]')
t.equal(components.host,'[2001:dbz::7]')

// mixed IPv4address & IPv6address
components = URI.parse('//[::ffff:129.144.52.38]')
t.equal(components.error, undefined, 'IPv4address errors')
Expand Down Expand Up @@ -297,5 +301,10 @@ test('URI parse', (t) => {
t.equal(components.nid, 'foo', 'nid')
t.equal(components.nss, 'a123,456', 'nss')

components = URI.parse('//[2606:2800:220:1:248:1893:25c8:1946:43209]')
t.equal(components.host,"[2606:2800:220:1:248:1893:25c8:1946:43209]")

components = URI.parse('urn:foo:|\\24fpl')
t.equal(components.error,'URN can not be parsed.')
t.end()
})

0 comments on commit 96ef89d

Please sign in to comment.