Created
April 19, 2019 00:30
-
-
Save fhammerschmidt/c1696f7416dfc394f80f8a8857782048 to your computer and use it in GitHub Desktop.
Draft bs-react-native (legacy) to bs-react-native-next converter
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
open Belt; | |
let teststring = {| | |
"container": | |
style([ | |
flex(1.0), | |
backgroundColor(Colors.darkBackground), | |
justifyContent(SpaceBetween), | |
alignItems(Center), | |
overflow(Hidden), | |
]), | |
"welcome": style([fontSize(Float(20.0)), marginBottom(Pt(10.0)), color(Colors.white)]), | |
"text": style([flex(1.0), backgroundColor(String("#EFEFF4"))]), | |
"errorText": | |
style([marginTop(Pt(5.)), marginBottom(Pt(10.)), fontSize(Float(15.)), color(String("red"))]), | |
"explanation": style([fontSize(Float(15.0)), marginBottom(Pt(20.0))]), | |
"textInput": | |
style([ | |
flex(1.0), | |
alignSelf(Stretch), | |
marginTop(Pt(1.0)), | |
marginRight(Pt(15.0)), | |
marginLeft(Pt(5.0)), | |
paddingVertical(Pt(Platform.os() == Platform.Android ? 6.0 : 0.0)), | |
fontSize(Float(17.0)), | |
color(Colors.white), | |
]), | |
"buttonContainer": style([marginTop(Pt(10.0)), alignItems(Center), marginVertical(Pt(10.0))]), | |
"contentContainer": style([marginTop(Pt(10.0))]), | |
|}; | |
let unsafeGetFromArray = (index, array) => | |
array->Array.get(index)->Option.getExn->Option.getExn; | |
let addCommaIfNeeded = str => | |
Js.String.split(",", str)->Array.get(0)->Option.getExn ++ ","; | |
let addApostropheIfNeeded = str => | |
if (str |> Js.String.charAt(0) === "\"") { | |
str ++ "\""; | |
} else { | |
"\"" ++ str ++ "\""; | |
}; | |
let makeContent = arr => { | |
let arr2 = | |
Js.Array.slice(~start=1, ~end_=Array.length(arr), arr) | |
->Array.mapWithIndex((i, item) => i === 0 ? item : "(" ++ item) | |
|> Js.Array.joinWith(""); | |
arr2 |> Js.String.substring(~from=0, ~to_=Js.String.length(arr2) - 1); | |
}; | |
let replaceText = text => { | |
let keywordConversions = [ | |
("FlexStart", "`flexStart"), | |
("FlexEnd", "`flexEnd"), | |
("Center", "`center"), | |
("Stretch", "`stretch"), | |
("SpaceAround", "`spaceAround"), | |
("SpaceBetween", "`spaceBetween"), | |
("SpaceEvenly", "`spaceEvenly"), | |
("Baseline", "`baseline"), | |
("Visible", "`visible"), | |
("Hidden", "`hidden"), | |
("Scroll", "`scroll"), | |
("Solid", "`solid"), | |
("Dotted", "`dotted"), | |
("Dashed", "`dashed"), | |
("RowReverse", "`rowReverse"), | |
("ColumnReverse", "`columnReverse"), | |
("Absolute", "`absolute"), | |
("Relative", "`relative"), | |
("Normal", "`normal"), | |
("Italic", "`italic"), | |
("Auto", "`auto"), | |
("Left", "`left"), | |
("Right", "`right"), | |
("Corner", "`corner"), | |
("Justify", "`justify"), | |
("Top", "`top"), | |
("Bottom", "`bottom"), | |
("Underline", "`underline"), | |
("LineThrough", "`lineThrough"), | |
("UnderlineLineThrough", "`underlineLineThrough"), | |
("Solid", "`solid"), | |
("Double", "`double"), | |
("Dotted", "`dotted"), | |
("Dashed", "`dashed"), | |
("None", "`none"), | |
("Uppercase", "`uppercase"), | |
("Lowercase", "`lowercase"), | |
("Capitalize", "`capitalize"), | |
("Cover", "`cover"), | |
("Contain", "`contain"), | |
("Repeat", "`repeat"), | |
]; | |
let text = ref(text); | |
keywordConversions->List.map(item => { | |
let (from, to_) = item; | |
text := text^ |> Js.String.replace(from, to_); | |
}); | |
text^; | |
}; | |
let trimmedText = | |
teststring | |
|> Js.String.replaceByRe( | |
Js.Re.fromStringWithFlags("[\s]+", ~flags="g"), | |
"", | |
) | |
|> Js.String.splitByRe(Js.Re.fromStringWithFlags(",\"", ~flags="g")); | |
let newStyle = | |
trimmedText->Array.map(segment => { | |
let segment = segment->Option.getExn; | |
let styleWrapper = | |
segment | |
|> Js.String.splitByRe(Js.Re.fromStringWithFlags("\":", ~flags="g")); | |
let styleDesc = | |
styleWrapper |> unsafeGetFromArray(0) |> addApostropheIfNeeded; | |
let style = styleWrapper |> unsafeGetFromArray(1); | |
let content = | |
style | |
|> Js.String.splitByRe( | |
Js.Re.fromStringWithFlags("style\(\[", ~flags="g"), | |
) | |
|> unsafeGetFromArray(1) | |
|> Js.String.splitByRe(Js.Re.fromStringWithFlags("\]\)", ~flags="g")) | |
|> unsafeGetFromArray(0) | |
|> Js.String.split(","); | |
let content = | |
Js.Array.slice(content, ~start=0, ~end_=Array.length(content)); | |
let newStyle = | |
content->Array.map(part => { | |
let style = part |> Js.String.split("("); | |
let styleName = style->Array.get(0)->Option.getExn; | |
let styleContent = makeContent(style); | |
let styleContent = | |
styleContent | |
|> Js.String.substring( | |
~from=0, | |
~to_=Js.String.length(styleContent), | |
) | |
|> replaceText; | |
if (styleContent !== "") { | |
"~" ++ styleName ++ "=" ++ styleContent->addCommaIfNeeded ++ " "; | |
} else { | |
""; | |
}; | |
}) | |
|> Js.Array.joinWith(""); | |
if (newStyle !== "") { | |
styleDesc ++ ": style(" ++ newStyle ++ "()),\n"; | |
} else { | |
""; | |
}; | |
}); | |
Js.log(newStyle |> Js.Array.joinWith("")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment