Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Dict.get a case insensitive key?

Tags:

dictionary

elm

I'd like to grab the Total-Records key of a response.headers HTTP response.

Problem is that in some browser it is as returned by the server Total-Records but in some other, it is in lower case.

I'd like to get the value of the Total-Records header regardless of its case. How would you do that?

like image 594
Natim Avatar asked Jan 03 '23 22:01

Natim


1 Answers

Yet another alternative is to use the find function from elm-community/dict-extra to provide a custom comparison operator:

import Dict.Extra

caseInsensitiveGet : String -> Dict String v -> Maybe v
caseInsensitiveGet key =
    let
        lowerKey = String.toLower key
    in
        Dict.Extra.find (\k _ -> String.toLower k == lowerKey)
            >> Maybe.map Tuple.second
like image 131
Chad Gilbert Avatar answered Jan 13 '23 14:01

Chad Gilbert