More simplify code

This commit is contained in:
Anton Vakhrushev 2017-11-14 20:55:58 +03:00
parent 07d25c7e44
commit 06661e173e
2 changed files with 7 additions and 17 deletions

View File

@ -3,7 +3,6 @@ module Pattern
, match
, parse
, check
, createFields
) where
import Constraint
@ -27,12 +26,12 @@ match s d =
Nothing -> Nothing
parse :: String -> Maybe Pattern
parse s
| isInvalid = Nothing
| otherwise = Just (createPattern $ catMaybes parts)
parse text
| isValid = Just (createPattern $ catMaybes fields)
| otherwise = Nothing
where
parts = createFields s
isInvalid = not (checkParts parts)
fields = zipWith parseField (words text) constraints
isValid = checkFields fields
createPattern xs =
Pattern
{ cminute = head xs
@ -43,20 +42,12 @@ parse s
, cyear = xs !! 5
}
createFields :: String -> [Maybe Field]
createFields text = zipWith f constraints (words text)
where
f constraint word = parseField word constraint
checkParts :: [Maybe Field] -> Bool
checkParts xs
checkFields :: [Maybe Field] -> Bool
checkFields xs
| length xs /= 6 = False
| any isNothing xs = False
| otherwise = True
parseFieldAdapter :: Constraint -> String -> Maybe Field
parseFieldAdapter constraint text = parseField text constraint
constrainMinute :: Constraint
constrainMinute = Constraint 0 59

View File

@ -14,7 +14,6 @@ main = hspec spec
spec :: Spec
spec =
describe "Cron pattern" $ do
it "createFields" $ length (createFields "* * * * * *") `shouldBe` 6
it "matches fixed time" $
let ptn = "* * * * * *"
date = DateTime 2017 10 11 0 0 0