(mapping: M)
| 3704 | * @since 4.0.0 |
| 3705 | */ |
| 3706 | export 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 | * |
nothing calls this directly
no test coverage detected