Created
January 28, 2019 23:29
-
-
Save xdaDaveShaw/a21142852d1bf9aff772b07e2523a946 to your computer and use it in GitHub Desktop.
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 System | |
let checkLength s = | |
let len = Seq.length s | |
len >= 6 && len <= 16 | |
let checkDigits s = | |
let checkDigit c = Char.IsLetterOrDigit(c) || c = '-' | |
s | |
|> Seq.forall checkDigit | |
let startsWithLetter s = | |
s |> Seq.head |> Char.IsLetter | |
let doesNotEndWithHypen s = | |
s |> Seq.last <> '-' | |
let containsOnlyOneHyphen s = | |
s |> Seq.filter (fun c -> c = '-') |> Seq.length <= 1 | |
let validate s = | |
let checks = [ | |
checkLength | |
checkDigits | |
startsWithLetter | |
doesNotEndWithHypen | |
containsOnlyOneHyphen ] | |
checks | |
|> Seq.map (fun check -> check s) | |
|> Seq.forall id | |
let test s expected = | |
let actual = validate s | |
let ok = if actual = expected then "√" else "⌧" | |
printfn "%s Validated '%s' expected: '%b' actual: '%b'" ok s expected actual | |
test "" false | |
test "abc" false | |
test "abcdef" true | |
test "abcdefabcdefabcd" true | |
test "abcdefabcdefabcdef" false | |
test "dave123" true | |
test "dave!shaw" false | |
test "dave;'#" false | |
test "abc-def" true | |
test "abc--def" false | |
test "daveshaw-" false | |
test "dave shaw" false | |
test "1daveshaw" false | |
test "-daveshaw" false | |
test "DaveShaw" true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment