updux/src/buildInitial/index.ts

18 lines
532 B
TypeScript
Raw Normal View History

2021-10-15 16:41:58 +00:00
import { isPlainObject, mapValues } from 'lodash';
import u from 'updeep';
2021-09-28 03:11:13 +00:00
export function buildInitial(initial, subduxes = {}) {
if (!isPlainObject(initial) && Object.keys(subduxes).length > 0)
throw new Error(
"can't have subduxes on a dux which state is not an object"
);
2021-10-12 13:42:30 +00:00
if (Object.keys(subduxes).length === 1 && subduxes['*']) return initial;
2021-09-28 03:11:13 +00:00
const subInitial = mapValues(subduxes, ({ initial }, key) =>
key === '*' ? [] : initial
);
return u(initial, subInitial);
}