@voidhash/mimic 0.0.1-alpha.2 → 0.0.1-alpha.3

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/index.mjs CHANGED
@@ -1632,6 +1632,188 @@ function Union(options) {
1632
1632
  });
1633
1633
  }
1634
1634
 
1635
+ //#endregion
1636
+ //#region src/primitives/Either.ts
1637
+ var EitherPrimitive = class EitherPrimitive {
1638
+ constructor(schema) {
1639
+ _defineProperty(this, "_tag", "EitherPrimitive");
1640
+ _defineProperty(this, "_State", void 0);
1641
+ _defineProperty(this, "_Proxy", void 0);
1642
+ _defineProperty(this, "_schema", void 0);
1643
+ _defineProperty(this, "_opDefinitions", { set: make$5({
1644
+ kind: "either.set",
1645
+ payload: Schema.Unknown,
1646
+ target: Schema.Unknown,
1647
+ apply: (payload) => payload
1648
+ }) });
1649
+ _defineProperty(this, "_internal", {
1650
+ createProxy: (env, operationPath) => {
1651
+ const defaultValue = this._schema.defaultValue;
1652
+ return {
1653
+ get: () => {
1654
+ const state = env.getState(operationPath);
1655
+ return state !== null && state !== void 0 ? state : defaultValue;
1656
+ },
1657
+ set: (value) => {
1658
+ env.addOperation(fromDefinition(operationPath, this._opDefinitions.set, value));
1659
+ },
1660
+ match: (handlers) => {
1661
+ const currentState = env.getState(operationPath);
1662
+ const effectiveState = currentState !== null && currentState !== void 0 ? currentState : defaultValue;
1663
+ if (effectiveState === void 0) return void 0;
1664
+ const valueType = this._getValueType(effectiveState);
1665
+ if (!valueType) return void 0;
1666
+ switch (valueType) {
1667
+ case "string":
1668
+ var _handlers$string;
1669
+ return (_handlers$string = handlers.string) === null || _handlers$string === void 0 ? void 0 : _handlers$string.call(handlers, effectiveState);
1670
+ case "number":
1671
+ var _handlers$number;
1672
+ return (_handlers$number = handlers.number) === null || _handlers$number === void 0 ? void 0 : _handlers$number.call(handlers, effectiveState);
1673
+ case "boolean":
1674
+ var _handlers$boolean;
1675
+ return (_handlers$boolean = handlers.boolean) === null || _handlers$boolean === void 0 ? void 0 : _handlers$boolean.call(handlers, effectiveState);
1676
+ case "literal":
1677
+ var _handlers$literal;
1678
+ return (_handlers$literal = handlers.literal) === null || _handlers$literal === void 0 ? void 0 : _handlers$literal.call(handlers, effectiveState);
1679
+ default: return;
1680
+ }
1681
+ },
1682
+ toSnapshot: () => {
1683
+ const state = env.getState(operationPath);
1684
+ return state !== null && state !== void 0 ? state : defaultValue;
1685
+ }
1686
+ };
1687
+ },
1688
+ applyOperation: (_state, operation) => {
1689
+ if (operation.kind !== "either.set") throw new ValidationError(`EitherPrimitive cannot apply operation of kind: ${operation.kind}`);
1690
+ const payload = operation.payload;
1691
+ this._validateAndApplyToVariant(payload, operation.path);
1692
+ return payload;
1693
+ },
1694
+ getInitialState: () => {
1695
+ return this._schema.defaultValue;
1696
+ },
1697
+ transformOperation: (clientOp, serverOp) => {
1698
+ if (!pathsOverlap(clientOp.path, serverOp.path)) return {
1699
+ type: "transformed",
1700
+ operation: clientOp
1701
+ };
1702
+ return {
1703
+ type: "transformed",
1704
+ operation: clientOp
1705
+ };
1706
+ }
1707
+ });
1708
+ this._schema = schema;
1709
+ }
1710
+ /** Mark this either as required */
1711
+ required() {
1712
+ return new EitherPrimitive(_objectSpread2(_objectSpread2({}, this._schema), {}, { required: true }));
1713
+ }
1714
+ /** Set a default value for this either */
1715
+ default(defaultValue) {
1716
+ return new EitherPrimitive(_objectSpread2(_objectSpread2({}, this._schema), {}, { defaultValue }));
1717
+ }
1718
+ /** Get the variants */
1719
+ get variants() {
1720
+ return this._schema.variants;
1721
+ }
1722
+ /**
1723
+ * Determine the type category of a value based on the variants
1724
+ */
1725
+ _getValueType(value) {
1726
+ const valueType = typeof value;
1727
+ for (const variant of this._schema.variants) if (variant._tag === "LiteralPrimitive") {
1728
+ if (value === variant.literal) return "literal";
1729
+ }
1730
+ if (valueType === "string") {
1731
+ for (const variant of this._schema.variants) if (variant._tag === "StringPrimitive") return "string";
1732
+ }
1733
+ if (valueType === "number") {
1734
+ for (const variant of this._schema.variants) if (variant._tag === "NumberPrimitive") return "number";
1735
+ }
1736
+ if (valueType === "boolean") {
1737
+ for (const variant of this._schema.variants) if (variant._tag === "BooleanPrimitive") return "boolean";
1738
+ }
1739
+ }
1740
+ /**
1741
+ * Find the matching variant for a value.
1742
+ * For literals, matches exact value. For other types, matches by typeof.
1743
+ */
1744
+ _findMatchingVariant(value) {
1745
+ const valueType = typeof value;
1746
+ for (const variant of this._schema.variants) if (variant._tag === "LiteralPrimitive") {
1747
+ if (value === variant.literal) return variant;
1748
+ }
1749
+ if (valueType === "string") {
1750
+ for (const variant of this._schema.variants) if (variant._tag === "StringPrimitive") return variant;
1751
+ }
1752
+ if (valueType === "number") {
1753
+ for (const variant of this._schema.variants) if (variant._tag === "NumberPrimitive") return variant;
1754
+ }
1755
+ if (valueType === "boolean") {
1756
+ for (const variant of this._schema.variants) if (variant._tag === "BooleanPrimitive") return variant;
1757
+ }
1758
+ }
1759
+ /**
1760
+ * Get the operation kind for a variant
1761
+ */
1762
+ _getVariantOperationKind(variant) {
1763
+ switch (variant._tag) {
1764
+ case "StringPrimitive": return "string.set";
1765
+ case "NumberPrimitive": return "number.set";
1766
+ case "BooleanPrimitive": return "boolean.set";
1767
+ case "LiteralPrimitive": return "literal.set";
1768
+ default: return "unknown.set";
1769
+ }
1770
+ }
1771
+ /**
1772
+ * Validate a value against the matching variant, including running its validators.
1773
+ * Throws ValidationError if the value doesn't match any variant or fails validation.
1774
+ */
1775
+ _validateAndApplyToVariant(value, path) {
1776
+ const matchingVariant = this._findMatchingVariant(value);
1777
+ if (!matchingVariant) throw new ValidationError(`EitherPrimitive.set requires a value matching one of: ${this._schema.variants.map((v) => v._tag).join(", ")}, got: ${typeof value}`);
1778
+ const syntheticOp = {
1779
+ kind: this._getVariantOperationKind(matchingVariant),
1780
+ path,
1781
+ payload: value
1782
+ };
1783
+ matchingVariant._internal.applyOperation(void 0, syntheticOp);
1784
+ }
1785
+ };
1786
+ /**
1787
+ * Creates a new EitherPrimitive with the given scalar variant types.
1788
+ * Validators defined on the variants are applied when validating values.
1789
+ *
1790
+ * @example
1791
+ * ```typescript
1792
+ * // String or number
1793
+ * const value = Either(String(), Number());
1794
+ *
1795
+ * // String, number, or boolean
1796
+ * const status = Either(String(), Number(), Boolean()).default("pending");
1797
+ *
1798
+ * // With literal types
1799
+ * const mode = Either(Literal("auto"), Literal("manual"), Number());
1800
+ *
1801
+ * // With validators - validates string length and number range
1802
+ * const constrained = Either(
1803
+ * String().min(2).max(50),
1804
+ * Number().max(255)
1805
+ * );
1806
+ * ```
1807
+ */
1808
+ function Either(...variants) {
1809
+ if (variants.length === 0) throw new ValidationError("Either requires at least one variant");
1810
+ return new EitherPrimitive({
1811
+ required: false,
1812
+ defaultValue: void 0,
1813
+ variants
1814
+ });
1815
+ }
1816
+
1635
1817
  //#endregion
1636
1818
  //#region src/primitives/TreeNode.ts
1637
1819
  /**
@@ -2223,6 +2405,8 @@ var Primitive_exports = /* @__PURE__ */ __export({
2223
2405
  ArrayPrimitive: () => ArrayPrimitive,
2224
2406
  Boolean: () => Boolean,
2225
2407
  BooleanPrimitive: () => BooleanPrimitive,
2408
+ Either: () => Either,
2409
+ EitherPrimitive: () => EitherPrimitive,
2226
2410
  Lazy: () => Lazy,
2227
2411
  LazyPrimitive: () => LazyPrimitive,
2228
2412
  Literal: () => Literal,