MCPcopy Create free account
hub / github.com/Effect-TS/effect / extendTo

Function extendTo

packages/effect/src/Schema.ts:3774–3808  ·  view source on GitHub ↗
(
  fields: Fields,
  derive: { readonly [K in keyof Fields]: (s: S["Type"]) => Option_.Option<Fields[K]["Type"]> }
)

Source from the content-addressed store, hash-verified

3772 * @since 4.0.0
3773 */
3774export function extendTo<S extends Struct<Struct.Fields>, const Fields extends Struct.Fields>(
3775 fields: Fields,
3776 derive: { readonly [K in keyof Fields]: (s: S["Type"]) => Option_.Option<Fields[K]["Type"]> }
3777) {
3778 return (
3779 self: S
3780 ): decodeTo<Struct<Simplify<{ [K in keyof S["fields"]]: toType<S["fields"][K]> } & Fields>>, S> => {
3781 const f = Struct_.map(self.fields, toType)
3782 const to = Struct({ ...f, ...fields })
3783 const keys = InternalRecord.ownEnumerableKeys(fields)
3784 return self.pipe(decodeTo(
3785 to,
3786 SchemaTransformation.transform({
3787 decode: (input) => {
3788 const out: any = { ...input }
3789 for (const k of keys) {
3790 const f = derive[k]
3791 const o = f(input)
3792 if (Option_.isSome(o)) {
3793 InternalRecord.assignProperty(out, k, o.value)
3794 }
3795 }
3796 return out
3797 },
3798 encode: (input) => {
3799 const out = { ...input }
3800 for (const k of keys) {
3801 delete out[k]
3802 }
3803 return out
3804 }
3805 })
3806 )) as any
3807 }
3808}
3809/**
3810 * Namespace for `Record` type utilities.
3811 *

Callers

nothing calls this directly

Calls 6

StructFunction · 0.70
decodeToFunction · 0.70
pipeMethod · 0.65
transformMethod · 0.65
fFunction · 0.50
mapMethod · 0.45

Tested by

no test coverage detected