@jsenv/urls 1.2.0 → 1.2.1

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.1",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,42 +1,32 @@
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
8
+ const length = pathname.length
9
+ const otherLength = otherPathname.length
10
+ while (i < pathname.length) {
11
+ const char = pathname.charAt(i)
12
+ const otherChar = otherPathname.charAt(i)
13
+ if (char === otherChar) {
14
+ if (char === "/") {
15
+ commonPart += "/"
16
+ commonPathname += commonPart
17
+ commonPart = ""
18
+ } else {
19
+ commonPart += char
20
+ }
21
+ } else {
22
+ return commonPathname
34
23
  }
35
24
  i++
36
25
  }
37
- if (string.length === otherString.length) {
38
- return -1
26
+ if (length === otherLength) {
27
+ commonPathname += commonPart
28
+ } else if (otherPathname.charAt(i) === "/") {
29
+ commonPathname += commonPart
39
30
  }
40
- // they differ at maxCommonLength
41
- return maxCommonLength
31
+ return commonPathname
42
32
  }