@jsenv/urls 1.2.0 → 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/common_pathname.js +28 -34
- package/src/move_url.js +5 -1
package/package.json
CHANGED
package/src/common_pathname.js
CHANGED
|
@@ -1,42 +1,36 @@
|
|
|
1
1
|
export const getCommonPathname = (pathname, otherPathname) => {
|
|
2
|
-
|
|
3
|
-
pathname,
|
|
4
|
-
otherPathname,
|
|
5
|
-
)
|
|
6
|
-
|
|
7
|
-
// pathname and otherpathname are exactly the same
|
|
8
|
-
if (firstDifferentCharacterIndex === -1) {
|
|
2
|
+
if (pathname === otherPathname) {
|
|
9
3
|
return pathname
|
|
10
4
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
// the first different char is at firstDifferentCharacterIndex
|
|
14
|
-
if (pathname.charAt(firstDifferentCharacterIndex) === "/") {
|
|
15
|
-
return commonString
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
if (otherPathname.charAt(firstDifferentCharacterIndex) === "/") {
|
|
19
|
-
return commonString
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const firstDifferentSlashIndex = commonString.lastIndexOf("/")
|
|
23
|
-
return pathname.slice(0, firstDifferentSlashIndex + 1)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const findFirstDifferentCharacterIndex = (string, otherString) => {
|
|
27
|
-
const maxCommonLength = Math.min(string.length, otherString.length)
|
|
5
|
+
let commonPart = ""
|
|
6
|
+
let commonPathname = ""
|
|
28
7
|
let i = 0
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
8
|
+
const length = pathname.length
|
|
9
|
+
const otherLength = otherPathname.length
|
|
10
|
+
while (i < length) {
|
|
11
|
+
const char = pathname.charAt(i)
|
|
12
|
+
const otherChar = otherPathname.charAt(i)
|
|
35
13
|
i++
|
|
14
|
+
if (char === otherChar) {
|
|
15
|
+
if (char === "/") {
|
|
16
|
+
commonPart += "/"
|
|
17
|
+
commonPathname += commonPart
|
|
18
|
+
commonPart = ""
|
|
19
|
+
} else {
|
|
20
|
+
commonPart += char
|
|
21
|
+
}
|
|
22
|
+
} else {
|
|
23
|
+
if (char === "/" && i - 1 === otherLength) {
|
|
24
|
+
commonPart += "/"
|
|
25
|
+
commonPathname += commonPart
|
|
26
|
+
}
|
|
27
|
+
return commonPathname
|
|
28
|
+
}
|
|
36
29
|
}
|
|
37
|
-
if (
|
|
38
|
-
|
|
30
|
+
if (length === otherLength) {
|
|
31
|
+
commonPathname += commonPart
|
|
32
|
+
} else if (otherPathname.charAt(i) === "/") {
|
|
33
|
+
commonPathname += commonPart
|
|
39
34
|
}
|
|
40
|
-
|
|
41
|
-
return maxCommonLength
|
|
35
|
+
return commonPathname
|
|
42
36
|
}
|
package/src/move_url.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { urlToRelativeUrl } from "./url_to_relative_url.js"
|
|
2
2
|
|
|
3
3
|
export const moveUrl = ({ url, from, to, preferAbsolute = false }) => {
|
|
4
|
-
|
|
4
|
+
let relativeUrl = urlToRelativeUrl(url, from)
|
|
5
|
+
if (relativeUrl.slice(0, 2) === "//") {
|
|
6
|
+
// restore the protocol
|
|
7
|
+
relativeUrl = new URL(relativeUrl, url).href
|
|
8
|
+
}
|
|
5
9
|
const absoluteUrl = new URL(relativeUrl, to).href
|
|
6
10
|
if (preferAbsolute) {
|
|
7
11
|
return absoluteUrl
|