An experimental safe and functional API for JavaScript regexes
/**
* Reason
*
* Dasherize camelCased identifiers inside string literals
*/
let code = {|
let borderLeftColor = "borderLeftColor";
let borderRightColor = "borderRightColor";
|};
code |> Revamp.replace({|"([^"]+)"|}, /* Matches the content of string literals */
Revamp.replace("[A-Z]", letter => /* Matches uppercase letters */
"-" ++ letter |> Js.String.toLowerCase)) /* Convert to lower case and prefix with a dash */
|> Js.log;
/* Outputs:
let borderLeftColor = "border-left-color";
let borderRightColor = "border-right-color";
*/
let code =
{|
let borderLeftColor = "borderLeftColor";
let borderRightColor = "borderRightColor";
|}
[@@ocaml.doc
"\n * Reason\n *\n * Dasherize camelCased identifiers inside string literals\n "]
let _ =
(code |>
(Revamp.replace {|"([^"]+)"|}
(Revamp.replace "[A-Z]"
(fun letter -> ("-" ^ letter) |> Js.String.toLowerCase))))
|> Js.log
/**
* OCaml
*
* Dasherize camelCased identifiers inside string literals
*/;
let code = {|
let borderLeftColor = "borderLeftColor";
let borderRightColor = "borderRightColor";
|};
let () =
code
|> Revamp.replace(
{|"([^"]*)"|}, /* Matches the content of string literals */
Revamp.replace("[A-Z]", letter
/* Matches uppercase letters */
=> "-" ++ letter |> Js.String.toLowerCase),
) /* Convert to lower case and prefix with a dash */
|> Js.log;
/* Outputs:
let borderLeftColor = "border-left-color";
let borderRightColor = "border-right-color";
*/
(**
* OCaml
*
* Dasherize camelCased identifiers inside string literals
*)
let code = {|
let borderLeftColor = "borderLeftColor";
let borderRightColor = "borderRightColor";
|}
let () =
code |> Revamp.replace {|"([^"]*)"|} (* Matches the content of string literals *)
(Revamp.replace "[A-Z]" (fun letter -> (* Matches uppercase letters *)
"-" ^ letter |> Js.String.toLowerCase)) (* Convert to lower case and prefix with a dash *)
|> Js.log
(* Outputs:
let borderLeftColor = "border-left-color";
let borderRightColor = "border-right-color";
*)
npm install --save @glennsl/bs-revamp
Then add @glennsl/bs-revamp
to bs-dependencies
in your bsconfig.json
:
{
...
"bs-dependencies": ["@glennsl/bs-revamp"]
}
For the moment, please see the interface file, Revamp.rei.
captures
from Sequence.t(array(string))
to Sequence.t(list(option(string)))
because the former was unsound.Match.match
and Match.captures
Match.matches
due to being unsound