@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/urls",
3
- "version": "1.2.0",
3
+ "version": "1.2.3",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,42 +1,36 @@
1
1
  export const getCommonPathname = (pathname, otherPathname) => {
2
- const firstDifferentCharacterIndex = findFirstDifferentCharacterIndex(
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
- const commonString = pathname.slice(0, firstDifferentCharacterIndex + 1)
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
- while (i < maxCommonLength) {
30
- const char = string.charAt(i)
31
- const otherChar = otherString.charAt(i)
32
- if (char !== otherChar) {
33
- return i
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 (string.length === otherString.length) {
38
- return -1
30
+ if (length === otherLength) {
31
+ commonPathname += commonPart
32
+ } else if (otherPathname.charAt(i) === "/") {
33
+ commonPathname += commonPart
39
34
  }
40
- // they differ at maxCommonLength
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
- const relativeUrl = urlToRelativeUrl(url, from)
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