(mapping: M)
| 3662 | * @since 4.0.0 |
| 3663 | */ |
| 3664 | export function encodeKeys< |
| 3665 | S extends Constraint & { readonly fields: Struct.Fields }, |
| 3666 | const M extends { readonly [K in keyof S["fields"]]?: PropertyKey } |
| 3667 | >(mapping: M) { |
| 3668 | return function(self: S): encodeKeys<S, M> { |
| 3669 | const fields: any = {} |
| 3670 | const appliedMapping: any = Object.create(null) |
| 3671 | const reverseMapping: any = Object.create(null) |
| 3672 | const seenEncodedKeys = new Set<string | symbol>() |
| 3673 | for (const k of Reflect.ownKeys(self.fields)) { |
| 3674 | const encoded = toEncoded(self.fields[k]) |
| 3675 | const hasMapping = Object.hasOwn(mapping, k) |
| 3676 | const encodedKey = hasMapping ? (mapping as any)[k] as PropertyKey : k |
| 3677 | const canonical = canonicalPropertyKey(encodedKey) |
| 3678 | if (seenEncodedKeys.has(canonical)) { |
| 3679 | throw new globalThis.Error(`Duplicate encoded keys: ${formatPropertyKey(encodedKey)}`) |
| 3680 | } |
| 3681 | seenEncodedKeys.add(canonical) |
| 3682 | InternalRecord.assignProperty(fields, encodedKey, encoded) |
| 3683 | if (hasMapping) { |
| 3684 | appliedMapping[k] = encodedKey |
| 3685 | reverseMapping[encodedKey] = k |
| 3686 | } |
| 3687 | } |
| 3688 | return Struct(fields).pipe(decodeTo( |
| 3689 | self, |
| 3690 | SchemaTransformation.transform<any, any>({ |
| 3691 | decode: Struct_.renameKeys(reverseMapping), |
| 3692 | encode: Struct_.renameKeys(appliedMapping) |
| 3693 | }) |
| 3694 | )) as any |
| 3695 | } |
| 3696 | } |
| 3697 | |
| 3698 | /** |
| 3699 | * Adds derived fields to a struct schema during decoding. |
nothing calls this directly
no test coverage detected