Iteratee-first data-last null-safe curried functions wrapper for immutable.js
Are you using Immutable.js with React and Redux? Take a look at react-redux-immutabe for mapping your immutable state to your props without performance hit.
Functional Immutable relies on a single peer dependency, it requires Immutable 3 already installed.
npm install --save functional-immutable
Functional Immutable aims to provide all immutable object methods as curried, data-last, null-safe functions. It is designed to work seamlessly in an point-free style program.
For example :
import fp from "lodash/fp";
import Immutable from "immutable";
import * as fi from "functional-immutable";
const immutableObject = Immutable.fromJS({
user: {
lastName: "Skywalker"
}
});
export const getUppercaseLastName = fp.flow(
fi.getIn(['user', 'lastName']),
fp.defaultTo('Doe'),
fp.toUpper,
);
getUppercaseLastName(immutableObject); // SKYWALKER
getUppercaseLastName(); // DOE
Also, Functional Immutable is non-opinionated and will work with any point-free library such as Ramda.
import R from "ramda";
import Immutable from "immutable";
import * as fi from "functional-immutable";
const immutableObject = Immutable.fromJS({
user: {
lastName: "Skywalker"
}
});
export const getUppercaseLastName = R.compose(
R.toUpper,
R.defaultTo('Doe'),
fi.getIn(['user', 'lastName']),
);
getUppercaseLastName(immutableObject); // SKYWALKER
getUppercaseLastName(); // DOE
It should also shine with Redux selectors such as reselect or re-reselect.
import { createSelector } from "reselect";
import Immutable from "immutable";
import * as fi from "functional-immutable";
const applicationState = Immutable.fromJS({
user: {
lastName: "Skywalker"
}
});
export const getUppercaseLastName = createSelector(
fp.flow(
fi.getIn(['user', 'lastName']),
fp.defaultTo('Doe')
),
fp.toUpper
);
getUppercaseLastName(applicationState); // SKYWALKER
getUppercaseLastName(); // DOE
delete
being a reserved keyword, the delete method has been re-exported as deleteAt
.
Found a missing function? Feel free to contribute by opening a Pull Request.
This wrapper relies on naïve re-exports of Immutable object methods. Here is how to add a missing method called missing
.
In index.js add the following:
export const missing = (...vargs) => data => safeApply(data, 'missing', vargs);
You can also contribute by writing unit tests, for example by rewriting existing Immutable 3 Unit Tests using functional-immutable
.