Skip to content

bydooweedoo/renum

Repository files navigation

renum npm package Travis Coveralls

renum is a small library to create frozen objects in javascript from multiple sources.

Getting started

Install renum using npm:

npm install renum --save

Then using ES6

import renum from 'renum';

export default renum(
  'INCREMENT',
  'DECREMENT'
);

Using ES5

const renum = require('renum');

module.exports = renum(
  'INCREMENT',
  'DECREMENT'
);

Usage

From primitive arguments

Strings

renum('1', '2', '3') //=> {1: '1', 2: '2', 3: '3'}

String

renum('1 2 3') //=> {1: '1', 2: '2', 3: '3'}

Template String

renum(`
  1
  2
  3
`) //=> {1: '1', 2: '2', 3: '3'}

Numbers

renum(1, 2, 3) //=> {1: 1, 2: 2, 3: 3}

Boolean

renum(true, false, true) //=> {true: true, false: false}

Empty values

renum(null, undefined) //=> {}

Symbol

renum(Symbol(1), Symbol(f => f + 1)) //=> {Symbol(1): Symbol(1), Symbol(f => f + 1): 'Symbol(f => f + 1)'}

From Objects

Array

renum(['1', 2, true]) //=> {1: '1', 2: 2, true: true}

Pairs

renum([
  ['INCREMENT', n => n + 1],
  ['DECREMENT', n => n - 1],
]) //=> {INCREMENT: [Function], DECREMENT: [Function]}

Map

renum(new Map({INCREMENT: '+', DECREMENT: '-'})) //=> {INCREMENT: '+', DECREMENT: '-'}

Set

renum(new Set([1, 2, 3])) //=> {1: 1, 2: 2, 3: 3}

Inception

renum(1, renum(2, renum(3))) //=> {1: 1, 2: 2, 3: 3}

Extending renum

In order to let you use renum with other libraries, you can implement your own translator.

Example for immutable:

// renum-immutable.js
import R from 'ramda';
import Immutable from 'immutable';

export default [
  [Immutable.List.isList, R.invoker(0, 'toArray')],
  [Immutable.Map.isMap, R.invoker(0, 'toObject')],
  [Immutable.Stack.isStack, R.invoker(0, 'toJS')],
];

And then:

import R from 'ramda';
import renum from 'renum';
import renumImmutable from './renum-immutable';

// from a list of pairs
renum.extend(renumImmutable);

// or from arguments directly
renum.extend(R.isEmpty, R.always({}));

Extend functions are called in priority first pass, then a second pass will be done with the default included predicates/transforms. It mean you actually can return another type rather than a frozen Object, and renum will convert it properly.

Typescript

renum Typescript type declarations are available, you just need to add a reference:

///<reference path='./node_modules/renum/type-definitions/renum.d.ts'/>

You can also read this file in order to better understand how renum works.

Testing

npm test

Benchmark

npm run bench

Motivations

  • Firstly designed to create immutable action types from redux based projects and easily be able to merge each other.
  • Learning Functional Programming.