( proto: T, readableProperties: ReadableProperties, writeableProperties?: WriteableProperties, )
| 3 | import type { MutableBuffer } from "./binary/index.js"; |
| 4 | |
| 5 | export const createProto = < |
| 6 | T extends object, |
| 7 | ReadableProperties extends Readonly<Record<string, unknown>>, |
| 8 | WriteableProperties extends Readonly<Record<string, unknown>>, |
| 9 | >( |
| 10 | proto: T, |
| 11 | readableProperties: ReadableProperties, |
| 12 | writeableProperties?: WriteableProperties, |
| 13 | ): T & Readonly<ReadableProperties> & WriteableProperties => { |
| 14 | const readablePropertyDescriptors = Record.map( |
| 15 | readableProperties, |
| 16 | (value): PropertyDescriptor => ({ |
| 17 | enumerable: true, |
| 18 | configurable: false, |
| 19 | writable: false, |
| 20 | value, |
| 21 | }), |
| 22 | ); |
| 23 | |
| 24 | const writeablePropertyDescriptors = Record.map( |
| 25 | writeableProperties ?? {}, |
| 26 | (value): PropertyDescriptor => ({ |
| 27 | enumerable: true, |
| 28 | configurable: false, |
| 29 | writable: true, |
| 30 | value, |
| 31 | }), |
| 32 | ); |
| 33 | |
| 34 | return Object.create( |
| 35 | proto, |
| 36 | Record.union( |
| 37 | readablePropertyDescriptors, |
| 38 | writeablePropertyDescriptors, |
| 39 | Function.untupled(Tuple.getFirst), |
| 40 | ), |
| 41 | ) as T & Readonly<ReadableProperties> & WriteableProperties; |
| 42 | }; |
| 43 | |
| 44 | /** |
| 45 | * This is more strictly typed than necessary, but this allows us to give better type hints. |
no test coverage detected