@@ -4,29 +4,30 @@ const { isObject } = require('./objects')
4
4
const { isNonEmptyString } = require ( './strings' )
5
5
6
6
const reusedSearchParams = new URLSearchParams ( )
7
+ const reusedSearchParamKey = '_'
8
+ const reusedSearchParamOffset = 2 // '_='.length
7
9
8
10
const { encodeURIComponent } = globalThis
9
11
10
- function encodeWithColonAndForwardSlash ( str ) {
11
- return encodeURIComponent ( str ) . replace ( / % 3 A / g, ':' ) . replace ( / % 2 F / g, '/' )
12
- }
13
-
14
- function encodeWithColonAndPlusSign ( str ) {
15
- return encodeURIComponent ( str ) . replace ( / % 3 A / g, ':' ) . replace ( / % 2 B / g, '+' )
16
- }
17
-
18
- function encodeWithForwardSlash ( str ) {
19
- return encodeURIComponent ( str ) . replace ( / % 2 F / g, '/' )
20
- }
21
-
22
12
function encodeNamespace ( namespace ) {
23
13
return isNonEmptyString ( namespace )
24
- ? encodeWithColonAndForwardSlash ( namespace )
14
+ ? encodeURIComponent ( namespace )
15
+ . replace ( / % 3 A / g, ':' )
16
+ . replace ( / % 2 F / g, '/' )
25
17
: ''
26
18
}
27
19
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 ''
30
31
}
31
32
32
33
function encodeQualifiers ( qualifiers ) {
@@ -43,26 +44,16 @@ function encodeQualifiers(qualifiers) {
43
44
return ''
44
45
}
45
46
46
- function encodeQualifierParam ( qualifierValue ) {
47
- return isNonEmptyString
48
- ? encodeURLSearchParamWithPercentEncodedSpace ( param )
49
- : ''
50
- }
51
-
52
47
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 ( / % 2 F / g, '/' )
50
+ : ''
62
51
}
63
52
64
- function encodeURLSearchParamWithPercentEncodedSpace ( str ) {
65
- return replacePlusSignWithPercentEncodedSpace ( encodeURLSearchParam ( str ) )
53
+ function encodeVersion ( version ) {
54
+ return isNonEmptyString ( version )
55
+ ? encodeURIComponent ( version ) . replace ( / % 3 A / g, ':' ) . replace ( / % 2 B / g, '+' )
56
+ : ''
66
57
}
67
58
68
59
function replacePlusSignWithPercentEncodedSpace ( str ) {
@@ -71,15 +62,10 @@ function replacePlusSignWithPercentEncodedSpace(str) {
71
62
}
72
63
73
64
module . exports = {
74
- encodeWithColonAndForwardSlash,
75
- encodeWithColonAndPlusSign,
76
- encodeWithForwardSlash,
77
65
encodeNamespace,
78
66
encodeVersion,
79
67
encodeQualifiers,
80
68
encodeQualifierParam,
81
69
encodeSubpath,
82
- encodeURIComponent,
83
- encodeURLSearchParam,
84
- encodeURLSearchParamWithPercentEncodedSpace
70
+ encodeURIComponent
85
71
}
0 commit comments