📚💙 object-with-expectations
Let you merge objects with expected asyncronious results and normal properties.
@luxcium/object-with-expectations@0.2.2
install with pnpm npm or yarn
pnpm add @luxcium/object-with-expectations
All example imply this code:
import { mixBases, mixExpected } from '@luxcium/object-with-expectations';
const onlyFoo = {
foo: 'machin',
};
const onlyBar = {
bar: 'bidule',
};
const foo = {
foo: 'machin',
expected: { bolo: (async () => 'await bolo')() },
};
const bar = {
bar: 'bidule',
expected: { toto: (async () => 'await toto')() },
};
Use mixBases to mix 2 objects without an expected
property.
const withBaseOnly = {
...mixBases<any, any, { foo: string }, { bar: string }>(onlyFoo, onlyBar),
};
console.log('at withBaseOnly:', withBaseOnly);
Use mixBases to mix 2 objects with an expected
property.
const withExpectedProperty = { ...mixBases(foo, bar) };
console.log('at withExpectedProperty:', withExpectedProperty);
A separate function is required in order to merge the sub-properties from your expeceted
object.
const mergeExpected = mixExpected(foo, bar);
console.log('at mergeExpected:', mergeExpected);
To have meaningfull type inference it is required to groom your data in order to get the best TypeScript support.
Gradually reshaping the output type infered keeping the same value shape as expected.
const expected = mergeExpected.expected;
console.log('at expected:', expected);
To have meaningfull type inference it is required to groom your data in order to get the best TypeScript support.
Giving its final shape (type wise) for the outpput
const expectedResult = { expected: { ...expected } };
console.log('at expectedResult:', expectedResult);
You can then finalize by merging the 2 objects parts together.
const mergingAll = { ...expectOnly, ...expectedFinal };
console.log('at mergingAll:', mergingAll);
† Scientia es lux principium✨ ™
MIT © 2022 Luxcium ✨ (Benjamin Vincent)