| 11418 | * @since 3.10.0 |
| 11419 | */ |
| 11420 | export function HashSet<Value extends Constraint>(value: Value): HashSet<Value> { |
| 11421 | const schema = declareConstructor< |
| 11422 | HashSet_.HashSet<Value["Type"]>, |
| 11423 | HashSet_.HashSet<Value["Encoded"]>, |
| 11424 | HashSetIso<Value> |
| 11425 | >()( |
| 11426 | [value], |
| 11427 | ([value]) => { |
| 11428 | const values = ArraySchema(value) |
| 11429 | return (input, ast, options) => { |
| 11430 | if (HashSet_.isHashSet(input)) { |
| 11431 | return Effect.mapBothEager( |
| 11432 | SchemaParser.decodeUnknownEffect(values)(Arr.fromIterable(input), options), |
| 11433 | { |
| 11434 | onSuccess: HashSet_.fromIterable, |
| 11435 | onFailure: (issue) => SchemaIssue.makeCompositeAtKey(ast, "values", issue, input, options) |
| 11436 | } |
| 11437 | ) |
| 11438 | } |
| 11439 | return Effect.fail(new SchemaIssue.InvalidType(ast, input, options)) |
| 11440 | } |
| 11441 | }, |
| 11442 | { |
| 11443 | representation: { |
| 11444 | id: "effect/schema/HashSet", |
| 11445 | payload: null |
| 11446 | }, |
| 11447 | toCode: ({ typeParameters }) => ({ |
| 11448 | runtime: `Schema.HashSet(${typeParameters[0].runtime})`, |
| 11449 | Type: `HashSet.HashSet<${typeParameters[0].Type}>` |
| 11450 | }), |
| 11451 | expected: "HashSet", |
| 11452 | toCodec: ([value]) => |
| 11453 | link<HashSet_.HashSet<Value["Encoded"]>>()( |
| 11454 | ArraySchema(value), |
| 11455 | SchemaTransformation.transform({ |
| 11456 | decode: HashSet_.fromIterable, |
| 11457 | encode: Arr.fromIterable |
| 11458 | }) |
| 11459 | ), |
| 11460 | toArbitrary: ([value]) => (fc, ctx) => |
| 11461 | collectionArbitrary(fc, ctx, value.arbitrary, value.terminal, HashSet_.fromIterable, Equal.equals), |
| 11462 | toEquivalence: ([value]) => Equal.makeCompareSet(value), |
| 11463 | toFormatter: ([value]) => (t) => { |
| 11464 | const size = HashSet_.size(t) |
| 11465 | if (size === 0) { |
| 11466 | return "HashSet(0) {}" |
| 11467 | } |
| 11468 | const values = globalThis.Array.from(t).sort().map((v) => `${value(v)}`) |
| 11469 | return `HashSet(${size}) { ${values.join(", ")} }` |
| 11470 | } |
| 11471 | } |
| 11472 | ) |
| 11473 | return make(schema.ast, { value }) |
| 11474 | } |
| 11475 | |
| 11476 | /** |
| 11477 | * Reviver for persisted `HashSet` declarations. |