( key: Key, value: Value )
| 11083 | * @since 3.10.0 |
| 11084 | */ |
| 11085 | export function ReadonlyMap<Key extends Constraint, Value extends Constraint>( |
| 11086 | key: Key, |
| 11087 | value: Value |
| 11088 | ): $ReadonlyMap<Key, Value> { |
| 11089 | const schema = declareConstructor< |
| 11090 | globalThis.ReadonlyMap<Key["Type"], Value["Type"]>, |
| 11091 | globalThis.ReadonlyMap<Key["Encoded"], Value["Encoded"]>, |
| 11092 | ReadonlyMapIso<Key, Value> |
| 11093 | >()( |
| 11094 | [key, value], |
| 11095 | ([key, value]) => { |
| 11096 | const array = ArraySchema(Tuple([key, value])) |
| 11097 | return (input, ast, options) => { |
| 11098 | if (input instanceof globalThis.Map) { |
| 11099 | return Effect.mapBothEager( |
| 11100 | SchemaParser.decodeUnknownEffect(array)([...input], options), |
| 11101 | { |
| 11102 | onSuccess: (array: ReadonlyArray<readonly [Key["Type"], Value["Type"]]>) => new globalThis.Map(array), |
| 11103 | onFailure: (issue) => SchemaIssue.makeCompositeAtKey(ast, "entries", issue, input, options) |
| 11104 | } |
| 11105 | ) |
| 11106 | } |
| 11107 | return Effect.fail(new SchemaIssue.InvalidType(ast, input, options)) |
| 11108 | } |
| 11109 | }, |
| 11110 | { |
| 11111 | representation: { |
| 11112 | id: "effect/schema/ReadonlyMap", |
| 11113 | payload: null |
| 11114 | }, |
| 11115 | toCode: ({ typeParameters }) => ({ |
| 11116 | runtime: `Schema.ReadonlyMap(${typeParameters[0].runtime}, ${typeParameters[1].runtime})`, |
| 11117 | Type: `globalThis.ReadonlyMap<${typeParameters[0].Type}, ${typeParameters[1].Type}>` |
| 11118 | }), |
| 11119 | expected: "ReadonlyMap", |
| 11120 | toCodec: ([key, value]) => |
| 11121 | link<globalThis.Map<Key["Encoded"], Value["Encoded"]>>()( |
| 11122 | ArraySchema(Tuple([key, value])), |
| 11123 | SchemaTransformation.transform({ |
| 11124 | decode: (e) => new globalThis.Map(e), |
| 11125 | encode: (map) => [...map.entries()] |
| 11126 | }) |
| 11127 | ), |
| 11128 | toArbitrary: ([key, value]) => (fc, ctx) => entriesArbitrary(fc, ctx, key, value, (as) => new globalThis.Map(as)), |
| 11129 | toEquivalence: ([key, value]) => Equal.makeCompareMap(key, value), |
| 11130 | toFormatter: ([key, value]) => (t) => { |
| 11131 | const size = t.size |
| 11132 | if (size === 0) { |
| 11133 | return "ReadonlyMap(0) {}" |
| 11134 | } |
| 11135 | const entries = globalThis.Array.from(t.entries()).sort().map(([k, v]) => `${key(k)} => ${value(v)}`) |
| 11136 | return `ReadonlyMap(${size}) { ${entries.join(", ")} }` |
| 11137 | } |
| 11138 | } |
| 11139 | ) |
| 11140 | return make(schema.ast, { key, value }) |
| 11141 | } |
| 11142 |
no test coverage detected