module RuleTest exposing (..)
import Html.App
import Html exposing (..)
import Html.Attributes exposing (..)
import Regex
main : Platform.Program Basics.Never
main =
Html.App.program
{ init = init
, view = viewRuleTest
, update = update
, subscriptions = \_ -> Sub.none
}
type alias Rule =
{ regex : String
, substitution : String
}
type alias RuleTest =
{ rule : Rule
, input : String
, output : String
, matched : Bool
}
type Msg
= StartTest String
-- init
init : (RuleTest, Cmd Msg)
init =
( apply "reg1" (Rule "reg1" "sub1")
, Cmd.none
)
-- update
update : Msg -> RuleTest -> (RuleTest, Cmd Msg)
update msg ruleTest =
case msg of
StartTest input ->
( apply input ruleTest.rule, Cmd.none )
apply : String -> Rule -> RuleTest
apply input rule =
let
output = replace rule.regex rule.substitute input
matched = input /= output
rule = rule
in
RuleTest rule input output True
replace : String -> String -> String -> String
replace regex substitute input =
Regex.replace Regex.All (Regex.regex regex) (\_ -> substitute) input
viewRuleTest : RuleTest -> Html Msg
viewRuleTest ruleTest =
div []
[ label [] [ text "Pattern" ]
, input [ Html.Attributes.value ruleTest.rule.regex, disabled True ] []
, label [] [ text "Substitution" ]
, input [ Html.Attributes.value ruleTest.rule.substitution, disabled True ] []
, label [] [ text "Input" ]
, input [ Html.Attributes.value ruleTest.input, disabled True ] []
, label [] [ text "Output" ]
, input [ Html.Attributes.value ruleTest.output, disabled True ] []
, label [] [ text "matched" ]
, input [ type' "checkbox", checked ruleTest.matched ] []
]