initial state and splats

This commit is contained in:
Yanick Champoux 2022-08-30 10:55:16 -04:00
parent 51421c1052
commit 3e6d2dae55
2 changed files with 25 additions and 3 deletions

View File

@ -24,7 +24,7 @@ export class Updux {
constructor(config = {}) { constructor(config = {}) {
this.#config = config; this.#config = config;
this.#localInitial = config.initial ?? {}; this.#localInitial = config.initial;
this.#subduxes = config.subduxes ?? {}; this.#subduxes = config.subduxes ?? {};
this.#actions = R.mapValues(config.actions ?? {}, (arg, name) => this.#actions = R.mapValues(config.actions ?? {}, (arg, name) =>
@ -73,11 +73,16 @@ export class Updux {
} }
get initial() { get initial() {
if (Object.keys(this.#subduxes).length === 0) return this.#localInitial; if (Object.keys(this.#subduxes).length === 0) return this.#localInitial ?? {};
if( this.#subduxes['*'] ) {
if( this.#localInitial ) return this.#localInitial;
return [];
}
return Object.assign( return Object.assign(
{}, {},
this.#localInitial, this.#localInitial ?? {},
R.mapValues(this.#subduxes, ({ initial }) => initial), R.mapValues(this.#subduxes, ({ initial }) => initial),
); );
} }

View File

@ -25,3 +25,20 @@ test('initial value', () => {
bar: 123, bar: 123,
}); });
}); });
test( "splat initial", async () => {
const bar = new Updux({
initial: { id: 0 },
});
const foo = new Updux({
subduxes: { '*': bar },
});
expect(foo.initial).toEqual([]);
expect( new Updux({
initial: 'overriden',
subduxes: { '*': bar },
}).initial ).toEqual('overriden');
});