Skip to content

Commit 6bb130e

Browse files
committed
Remove reduce in Url parsing
1 parent 6898edd commit 6bb130e

File tree

2 files changed

+42
-70
lines changed

2 files changed

+42
-70
lines changed

src/common/Url.mjs

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22

33
import * as Util from "./Util.mjs";
44
import * as Belt_Array from "rescript/lib/es6/belt_Array.js";
5-
import * as Belt_Option from "rescript/lib/es6/belt_Option.js";
6-
import * as Caml_option from "rescript/lib/es6/caml_option.js";
7-
8-
function isVersion(str) {
9-
return Belt_Option.isSome(Caml_option.null_to_opt(str.match(/latest|v\d+(\.\d+)?(\.\d+)?/)));
10-
}
5+
import * as Caml_array from "rescript/lib/es6/caml_array.js";
116

127
function prettyString(str) {
138
return Util.$$String.capitalize(Util.$$String.camelCase(str));
@@ -17,44 +12,32 @@ function parse(route) {
1712
var fullpath = Belt_Array.keep(route.split("/"), (function (s) {
1813
return s !== "";
1914
}));
20-
var match = Belt_Array.reduce(fullpath, [
21-
[],
22-
/* NoVersion */1,
23-
[]
24-
], (function (acc, next) {
25-
var pagepath = acc[2];
26-
var version = acc[1];
27-
var base = acc[0];
28-
if (version === /* NoVersion */1) {
29-
if (isVersion(next)) {
30-
var version$1 = next === "latest" ? /* Latest */0 : ({
31-
_0: next,
32-
[Symbol.for("name")]: "Version"
33-
});
34-
return [
35-
base,
36-
version$1,
37-
pagepath
38-
];
39-
}
40-
var base$1 = Belt_Array.concat(base, [next]);
41-
return [
42-
base$1,
43-
version,
44-
pagepath
45-
];
46-
}
47-
var pagepath$1 = Belt_Array.concat(pagepath, [next]);
48-
return [
49-
base,
50-
version,
51-
pagepath$1
52-
];
53-
}));
15+
var foundVersionIndex = fullpath.findIndex(function (chunk) {
16+
return /latest|v\d+(\.\d+)?(\.\d+)?/.test(chunk);
17+
});
18+
var match;
19+
if (foundVersionIndex === -1) {
20+
match = [
21+
/* NoVersion */1,
22+
fullpath,
23+
[]
24+
];
25+
} else {
26+
var v = Caml_array.get(fullpath, foundVersionIndex);
27+
var version = v === "latest" ? /* Latest */0 : ({
28+
_0: v,
29+
[Symbol.for("name")]: "Version"
30+
});
31+
match = [
32+
version,
33+
fullpath.slice(0, foundVersionIndex),
34+
fullpath.slice(foundVersionIndex + 1 | 0, fullpath.length)
35+
];
36+
}
5437
return {
5538
fullpath: fullpath,
56-
base: match[0],
57-
version: match[1],
39+
base: match[1],
40+
version: match[0],
5841
pagepath: match[2]
5942
};
6043
}

src/common/Url.res

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type version =
2121
Results in:
2222
fullpath: ["apis"]
2323
base: ["apis"]
24-
version: None
24+
version: NoVersion
2525
pagepath: []
2626
*/
2727

@@ -48,42 +48,31 @@ type breadcrumb = {
4848
href: string,
4949
}
5050

51-
let isVersion = str =>
52-
Js.String2.match_(str, %re("/latest|v\\d+(\\.\\d+)?(\\.\\d+)?/"))->Belt.Option.isSome
53-
5451
/* Beautifies url based string to somewhat acceptable representation */
5552
let prettyString = (str: string) => {
5653
open Util.String
5754
str->camelCase->capitalize
5855
}
5956

6057
let parse = (route: string): t => {
61-
let fullpath = {
62-
open Js.String2
63-
route->split("/")->Belt.Array.keep(s => s !== "")
64-
}
65-
66-
let (base, version, pagepath) = Belt.Array.reduce(fullpath, ([], NoVersion, []), (acc, next) => {
67-
let (base, version, pagepath) = acc
68-
69-
if version === NoVersion {
70-
if isVersion(next) {
71-
let version = if next === "latest" {
72-
Latest
73-
} else {
74-
Version(next)
75-
}
76-
(base, version, pagepath)
77-
} else {
78-
let base = Belt.Array.concat(base, [next])
79-
(base, version, pagepath)
80-
}
81-
} else {
82-
let pagepath = Belt.Array.concat(pagepath, [next])
58+
let fullpath = route->Js.String2.split("/")->Belt.Array.keep(s => s !== "")
59+
let foundVersionIndex = Js.Array2.findIndex(fullpath, chunk => {
60+
Js.Re.test_(%re(`/latest|v\d+(\.\d+)?(\.\d+)?/`), chunk)
61+
})
8362

84-
(base, version, pagepath)
63+
let (version, base, pagepath) = if foundVersionIndex == -1 {
64+
(NoVersion, fullpath, [])
65+
} else {
66+
let version = switch fullpath[foundVersionIndex] {
67+
| "latest" => Latest
68+
| v => Version(v)
8569
}
86-
})
70+
(
71+
version,
72+
fullpath->Js.Array2.slice(~start=0, ~end_=foundVersionIndex),
73+
fullpath->Js.Array2.slice(~start=foundVersionIndex + 1, ~end_=Js.Array2.length(fullpath)),
74+
)
75+
}
8776

8877
{fullpath: fullpath, base: base, version: version, pagepath: pagepath}
8978
}

0 commit comments

Comments
 (0)