Last active
April 19, 2021 21:14
-
-
Save Guria/4be1d386421cbf178b7b570553f92648 to your computer and use it in GitHub Desktop.
Rescript port of urlon npm package https://github.com/cerebral/urlon/blob/122edaf551c4e84c1649f0e2aa22ad06f87b7dd1/lib/urlon.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%private(let keyStringifyRegexp = %re("/([=:@$/])/g")) | |
%%private(let valueStringifyRegexp = %re("/([&;/])/g")) | |
%%private(let keyParseRegexp = %re("/[=:@$]/")) | |
%%private(let valueParseRegexp = %re("/[&;]/")) | |
%%private( | |
let encodeString = (str, regexp) => | |
Js.Global.encodeURI(Js.String2.replaceByRe(str, regexp, "/$1")) | |
) | |
%%private( | |
let trim = (maybeRes: option<string>) => | |
Js.Option.map((. res: string) => Js.String2.replaceByRe(res, %re("/;+$/g"), ""), maybeRes) | |
) | |
type matcher = {maybeInput: option<Js.Json.t>, recursive: bool} | |
let rec stringify = (maybeInput: option<Js.Json.t>, ~recursive: bool=false, ()): option<string> => { | |
let match = {maybeInput: maybeInput, recursive: recursive} | |
switch match { | |
| {maybeInput: None} => None | |
| {recursive: false} => trim(stringify(maybeInput, ~recursive=true, ())) | |
| {maybeInput: Some(input)} => | |
switch Js.Json.classify(input) { | |
| Js.Json.JSONArray(arr) => | |
Js.Array2.map(arr, x => { | |
switch Js.Nullable.toOption(Js.Nullable.return(x)) { | |
| None => Some(":null") | |
| Some(_) => stringify(Some(x), ~recursive=true, ()) | |
} | |
}) | |
->Js.Array2.joinWith("&") | |
->(str => Some("@" ++ str ++ ";")) | |
| Js.Json.JSONObject(obj) => { | |
let arr: Js.Array2.t<string> = [] | |
Js.Array2.forEach(Js.Dict.entries(obj), ((key, value)) => { | |
if Js.Nullable.return(value) != Js.Nullable.undefined { | |
switch stringify(Some(value), ~recursive=true, ()) { | |
| None => () | |
| Some(val) => | |
(Js.Array2.push(arr, encodeString(key, keyStringifyRegexp) ++ val): int)->ignore | |
} | |
} | |
}) | |
arr->Js.Array2.joinWith("&")->(str => Some("$" ++ str ++ ";")) | |
} | |
| Js.Json.JSONString(str) => Some("=" ++ encodeString(str, valueStringifyRegexp)) | |
| Js.Json.JSONNumber(_) | |
| Js.Json.JSONTrue | |
| Js.Json.JSONFalse | |
| Js.Json.JSONNull => | |
Some(j`:$input`) | |
} | |
} | |
} | |
let parse = (input: string): Js.Json.t => { | |
let pos = ref(0) | |
let str = Js.Global.decodeURI(input) | |
let readChar = () => Js.String2.charAt(str, pos.contents) | |
let seek = () => pos.contents = pos.contents + 1 | |
let isReachedEnd = () => pos.contents >= Js.String2.length(str) | |
let isReachedSeparator = () => isReachedEnd() || readChar() == ";" | |
let readToken = (regexp: Js_re.t): string => { | |
let token = ref("") | |
let break = ref(false) | |
while !break.contents { | |
switch readChar() { | |
| "/" => { | |
seek() | |
if isReachedEnd() { | |
token.contents = token.contents ++ ";" | |
break.contents = true | |
} | |
} | |
| char => | |
if Js.Re.test_(regexp, char) { | |
break.contents = true | |
} | |
} | |
if !break.contents { | |
token.contents = token.contents ++ readChar() | |
seek() | |
} | |
if isReachedEnd() { | |
break.contents = true | |
} | |
} | |
token.contents | |
} | |
let rec parseToken = (): Js.Json.t => { | |
let tokenType = readChar() | |
seek() | |
switch tokenType { | |
| "=" => Js.Json.string(readToken(valueParseRegexp)) | |
| ":" => | |
switch readToken(valueParseRegexp) { | |
| "true" => Js.Json.boolean(true) | |
| "false" => Js.Json.boolean(false) | |
| token => { | |
let value = Js.Float.fromString(token) | |
Js.Float.isNaN(value) ? Js.Json.null : Js.Json.number(value) | |
} | |
} | |
| "@" => { | |
let res: Js.Array2.t<Js.Json.t> = [] | |
let break = ref(false) | |
if isReachedSeparator() { | |
break.contents = true | |
} | |
while !break.contents { | |
(Js.Array2.push(res, parseToken()): int)->ignore | |
if isReachedSeparator() { | |
break.contents = true | |
} | |
if !break.contents { | |
seek() | |
} | |
} | |
seek() | |
Js.Json.array(res) | |
} | |
| "$" => { | |
let res: Js.Dict.t<Js.Json.t> = Js.Dict.empty() | |
let break = ref(false) | |
if isReachedSeparator() { | |
break.contents = true | |
} | |
while !break.contents { | |
let name = readToken(keyParseRegexp) | |
Js.Dict.set(res, name, parseToken()) | |
if isReachedSeparator() { | |
break.contents = true | |
} | |
if !break.contents { | |
seek() | |
} | |
} | |
seek() | |
Js.Json.object_(res) | |
} | |
| _ => Js.Exn.raiseError("Unknown type: " ++ tokenType) | |
} | |
} | |
parseToken() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
compiled result (formatted with prettier):