updux/src/buildSelectors/index.ts

28 lines
759 B
TypeScript
Raw Normal View History

2020-01-03 17:00:24 +00:00
import fp from 'lodash/fp';
import Updux from '..';
import { Dictionary, Selector } from '../types';
2020-06-02 20:00:48 +00:00
function subSelectors([slice, selectors]: [string, Function]): [string, Selector][] {
2020-01-03 17:00:24 +00:00
if (!selectors) return [];
return Object.entries(
2020-01-03 22:27:11 +00:00
fp.mapValues(selector => (state: any) =>
(selector as any)(state[slice])
2020-06-02 20:00:48 +00:00
)(selectors as any)
2020-01-03 17:00:24 +00:00
);
}
export default function buildSelectors(
localSelectors: Dictionary<Selector> = {},
2020-06-02 20:00:48 +00:00
coduxes: Dictionary<Selector>[] = [],
subduxes: Dictionary<Selector> = {}
2020-01-03 17:00:24 +00:00
) {
return Object.fromEntries(
[
Object.entries(subduxes).flatMap(subSelectors),
2020-06-02 20:00:48 +00:00
Object.entries(coduxes),
2020-01-03 17:00:24 +00:00
Object.entries(localSelectors),
].flat()
);
}