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

Function ReadonlySet

packages/effect/src/Schema.ts:11310–11364  ·  view source on GitHub ↗
(value: Value)

Source from the content-addressed store, hash-verified

11308 * @since 3.10.0
11309 */
11310export function ReadonlySet<Value extends Constraint>(value: Value): $ReadonlySet<Value> {
11311 const schema = declareConstructor<
11312 globalThis.ReadonlySet<Value["Type"]>,
11313 globalThis.ReadonlySet<Value["Encoded"]>,
11314 ReadonlySetIso<Value>
11315 >()(
11316 [value],
11317 ([value]) => {
11318 const array = ArraySchema(value)
11319 return (input, ast, options) => {
11320 if (input instanceof globalThis.Set) {
11321 return Effect.mapBothEager(
11322 SchemaParser.decodeUnknownEffect(array)([...input], options),
11323 {
11324 onSuccess: (array: ReadonlyArray<Value["Type"]>) => new globalThis.Set(array),
11325 onFailure: (issue) => SchemaIssue.makeCompositeAtKey(ast, "values", issue, input, options)
11326 }
11327 )
11328 }
11329 return Effect.fail(new SchemaIssue.InvalidType(ast, input, options))
11330 }
11331 },
11332 {
11333 representation: {
11334 id: "effect/schema/ReadonlySet",
11335 payload: null
11336 },
11337 toCode: ({ typeParameters }) => ({
11338 runtime: `Schema.ReadonlySet(${typeParameters[0].runtime})`,
11339 Type: `globalThis.ReadonlySet<${typeParameters[0].Type}>`
11340 }),
11341 expected: "ReadonlySet",
11342 toCodec: ([value]) =>
11343 link<globalThis.Set<Value["Encoded"]>>()(
11344 ArraySchema(value),
11345 SchemaTransformation.transform({
11346 decode: (e) => new globalThis.Set(e),
11347 encode: (set) => [...set.values()]
11348 })
11349 ),
11350 toArbitrary: ([value]) => (fc, ctx) =>
11351 collectionArbitrary(fc, ctx, value.arbitrary, value.terminal, (as) => new globalThis.Set(as), Equal.equals),
11352 toEquivalence: ([value]) => Equal.makeCompareSet(value),
11353 toFormatter: ([value]) => (t) => {
11354 const size = t.size
11355 if (size === 0) {
11356 return "ReadonlySet(0) {}"
11357 }
11358 const values = globalThis.Array.from(t.values()).sort().map((v) => `${value(v)}`)
11359 return `ReadonlySet(${size}) { ${values.join(", ")} }`
11360 }
11361 }
11362 )
11363 return make(schema.ast, { value })
11364}
11365
11366/**
11367 * Reviver for persisted {@link ReadonlySet} declarations.

Callers 1

Schema.tsFile · 0.85

Calls 9

collectionArbitraryFunction · 0.85
joinMethod · 0.80
declareConstructorFunction · 0.70
linkFunction · 0.70
valueFunction · 0.70
makeFunction · 0.70
transformMethod · 0.65
failMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected