renum
is a small library to create frozen objects in javascript from multiple sources.
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'
);
renum('1', '2', '3') //=> {1: '1', 2: '2', 3: '3'}
renum('1 2 3') //=> {1: '1', 2: '2', 3: '3'}
renum(`
1
2
3
`) //=> {1: '1', 2: '2', 3: '3'}
renum(1, 2, 3) //=> {1: 1, 2: 2, 3: 3}
renum(true, false, true) //=> {true: true, false: false}
renum(null, undefined) //=> {}
renum(Symbol(1), Symbol(f => f + 1)) //=> {Symbol(1): Symbol(1), Symbol(f => f + 1): 'Symbol(f => f + 1)'}
renum(['1', 2, true]) //=> {1: '1', 2: 2, true: true}
renum([
['INCREMENT', n => n + 1],
['DECREMENT', n => n - 1],
]) //=> {INCREMENT: [Function], DECREMENT: [Function]}
renum(new Map({INCREMENT: '+', DECREMENT: '-'})) //=> {INCREMENT: '+', DECREMENT: '-'}
renum(new Set([1, 2, 3])) //=> {1: 1, 2: 2, 3: 3}
renum(1, renum(2, renum(3))) //=> {1: 1, 2: 2, 3: 3}
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.
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.
npm test
npm run bench
- Firstly designed to create immutable
action types
fromredux
based projects and easily be able to merge each other. - Learning Functional Programming.