updux/src/schema.ts.2024-07-29

37 lines
905 B
Plaintext
Raw Normal View History

2023-09-06 19:09:45 +00:00
import u from '@yanick/updeep-remeda';
import * as R from 'remeda';
export default function buildSchema(
schema: any = {},
initialState = undefined,
subduxes = {},
) {
if (typeof initialState !== 'undefined')
schema = u(schema, { default: u.constant(initialState) });
if (!schema.type) {
schema = u(schema, {
type: Array.isArray(schema.default)
? 'array'
: typeof schema.default,
});
}
if (schema.type === 'object') {
// TODO will break with objects and arrays
schema = u(schema, {
properties: R.mapValues(schema.default, (v) => ({
type: typeof v,
})),
});
}
if (Object.keys(subduxes).length > 0) {
schema = u(schema, {
properties: R.mapValues(subduxes, R.prop('schema')),
});
}
return schema;
}