Safe Haskell | None |
---|---|
Language | Haskell2010 |
Common predicates for use with Refined, Refined2, and Refined3
Synopsis
- type DateNip = ParseTimes Day DateFmts Id
- type DateFmts = '["%Y-%m-%d", "%m/%d/%y", "%B %d %Y"]
- type DateTimeFmts = '["%Y-%m-%d %H:%M:%S", "%m/%d/%y %H:%M:%S", "%B %d %Y %H:%M:%S", "%Y-%m-%dT%H:%M:%S"]
- type DateTimeNip = ParseTimes UTCTime DateTimeFmts Id
- type Dtip t = ParseTimeP t "%F %T"
- type Dtfmt = FormatTimeP "%F %T"
- type DdmmyyyyRE = "^(\\d{2})-(\\d{2})-(\\d{4})$"
- type Ddmmyyyyop = GuardsDetail "%s %d is out of range" '['("day", Between 1 31 Id), '("month", Between 1 12 Id), '("year", Between 1990 2050 Id)] >> 'True
- type Ddmmyyyyop' = Bools '['("day", Between 1 31 Id), '("month", Between 1 12 Id), '("year", Between 1990 2050 Id)]
- type JsonMicrosoftDateTime = Do '[Rescan "^Date\\(([-+])?(\\d*?)(\\d{0,3})([+-]\\d{4})\\)", Head, Snd, If ((Id !! 0) == "-") "-" "" <> (PadL 1 (C "0") (Id !! 1) <> ("." <> (PadL 3 (C "0") (Id !! 2) <> (Id !! 3)))), ParseTimeP ZonedTime "%s%Q%z"]
- type Hmsip = Map' (ReadP Int Id) (Resplit ":")
- type Hmsop = GuardsDetail "%s invalid: found %d" '['("hours", Between 0 23 Id), '("minutes", Between 0 59 Id), '("seconds", Between 0 59 Id)] >> 'True
- type Hmsop' = Bools '['("hours", Between 0 23 Id), '("minutes", Between 0 59 Id), '("seconds", Between 0 59 Id)]
- type Hmsfmt = PrintL 3 "%02d:%02d:%02d" Id
- type HmsRE = "^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$"
- type Luhnip = Map' (ReadP Int Id) (Remove "-" Id >> Ones)
- type Luhnop (n :: Nat) = GuardBool (PrintT "expected %d digits but found %d" '(n, Len)) (Len == n) && GuardBool "invalid checkdigit" IsLuhn
- type Luhnfmt (ns :: [Nat]) = ConcatMap (ShowP Id) Id >> (SplitAts ns Id >> (Intercalate '["-"] Id >> Concat))
- type Luhn' (n :: Nat) = Msg "Luhn'" (Do '[Guard (PrintT "incorrect length: found %d but expected %d in [%s]" '(Len, n, Id)) (Len == n), Do '[Ones, Map (ReadP Int Id), Reverse, ZipWith ((Fst * Snd) >> If (Id >= 10) (Id - 9) Id) (Cycle n [1, 2]) Id, Sum], Guard (PrintT "expected %d mod 10 = 0 but found %d" '(Id, Id `Mod` 10)) (Mod Id 10 == 0)])
- type Luhnop' (n :: Nat) = Guard (PrintT "incorrect number of digits found %d but expected %d in [%s]" '(Len, n, ShowP Id)) (Len == n) >> (Do '[Reverse, ZipWith ((Fst * Snd) >> If (Id >= 10) (Id - 9) Id) (Cycle n [1, 2]) Id, Sum] >> Guard (PrintT "expected %d mod 10 = 0 but found %d" '(Id, Id `Mod` 10)) (Mod Id 10 == 0))
- type Ssnip = Map' (ReadP Int Id) (Rescan "^(\\d{3})-(\\d{2})-(\\d{4})$" >> (OneP >> Snd))
- type Ssnop = BoolsQuick (PrintT "number for group %d invalid: found %d" Id) '[Between 1 899 Id && (Id /= 666), Between 1 99 Id, Between 1 9999 Id]
- type Ssnfmt = PrintL 3 "%03d-%02d-%04d" Id
- type Ip4ip = Map' (ReadP Int Id) (Resplit "\\.")
- type Ip4ip' = Map' (ReadP Int Id) (Rescan Ip4RE >> (OneP >> Snd))
- type Ip4op = GuardsN 4 (PrintT "octet %d out of range 0-255 found %d" Id) (0 <..> 255) >> 'True
- type Ip4op' = BoolsN (PrintT "octet %d out of range 0-255 found %d" Id) 4 (0 <..> 255)
- type Ip4fmt = PrintL 4 "%03d.%03d.%03d.%03d" Id
- type OctetRE = "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])"
- type Ip4RE = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$"
- type Ip4StrictRE = "^" <%> (IntersperseT "\\." (RepeatT 4 OctetRE) <%> "$")
- type Ip6ip = Resplit ":" >> (Map (If (Id == "") "0" Id) >> (Map (ReadBase Int 16) >> PadL 8 0 Id))
- type Ip6op = Msg "count is bad:" (Len == 8) && Msg "out of bounds:" (All (0 <..> 65535))
- type Ip6fmt = PrintL 8 "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x" Id
- type Isbn10ip = Resplit "-" >> (Concat >> ('Just Unsnoc >> (Map (ReadP Int (Singleton Id)) *** If (Singleton Id ==~ "X") 10 (ReadP Int (Singleton Id)))))
- type Isbn10op = GuardSimple ((Fst >> All (0 <..> 9)) && Between 0 10 Snd) >> (ZipWith (Fst * Snd) ((1 ... 10) >> Reverse) (Fst +: Snd) >> (Sum >> GuardBool "mod 0 oops" ((Id `Mod` 11) == 0)))
- type Isbn10fmt = (ConcatMap (ShowP Id) Id *** If (Id == 10) "X" (ShowP Id)) >> (Fst <> ("-" <> Snd))
- type Isbn13ip = Resplit "-" >> (Concat >> Map (ReadP Int (Singleton Id)))
- type Isbn13op = ZipWith (Fst * Snd) (Cycle 13 [1, 3] >> Reverse) Id >> (Sum >> ('(Id, Id `Mod` 10) >> GuardBool (PrintT "sum=%d mod 10=%d" Id) (Snd == 0)))
- type Isbn13fmt = 'Just Unsnoc >> (ConcatMap (ShowP Id) Fst <> ("-" <> ShowP Snd))
datetime
type DateNip = ParseTimes Day DateFmts Id Source #
ip
type for reading one of many date formats from DateFmts
type DateFmts = '["%Y-%m-%d", "%m/%d/%y", "%B %d %Y"] Source #
selected date formats used in the examples
type DateTimeFmts = '["%Y-%m-%d %H:%M:%S", "%m/%d/%y %H:%M:%S", "%B %d %Y %H:%M:%S", "%Y-%m-%dT%H:%M:%S"] Source #
selected datetime formats used in the examples
type DateTimeNip = ParseTimes UTCTime DateTimeFmts Id Source #
ip
type for reading one of many date time formats from DateTimeFmts
type Dtip t = ParseTimeP t "%F %T" Source #
ip
type for reading in a date time
type Dtfmt = FormatTimeP "%F %T" Source #
fmt
type for formatting the date time compatible ith Dtip
type DdmmyyyyRE = "^(\\d{2})-(\\d{2})-(\\d{4})$" Source #
simple date regular expression
type Ddmmyyyyop = GuardsDetail "%s %d is out of range" '['("day", Between 1 31 Id), '("month", Between 1 12 Id), '("year", Between 1990 2050 Id)] >> 'True Source #
date validator for a three value list containing day month and year using GuardsDetail
type Ddmmyyyyop' = Bools '['("day", Between 1 31 Id), '("month", Between 1 12 Id), '("year", Between 1990 2050 Id)] Source #
date validator for a three value list containing day month and year using Bools
type JsonMicrosoftDateTime = Do '[Rescan "^Date\\(([-+])?(\\d*?)(\\d{0,3})([+-]\\d{4})\\)", Head, Snd, If ((Id !! 0) == "-") "-" "" <> (PadL 1 (C "0") (Id !! 1) <> ("." <> (PadL 3 (C "0") (Id !! 2) <> (Id !! 3)))), ParseTimeP ZonedTime "%s%Q%z"] Source #
convert json microsoft datetime to zonedtime
>>>
pz @JsonMicrosoftDateTime "Date(1593460089052+0800)"
Val 2020-06-30 03:48:09.052 +0800
>>>
pz @JsonMicrosoftDateTime "Date(0+0800)"
Val 1970-01-01 08:00:00 +0800
>>>
pz @JsonMicrosoftDateTime "Date(12+0800)"
Val 1970-01-01 08:00:00.012 +0800
>>>
pz @JsonMicrosoftDateTime "Date(123+0800)"
Val 1970-01-01 08:00:00.123 +0800
>>>
pz @JsonMicrosoftDateTime "Date(+1234+0800)"
Val 1970-01-01 08:00:01.234 +0800
>>>
pz @JsonMicrosoftDateTime "Date(-123456+0000)"
Val 1969-12-31 23:57:57.456 +0000
time
type Hmsop = GuardsDetail "%s invalid: found %d" '['("hours", Between 0 23 Id), '("minutes", Between 0 59 Id), '("seconds", Between 0 59 Id)] >> 'True Source #
op
type for validating the time using a guard
type Hmsop' = Bools '['("hours", Between 0 23 Id), '("minutes", Between 0 59 Id), '("seconds", Between 0 59 Id)] Source #
op
type for validating the time using predicate
type HmsRE = "^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$" Source #
regular expression for a time component
luhn check
type Luhnip = Map' (ReadP Int Id) (Remove "-" Id >> Ones) Source #
ip
type for converting a credit card number to a list of singleton digits
type Luhnop (n :: Nat) = GuardBool (PrintT "expected %d digits but found %d" '(n, Len)) (Len == n) && GuardBool "invalid checkdigit" IsLuhn Source #
op
type for validating a credit card number by check digit
type Luhnfmt (ns :: [Nat]) = ConcatMap (ShowP Id) Id >> (SplitAts ns Id >> (Intercalate '["-"] Id >> Concat)) Source #
fmt
type for formatting a credit card using ns
as the format
type Luhn' (n :: Nat) = Msg "Luhn'" (Do '[Guard (PrintT "incorrect length: found %d but expected %d in [%s]" '(Len, n, Id)) (Len == n), Do '[Ones, Map (ReadP Int Id), Reverse, ZipWith ((Fst * Snd) >> If (Id >= 10) (Id - 9) Id) (Cycle n [1, 2]) Id, Sum], Guard (PrintT "expected %d mod 10 = 0 but found %d" '(Id, Id `Mod` 10)) (Mod Id 10 == 0)]) Source #
luhn check digit validator (alternate version)
type Luhnop' (n :: Nat) = Guard (PrintT "incorrect number of digits found %d but expected %d in [%s]" '(Len, n, ShowP Id)) (Len == n) >> (Do '[Reverse, ZipWith ((Fst * Snd) >> If (Id >= 10) (Id - 9) Id) (Cycle n [1, 2]) Id, Sum] >> Guard (PrintT "expected %d mod 10 = 0 but found %d" '(Id, Id `Mod` 10)) (Mod Id 10 == 0)) Source #
luhn check digit validator
ssn
type Ssnip = Map' (ReadP Int Id) (Rescan "^(\\d{3})-(\\d{2})-(\\d{4})$" >> (OneP >> Snd)) Source #
ip
type for reading in a ssn
type Ssnop = BoolsQuick (PrintT "number for group %d invalid: found %d" Id) '[Between 1 899 Id && (Id /= 666), Between 1 99 Id, Between 1 9999 Id] Source #
op
type for validating a ssn
type Ssnfmt = PrintL 3 "%03d-%02d-%04d" Id Source #
fmt
type for formatting the ssn compatible with Ssnip
ipv4
type Ip4ip' = Map' (ReadP Int Id) (Rescan Ip4RE >> (OneP >> Snd)) Source #
ip
type for reading in an ip4 address using a regular expression
type Ip4op = GuardsN 4 (PrintT "octet %d out of range 0-255 found %d" Id) (0 <..> 255) >> 'True Source #
op
type for validating an ip4 address using a guard
type Ip4op' = BoolsN (PrintT "octet %d out of range 0-255 found %d" Id) 4 (0 <..> 255) Source #
op
type for validating an ip4 address using a predicate
type OctetRE = "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])" Source #
regular expression for an octet
type Ip4RE = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$" Source #
regular expression for an ip4 address
type Ip4StrictRE = "^" <%> (IntersperseT "\\." (RepeatT 4 OctetRE) <%> "$") Source #
regular expression for an ip4 address
ipv6
type Ip6ip = Resplit ":" >> (Map (If (Id == "") "0" Id) >> (Map (ReadBase Int 16) >> PadL 8 0 Id)) Source #
ip
type for reading in an ip6 address
type Ip6op = Msg "count is bad:" (Len == 8) && Msg "out of bounds:" (All (0 <..> 65535)) Source #
op
type for validating an ip6 address using predicates
type Ip6fmt = PrintL 8 "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x" Id Source #
fmt
type for formatting an ip6 address
isbn10
type Isbn10ip = Resplit "-" >> (Concat >> ('Just Unsnoc >> (Map (ReadP Int (Singleton Id)) *** If (Singleton Id ==~ "X") 10 (ReadP Int (Singleton Id))))) Source #
isbn 10 converter
type Isbn10op = GuardSimple ((Fst >> All (0 <..> 9)) && Between 0 10 Snd) >> (ZipWith (Fst * Snd) ((1 ... 10) >> Reverse) (Fst +: Snd) >> (Sum >> GuardBool "mod 0 oops" ((Id `Mod` 11) == 0))) Source #
isbn 10 validator
type Isbn10fmt = (ConcatMap (ShowP Id) Id *** If (Id == 10) "X" (ShowP Id)) >> (Fst <> ("-" <> Snd)) Source #
isbn 10 formatter