Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

unmarshaller

Package Overview
Dependencies
Maintainers
4
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unmarshaller

Toolbox for configuration

  • 1.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
increased by200%
Maintainers
4
Weekly downloads
 
Created
Source

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
nametypedescription
defaultValuestringfallback value if the lookup returned undefined or null
ofarrayprovide an enumeration of possible values
parserfunctionprovide 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.

Keywords

FAQs

Package last updated on 10 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc