GithubHelp home page GithubHelp logo

unmarshaller's Introduction

Unmarshaller

Greenkeeper badge

Toolbox for configuration

Build Status

Motivations

  • A declarative way for defining configuration
  • Reusability and composability
  • Flexible and generic

Installation

npm install --save unmarshaller

Example

import {builder, unmarshal} from 'unmarshaller';

const unmarshaller = {
  editor: builder.string('EDITOR'),
  browser: builder.string('BROWSER'),
};

const lookupFn = (key) => process.env[key];
const config = unmarshal(lookupFn, unmarshaller);

console.log(config);

Basics

Lookup function

You need to provide to the unmarshal function a way to lookup from keys in configuration.

The example above returns the value found in the process's environment:

const lookupFn = (key) => process.env[key];

Unmarshaller

Just an object which represents your configuration.

Builder

Helper functions to build the unmarshaller object.

Default types

type
string
boolean
number
object
holder
or

Default options

name type description
defaultValue string fallback value if the lookup returned undefined or null
of array provide an enumeration of possible values
parser function provide an custom parser function (usually when you want your own types)

Or

const unmarshaller = {
  foo: builder.or(
    builder.string('foo_a'),
    builder.string('foo_b'),
  )
};

The unmarshalling process will call the lookup function until a value (not undefined or null) is returned.

Extending an existing holder

import {extend, builder} from 'unmarshaller';

const holder = builder.holder({
  editor: builder.string('EDITOR'),
  browser: builder.string('BROWSER'),
});

const extendedHolder = extend(holder, {
  version: builder.string('VERSION'),
});

Extending the default builder

The builder is a regular JavaScript object.

Customizing the builder gives you the possiblity to use your own data converters.

The following example use a custom type: color (which in my use case parses a string into a structure).

import {builder as defaultBuilder} from 'unmarshaller';

export const builder = {
  ...defaultBuilder,
  color: (name, options) => ({
    name,
    parser: parseColor,
    type: 'color',
    ...options
  }),
};

You need to provide a custom parser function (parseColor in the example above). The definition is: function(value: string): string.

unmarshaller's People

Contributors

greenkeeper[bot] avatar jsebfranck avatar tomesch avatar xtuc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

tomesch

unmarshaller's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.