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

View File

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