decode-js

0.3.1 • Public • Published

decode-js

Make working with JSON in your application more reliable and explicit. Strongly inspired by Elm's JSON Decoding library.

Install

npm install --save decode-js

Example

Given the following JSON:

{
  name_one: 'Jack',
  name_two: 'Franklin',
  age: 24,
  favourites: {
    color: 'red',
  },
  likes_javascript: true
}

We want to not only check that the types are as expected, but do some tidying of the data:

  • name_one and name_two should become firstName and lastName
  • favourites.color needs to be favourites.colour, we're not animals!
  • likes_javascript should be likesJS

We can type check and rename easily with decode-js:

import {
  createDecoder,
  object,
  boolean,
  string,
  number,
  rename
} from 'decode-js';

const decoder = createDecoder({
  firstName: rename('name_one', string),
  lastName: rename('name_two', string),
  age: number,
  favourites: object({
    colour: rename('colour', string),
  }),
  likesJS: rename('likes_javascript', boolean),
});

We can then parse some JSON:

// input here is the JSON from above
import { decode } from 'decode-js';

const result = decode(input, decoder);

Any errors will be under result.errors, and result.data will have the fully type checked and manipulated data.

In lieu of more documentation currently, the tests demonstrate all the functionality.

Readme

Keywords

none

Package Sidebar

Install

npm i decode-js

Weekly Downloads

0

Version

0.3.1

License

ISC

Last publish

Collaborators

  • jackfranklin