effect 4.0.0-beta.22 → 4.0.0-beta.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Schema.js CHANGED
@@ -4,6 +4,7 @@
4
4
  import * as Arr from "./Array.js";
5
5
  import * as BigDecimal_ from "./BigDecimal.js";
6
6
  import * as Cause_ from "./Cause.js";
7
+ import * as Chunk_ from "./Chunk.js";
7
8
  import * as Data from "./Data.js";
8
9
  import * as DateTime from "./DateTime.js";
9
10
  import * as Duration_ from "./Duration.js";
@@ -1248,17 +1249,6 @@ export function TaggedStruct(value, fields) {
1248
1249
  ...fields
1249
1250
  });
1250
1251
  }
1251
- /** @internal */
1252
- export function _getTagValueIfPropertyKey(tag, ast) {
1253
- const ps = ast.propertySignatures.find(p => p.name === tag);
1254
- if (ps) {
1255
- if (AST.isLiteral(ps.type) && Predicate.isPropertyKey(ps.type.literal)) {
1256
- return ps.type.literal;
1257
- } else if (AST.isUniqueSymbol(ps.type)) {
1258
- return ps.type.symbol;
1259
- }
1260
- }
1261
- }
1262
1252
  /**
1263
1253
  * @since 4.0.0
1264
1254
  * @experimental
@@ -1280,15 +1270,16 @@ export function toTaggedUnion(tag) {
1280
1270
  if (AST.isUnion(ast) && "members" in schema && globalThis.Array.isArray(schema.members) && schema.members.every(isSchema)) {
1281
1271
  return schema.members.forEach(walk);
1282
1272
  }
1283
- if (AST.isObjects(ast)) {
1284
- const key = _getTagValueIfPropertyKey(tag, ast);
1285
- if (key !== undefined) {
1286
- cases[key] = schema;
1287
- guards[key] = is(toType(schema));
1273
+ const sentinels = AST.collectSentinels(ast);
1274
+ if (sentinels.length > 0) {
1275
+ const literal = sentinels.find(s => s.key === tag)?.literal;
1276
+ if (Predicate.isPropertyKey(literal)) {
1277
+ cases[literal] = schema;
1278
+ guards[literal] = is(toType(schema));
1288
1279
  return;
1289
1280
  }
1290
1281
  }
1291
- throw new globalThis.Error("No literal found");
1282
+ throw new globalThis.Error("No literal or unique symbol found");
1292
1283
  }
1293
1284
  function match() {
1294
1285
  if (arguments.length === 1) {
@@ -3838,6 +3829,58 @@ export function HashSet(value) {
3838
3829
  value
3839
3830
  });
3840
3831
  }
3832
+ /**
3833
+ * Creates a schema that validates a `Chunk` where values must conform to the
3834
+ * provided schema.
3835
+ *
3836
+ * @category Chunk
3837
+ * @since 4.0.0
3838
+ */
3839
+ export function Chunk(value) {
3840
+ const schema = declareConstructor()([value], ([value]) => {
3841
+ const values = Array(value);
3842
+ return (input, ast, options) => {
3843
+ if (Chunk_.isChunk(input)) {
3844
+ return Effect.mapBothEager(Parser.decodeUnknownEffect(values)(Arr.fromIterable(input), options), {
3845
+ onSuccess: Chunk_.fromIterable,
3846
+ onFailure: issue => new Issue.Composite(ast, Option_.some(input), [new Issue.Pointer(["values"], issue)])
3847
+ });
3848
+ }
3849
+ return Effect.fail(new Issue.InvalidType(ast, Option_.some(input)));
3850
+ };
3851
+ }, {
3852
+ typeConstructor: {
3853
+ _tag: "effect/Chunk"
3854
+ },
3855
+ generation: {
3856
+ runtime: `Schema.Chunk(?)`,
3857
+ Type: `Chunk.Chunk<?>`
3858
+ },
3859
+ expected: "Chunk",
3860
+ toCodec: ([value]) => link()(Array(value), Transformation.transform({
3861
+ decode: Chunk_.fromIterable,
3862
+ encode: Arr.fromIterable
3863
+ })),
3864
+ toArbitrary: ([value]) => (fc, ctx) => {
3865
+ return fc.oneof(ctx?.isSuspend ? {
3866
+ maxDepth: 2,
3867
+ depthIdentifier: "Chunk"
3868
+ } : {}, fc.constant([]), fc.array(value, ctx?.constraints?.array)).map(Chunk_.fromIterable);
3869
+ },
3870
+ toEquivalence: ([value]) => Chunk_.makeEquivalence(value),
3871
+ toFormatter: ([value]) => t => {
3872
+ const size = Chunk_.size(t);
3873
+ if (size === 0) {
3874
+ return "Chunk(0) {}";
3875
+ }
3876
+ const values = globalThis.Array.from(t).sort().map(v => `${value(v)}`);
3877
+ return `Chunk(${size}) { ${values.join(", ")} }`;
3878
+ }
3879
+ });
3880
+ return make(schema.ast, {
3881
+ value
3882
+ });
3883
+ }
3841
3884
  /**
3842
3885
  * @since 4.0.0
3843
3886
  */
@@ -4893,6 +4936,7 @@ function getClassSchemaFactory(from, identifier, annotations) {
4893
4936
  toCodec: ([from]) => new AST.Link(from.ast, transformation),
4894
4937
  toArbitrary: ([from]) => () => from.map(args => new self(args)),
4895
4938
  toFormatter: ([from]) => t => `${self.identifier}(${from(t)})`,
4939
+ "~sentinels": AST.collectSentinels(from.ast),
4896
4940
  ...annotations
4897
4941
  }));
4898
4942
  memo = from.pipe(decodeTo(to, transformation));