diff options
author | Yorhel <git@yorhel.nl> | 2019-07-25 14:30:04 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2019-07-25 14:36:21 +0200 |
commit | f296495a912ce759df11c43e78b4552788bdbff2 (patch) | |
tree | 0c10802de65fb7c8475722e12234bff5eb980628 /elm3/Lib/Util.elm | |
parent | 0f3cfeb85caec6424bcbea47142eefbf8011636b (diff) |
Merge the v3 branch into separate namespace + fix Docker stuff (again)
I was getting tired of having to keep two branches up-to-date with the
latest developments, so decided to throw v3 into the same branch - just
different files (...which will get mostly rewritten again soon). The two
versions aren't very different in terms of dependencies, build system
and support code, so they can now properly share files. Added a section
to the README to avoid confusion.
This merge also makes it easier to quickly switch between the different
versions, which is handy for development. It's even possible to run both
at the same time, but my scripts use the same port so that needs a
workaround.
And it's amazing how often I break the Docker scripts.
Diffstat (limited to 'elm3/Lib/Util.elm')
-rw-r--r-- | elm3/Lib/Util.elm | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/elm3/Lib/Util.elm b/elm3/Lib/Util.elm new file mode 100644 index 00000000..f6b39188 --- /dev/null +++ b/elm3/Lib/Util.elm @@ -0,0 +1,76 @@ +module Lib.Util exposing (..) + +import Char +import Dict + +-- Delete an element from a List +delidx : Int -> List a -> List a +delidx n l = List.take n l ++ List.drop (n+1) l + + +-- Modify an element in a List +modidx : Int -> (a -> a) -> List a -> List a +modidx n f = List.indexedMap (\i e -> if i == n then f e else e) + + +isJust : Maybe a -> Bool +isJust m = case m of + Just _ -> True + _ -> False + + +-- Split by newline, trim whitespace and remove empty lines +splitLn : String -> List String +splitLn = String.lines >> List.map String.trim >> List.filter ((/=)"") + +-- Returns true if the list contains duplicates +hasDuplicates : List comparable -> Bool +hasDuplicates l = + let + step e acc = + case acc of + Nothing -> Nothing + Just m -> if Dict.member e m then Nothing else Just (Dict.insert e True m) + in + case List.foldr step (Just Dict.empty) l of + Nothing -> True + Just _ -> False + + +-- Similar to perl's ucfirst() (not terribly efficient) +toUpperFirst : String -> String +toUpperFirst s = String.toList s |> List.indexedMap (\i c -> if i == 0 then Char.toUpper c else c) |> String.fromList + + +-- Haskell's 'lookup' - find an entry in an association list +lookup : a -> List (a,b) -> Maybe b +lookup n l = List.filter (\(a,_) -> a == n) l |> List.head |> Maybe.map Tuple.second + + +formatGtin : Int -> String +formatGtin n = if n == 0 then "" else String.fromInt n |> String.padLeft 12 '0' + + +-- Based on VNDBUtil::gtintype() +validateGtin : String -> Maybe Int +validateGtin = + let check = String.fromInt + >> String.reverse + >> String.toList + >> List.indexedMap (\i c -> (Char.toCode c - Char.toCode '0') * if modBy 2 i == 0 then 1 else 3) + >> List.sum + inval n = + n < 1000000000 + || (n >= 200000000000 && n < 600000000000) + || (n >= 2000000000000 && n < 3000000000000) + || n >= 9770000000000 + || modBy 10 (check n) /= 0 + in String.filter Char.isDigit >> String.toInt >> Maybe.andThen (\n -> if inval n then Nothing else Just n) + + +spoilLevels : List (String, String) +spoilLevels = + [ ("0", "No spoiler") + , ("1", "Minor spoiler") + , ("2", "Major spoiler") + ] |