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

Function encodeKeys

packages/effect/src/Schema.ts:3706–3738  ·  view source on GitHub ↗
(mapping: M)

Source from the content-addressed store, hash-verified

3704 * @since 4.0.0
3705 */
3706export function encodeKeys<
3707 S extends Constraint & { readonly fields: Struct.Fields },
3708 const M extends { readonly [K in keyof S["fields"]]?: PropertyKey }
3709>(mapping: M) {
3710 return function(self: S): encodeKeys<S, M> {
3711 const fields: any = {}
3712 const appliedMapping: any = Object.create(null)
3713 const reverseMapping: any = Object.create(null)
3714 const seenEncodedKeys = new Set<string | symbol>()
3715 for (const k of InternalRecord.ownEnumerableKeys(self.fields)) {
3716 const encoded = toEncoded(self.fields[k])
3717 const hasMapping = Object.hasOwn(mapping, k)
3718 const encodedKey = hasMapping ? (mapping as any)[k] as PropertyKey : k
3719 const canonical = canonicalPropertyKey(encodedKey)
3720 if (seenEncodedKeys.has(canonical)) {
3721 throw new globalThis.Error(`Duplicate encoded keys: ${formatPropertyKey(encodedKey)}`)
3722 }
3723 seenEncodedKeys.add(canonical)
3724 InternalRecord.assignProperty(fields, encodedKey, encoded)
3725 if (hasMapping) {
3726 appliedMapping[k] = encodedKey
3727 reverseMapping[encodedKey] = k
3728 }
3729 }
3730 return Struct(fields).pipe(decodeTo(
3731 self,
3732 SchemaTransformation.transform<any, any>({
3733 decode: Struct_.renameKeys(reverseMapping),
3734 encode: Struct_.renameKeys(appliedMapping)
3735 })
3736 )) as any
3737 }
3738}
3739/**
3740 * Adds derived fields to a struct schema during decoding.
3741 *

Callers

nothing calls this directly

Calls 9

formatPropertyKeyFunction · 0.90
canonicalPropertyKeyFunction · 0.85
toEncodedInterface · 0.70
StructFunction · 0.70
decodeToFunction · 0.70
hasMethod · 0.65
addMethod · 0.65
pipeMethod · 0.65
transformMethod · 0.65

Tested by

no test coverage detected