@talend/react-cmf
Version:
A framework built on top of best react libraries
61 lines (58 loc) • 2.02 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.findListItem = findListItem;
exports.get = get;
exports.getAll = getAll;
exports.toJS = toJS;
var _immutable = require("immutable");
var _toJS = _interopRequireDefault(require("./toJS"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function getAll(state) {
return state.cmf.collections;
}
/**
* return a collection or subset of a collection from a cmf store
* @param {Object} state
* @param {String or Array<String>} collectionPath
* @example
* get('foo.bar', true) === state.cmf.collections.getIn(['foo', 'bar'], true)
*/
function get(state, collectionPath, defaultValue) {
let path;
if (typeof collectionPath === 'string') {
path = collectionPath.split('.');
} else if (Array.isArray(collectionPath)) {
path = collectionPath;
}
if (path) {
return state.cmf.collections.getIn(path, defaultValue);
}
throw Error(`Type mismatch: collectionPath should be a string or an array of string
got ${collectionPath}`);
}
/**
* for a collectionId and an id find and return the an item from this
* collection if it is a list
* @param {Object} state
* @param {String} collectionId
* @param {String} itemId
*/
function findListItem(state, collectionPath, itemId) {
const collectionOrCollectionSubset = get(state, collectionPath);
if (_immutable.List.isList(collectionOrCollectionSubset)) {
return collectionOrCollectionSubset.find(element => element && element.get('id') === itemId);
}
throw Error(`Type mismatch: ${collectionPath} does not resolve as an instance of Immutable.List,
got ${collectionOrCollectionSubset}`);
}
const selectors = {};
function toJS(state, path) {
const joinedPath = Array.isArray(path) ? path.join('.') : path;
if (!selectors[joinedPath]) {
selectors[joinedPath] = (0, _toJS.default)(calledState => get(calledState, path));
}
return selectors[joinedPath](state);
}
//# sourceMappingURL=collections.js.map
;