BrianHicks / elm-csv

Decode CSV in the most boring way possible.

Home Page:https://package.elm-lang.org/packages/BrianHicks/elm-csv/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Encode.withoutFieldNames to Dict Int String

pravdomil opened this issue · comments

I'm looking instead of
withoutFieldNames : (a -> List String) -> Encoder a
to
withoutFieldNames : (a -> Dict Int String) -> Encoder a
because I'm having following structure:

toDict a =
    [ ( 8, a.firstname )
    , ( 9, a.lastname )
    , ( 11, a.organization )

    --
    , ( 12, a.address )
    , ( 15, a.city )
    , ( 57, a.stateCode )
    , ( 18, a.countryCode )
    , ( 17, a.postcode )

    --
    , ( 51, a.email )
    , ( 52, a.phone )

    --
    , ( 47, a.type_ )
    , ( 22, a.services )
    , ( 21, a.weight )

    --
    , ( 37, a.note )
    , ( 38, a.labelNote )
    ]
        |> Dict.fromList

We can revert to List String simply by (List.indexedMap Tuple.pair) >> Dict.fromString

Great library!

Oh, that's interesting! So it seems like these things are true:

  • you have a very wide CSV
  • you don't care about most of the field names

Is that right? It might be useful to provide a sparser option in this case!

Yes, I just got set of fixed indexes for each field.

More I think about it, it's more of a edge case, and CSV is not ment to be used this way, so I'm going to close the issue, if you are interested, feel free to open it.

And I manage to use your library with following trick, so it is fine.

toList : Data -> List String
toList a =
    let
        dict : Dict Int String
        dict =
            a |> toDict

        end : Int
        end =
            dict |> Dict.keys |> List.foldl max 0
    in
    List.range 1 end
        |> List.map (\v -> dict |> Dict.get v |> Maybe.withDefault "")