Skip to content

Commit 96822af

Browse files
committed
fix: correct param name typos
1 parent f81a6be commit 96822af

File tree

3 files changed

+36
-49
lines changed

3 files changed

+36
-49
lines changed

src/encode.js

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,30 @@ const { isObject } = require('./objects')
44
const { isNonEmptyString } = require('./strings')
55

66
const reusedSearchParams = new URLSearchParams()
7+
const reusedSearchParamKey = '_'
8+
const reusedSearchParamOffset = 2 // '_='.length
79

810
const { encodeURIComponent } = globalThis
911

10-
function encodeWithColonAndForwardSlash(str) {
11-
return encodeURIComponent(str).replace(/%3A/g, ':').replace(/%2F/g, '/')
12-
}
13-
14-
function encodeWithColonAndPlusSign(str) {
15-
return encodeURIComponent(str).replace(/%3A/g, ':').replace(/%2B/g, '+')
16-
}
17-
18-
function encodeWithForwardSlash(str) {
19-
return encodeURIComponent(str).replace(/%2F/g, '/')
20-
}
21-
2212
function encodeNamespace(namespace) {
2313
return isNonEmptyString(namespace)
24-
? encodeWithColonAndForwardSlash(namespace)
14+
? encodeURIComponent(namespace)
15+
.replace(/%3A/g, ':')
16+
.replace(/%2F/g, '/')
2517
: ''
2618
}
2719

28-
function encodeVersion(version) {
29-
return isNonEmptyString(version) ? encodeWithColonAndPlusSign(version) : ''
20+
function encodeQualifierParam(param) {
21+
if (isNonEmptyString(param)) {
22+
// Param key and value are encoded with `percentEncodeSet` of
23+
// 'application/x-www-form-urlencoded' and `spaceAsPlus` of `true`.
24+
// https://url.spec.whatwg.org/#urlencoded-serializing
25+
reusedSearchParams.set(reusedSearchParamKey, param)
26+
return replacePlusSignWithPercentEncodedSpace(
27+
reusedSearchParams.toString().slice(reusedSearchParamOffset)
28+
)
29+
}
30+
return ''
3031
}
3132

3233
function encodeQualifiers(qualifiers) {
@@ -43,26 +44,16 @@ function encodeQualifiers(qualifiers) {
4344
return ''
4445
}
4546

46-
function encodeQualifierParam(qualifierValue) {
47-
return isNonEmptyString
48-
? encodeURLSearchParamWithPercentEncodedSpace(param)
49-
: ''
50-
}
51-
5247
function encodeSubpath(subpath) {
53-
return isNonEmptyString(subpath) ? encodeWithForwardSlash(subpath) : ''
54-
}
55-
56-
function encodeURLSearchParam(param) {
57-
// Param key and value are encoded with `percentEncodeSet` of
58-
// 'application/x-www-form-urlencoded' and `spaceAsPlus` of `true`.
59-
// https://url.spec.whatwg.org/#urlencoded-serializing
60-
reusedSearchParams.set('_', qualifierValue)
61-
return reusedSearchParams.toString().slice(2)
48+
return isNonEmptyString(subpath)
49+
? encodeURIComponent(subpath).replace(/%2F/g, '/')
50+
: ''
6251
}
6352

64-
function encodeURLSearchParamWithPercentEncodedSpace(str) {
65-
return replacePlusSignWithPercentEncodedSpace(encodeURLSearchParam(str))
53+
function encodeVersion(version) {
54+
return isNonEmptyString(version)
55+
? encodeURIComponent(version).replace(/%3A/g, ':').replace(/%2B/g, '+')
56+
: ''
6657
}
6758

6859
function replacePlusSignWithPercentEncodedSpace(str) {
@@ -71,15 +62,10 @@ function replacePlusSignWithPercentEncodedSpace(str) {
7162
}
7263

7364
module.exports = {
74-
encodeWithColonAndForwardSlash,
75-
encodeWithColonAndPlusSign,
76-
encodeWithForwardSlash,
7765
encodeNamespace,
7866
encodeVersion,
7967
encodeQualifiers,
8068
encodeQualifierParam,
8169
encodeSubpath,
82-
encodeURIComponent,
83-
encodeURLSearchParam,
84-
encodeURLSearchParamWithPercentEncodedSpace
70+
encodeURIComponent
8571
}

src/purl-component.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,28 @@ const {
1212
const { createHelpersNamespaceObject } = require('./helpers')
1313

1414
const {
15-
normalizeName,
15+
normalizeType,
1616
normalizeNamespace,
17+
normalizeName,
18+
normalizeVersion,
1719
normalizeQualifiers,
18-
normalizeSubpath,
19-
normalizeType,
20-
normalizeVersion
20+
normalizeSubpath
2121
} = require('./normalize')
2222

23-
const { localeCompare } = require('./strings')
23+
const { localeCompare, isNonEmptyString } = require('./strings')
2424

2525
const {
26-
validateName,
26+
validateType,
2727
validateNamespace,
28+
validateName,
29+
validateVersion,
2830
validateQualifiers,
2931
validateQualifierKey,
30-
validateSubpath,
31-
validateType,
32-
validateVersion
32+
validateSubpath
3333
} = require('./validate')
3434

3535
const PurlComponentEncoder = (comp) =>
36-
typeof comp === 'string' && comp.length ? encodeURIComponent(comp) : ''
36+
isNonEmptyString(comp) ? encodeURIComponent(comp) : ''
3737

3838
const PurlComponentStringNormalizer = (comp) =>
3939
typeof comp === 'string' ? comp : undefined

src/validate.js

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

33
const { isNullishOrEmptyString } = require('./lang')
4+
const { isNonEmptyString } = require('./strings')
45

56
function validateEmptyByType(type, name, value, throws) {
67
if (!isNullishOrEmptyString(value)) {
@@ -104,7 +105,7 @@ function validateRequiredByType(type, name, value, throws) {
104105
}
105106

106107
function validateStartsWithoutNumber(name, value, throws) {
107-
if (value.length !== 0) {
108+
if (isNonEmptyString(value)) {
108109
const code = value.charCodeAt(0)
109110
if (code >= 48 /*'0'*/ && code <= 57 /*'9'*/) {
110111
if (throws) {

0 commit comments

Comments
 (0)