npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

elm-gen

v0.1.12

Published

Experimental CLI tool for generating Elm JSON Decoders and Encoders, written mostly in Elm

Downloads

28

Readme

npm version

elm-gen

Experimental CLI tool for generating Elm JSON Decoders and Encoders, written mostly in Elm. Based on top of elm-ast parsing library.

Usage

Online demo

Simplified demonstration available here.

CLI tool

CLI tool can be obtained from NPM. After installation, you can used it like that:
elm-gen decoders&encoders Model.elm .
Call above will produce new file ModelEncodersAndDecoders.elm, which will contain decoders and encoders for types present in Model.elm.
Also note, that generated elm code for decoders uses NoRedInk elm-decode-pipeline package functions, so it must be installed into your project.
For more examples you may also look at functional tests in ts/MainTests.ts and correspondent files in tests_data folder.

Features

  • elm-gen can generate decoders and/or encoders for user defined Record and Union types without type variables.
  • elm-gen supports basic elm types supported by Json.(Encode|Decode) plus tuples and Maybe's.
  • elm-gen can use hand-made decoders and encoders Decoder X and X -> Value in order to generate decoders for types, dependent on type X
  • elm-gen will follow explicit import type references to look for type definitions doesnot present in current file
  • elm-gen dont require to break your Elm code in any way (its still compilable by elm-make without pre-processing), except for some meta-comments insertion.

Config

Some configuration (mostly about naming) can be applied from config json-file. Example of config can be found in tests_data folder. It's correspondent Elm type representation resides in Config.elm

Meta-Comments

Elm-gen supports meta-comments in source code, in form of either -- //Meta-Comment or {-| //Meta-Comment -}.
For now, there are 4 meta-comments:

  • Ignore -- elm-gen completely ignores next type definition (literally skipping it during parse stage).

  • DefaultValue -- use next pair of (value type def, value expr) to produce default value for Json.Decode.Pipeline.optional function. Supports values of types defaultValue: UnionType for union types, and defaultValue : Record or defaultValue : Record -> Record for record types.

  • NoDeclaration -- elm-gen won't generates type declaration for next type's decoder (usefull for preserving record structural typing ability for generated decoder )

  • Field name aliases - You can define aliases for some field names to avoid issues with elm reserved words, and to make some special field names (like _id and _rev which stored by Couchdb) normally looking in Elm representation. For example, I use meta-comment and record to define some common aliases for Couchdb-mapped types:

{-| //FieldNameMapping
-}
couchAliases =
    { id = "_id"
    , rev = "_rev"
    , type_ = "_type"
    }

And later I apply such mapping to other type via another meta-comment:

{-| //UseFieldNameMapping(couchAliases)
-}
type alias NewUser =
    { id : String
    , name : String
    , password : String
    , type_ : String
    , ...
    }