UNPKG

redux-saga-tools

Version:

A set of utility functions to write saga and reducers easily

68 lines (67 loc) 2.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function createReducer(initialState, handlers) { return function reducer(state = initialState, action) { if (handlers.hasOwnProperty(action.type)) { return handlers[action.type](state, action.payload || {}); } else { return state; } }; } exports.createReducer = createReducer; function reduceById(array) { return (array || []).reduce((a, b) => { if (b == undefined || b.id == undefined) { return a; } return Object.assign({}, a, { [b.id]: b }); }, {}); } exports.reduceById = reduceById; function toArray(byId) { return ids(byId).map(key => (byId || {})[key]); } exports.toArray = toArray; function ids(object) { return Object.keys(object || {}); } exports.ids = ids; function setById(state, byId) { return Object.assign({}, (state || {}), { byId: Object.assign({}, ((state || {}).byId || {}), byId) }); } exports.setById = setById; function filterById(byId, itemIds) { return (itemIds || []).reduce((a, key) => { const item = (byId || {})[key]; if (item == undefined) { return a; } return Object.assign({}, a, { [key]: item }); }, {}); } exports.filterById = filterById; function filterToArrayById(byId, itemIds) { return (itemIds || []).map(key => (byId || {})[key]); } exports.filterToArrayById = filterToArrayById; function unique(array) { const properties = (array || []).reduce((a, item) => (Object.assign({}, a, { [item]: true })), {}); return Object.keys(properties); } exports.unique = unique; function uniqueProps(array, property = 'id') { const properties = (array || []).reduce((a, item) => (Object.assign({}, a, { [(item || {})[property]]: true })), {}); return Object.keys(properties); } exports.uniqueProps = uniqueProps; function uniquePropsById(byId, property = 'id') { const properties = Object.keys(byId || {}).reduce((a, key) => (Object.assign({}, a, { [((byId || {})[key] || {})[property]]: true })), {}); return Object.keys(properties); } exports.uniquePropsById = uniquePropsById; function missingIds(byId, objectIds) { return (objectIds || []).filter(id => id != undefined && (byId || {})[id] == undefined); } exports.missingIds = missingIds;