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

Function HashMap

packages/effect/src/Schema.ts:11200–11254  ·  view source on GitHub ↗
(key: Key, value: Value)

Source from the content-addressed store, hash-verified

11198 * @since 3.10.0
11199 */
11200export function HashMap<Key extends Constraint, Value extends Constraint>(key: Key, value: Value): HashMap<Key, Value> {
11201 const schema = declareConstructor<
11202 HashMap_.HashMap<Key["Type"], Value["Type"]>,
11203 HashMap_.HashMap<Key["Encoded"], Value["Encoded"]>,
11204 HashMapIso<Key, Value>
11205 >()(
11206 [key, value],
11207 ([key, value]) => {
11208 const entries = ArraySchema(Tuple([key, value]))
11209 return (input, ast, options) => {
11210 if (HashMap_.isHashMap(input)) {
11211 return Effect.mapBothEager(
11212 SchemaParser.decodeUnknownEffect(entries)(HashMap_.toEntries(input), options),
11213 {
11214 onSuccess: HashMap_.fromIterable,
11215 onFailure: (issue) => SchemaIssue.makeCompositeAtKey(ast, "entries", issue, input, options)
11216 }
11217 )
11218 }
11219 return Effect.fail(new SchemaIssue.InvalidType(ast, input, options))
11220 }
11221 },
11222 {
11223 representation: {
11224 id: "effect/schema/HashMap",
11225 payload: null
11226 },
11227 toCode: ({ typeParameters }) => ({
11228 runtime: `Schema.HashMap(${typeParameters[0].runtime}, ${typeParameters[1].runtime})`,
11229 Type: `HashMap.HashMap<${typeParameters[0].Type}, ${typeParameters[1].Type}>`,
11230 importDeclarations: [`import * as HashMap from "effect/HashMap"`]
11231 }),
11232 expected: "HashMap",
11233 toCodec: ([key, value]) =>
11234 link<HashMap_.HashMap<Key["Encoded"], Value["Encoded"]>>()(
11235 ArraySchema(Tuple([key, value])),
11236 SchemaTransformation.transform({
11237 decode: HashMap_.fromIterable,
11238 encode: HashMap_.toEntries
11239 })
11240 ),
11241 toArbitrary: ([key, value]) => (fc, ctx) => entriesArbitrary(fc, ctx, key, value, HashMap_.fromIterable),
11242 toEquivalence: ([key, value]) => Equal.makeCompareMap(key, value),
11243 toFormatter: ([key, value]) => (t) => {
11244 const size = HashMap_.size(t)
11245 if (size === 0) {
11246 return "HashMap(0) {}"
11247 }
11248 const entries = HashMap_.toEntries(t).sort().map(([k, v]) => `${key(k)} => ${value(v)}`)
11249 return `HashMap(${size}) { ${entries.join(", ")} }`
11250 }
11251 }
11252 )
11253 return make(schema.ast, { key, value })
11254}
11255
11256/**
11257 * Reviver for persisted `HashMap` declarations.

Callers 1

Schema.tsFile · 0.70

Calls 12

entriesArbitraryFunction · 0.85
keyFunction · 0.85
joinMethod · 0.80
declareConstructorFunction · 0.70
TupleFunction · 0.70
linkFunction · 0.70
valueFunction · 0.70
makeFunction · 0.70
transformMethod · 0.65
sizeMethod · 0.65
failMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected