@voidhash/mimic 0.0.2 → 0.0.4
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/.turbo/turbo-build.log +257 -42
- package/dist/Document.cjs +152 -0
- package/dist/Document.d.cts +67 -0
- package/dist/Document.d.cts.map +1 -0
- package/dist/Document.d.mts +68 -0
- package/dist/Document.d.mts.map +1 -0
- package/dist/Document.mjs +147 -0
- package/dist/Document.mjs.map +1 -0
- package/dist/EffectSchema.cjs +180 -0
- package/dist/EffectSchema.d.cts +84 -0
- package/dist/EffectSchema.d.cts.map +1 -0
- package/dist/EffectSchema.d.mts +84 -0
- package/dist/EffectSchema.d.mts.map +1 -0
- package/dist/EffectSchema.mjs +176 -0
- package/dist/EffectSchema.mjs.map +1 -0
- package/dist/FractionalIndex.cjs +365 -0
- package/dist/FractionalIndex.mjs +364 -0
- package/dist/FractionalIndex.mjs.map +1 -0
- package/dist/Operation.cjs +53 -0
- package/dist/Operation.d.cts +39 -0
- package/dist/Operation.d.cts.map +1 -0
- package/dist/Operation.d.mts +39 -0
- package/dist/Operation.d.mts.map +1 -0
- package/dist/Operation.mjs +46 -0
- package/dist/Operation.mjs.map +1 -0
- package/dist/OperationDefinition.cjs +13 -0
- package/dist/OperationDefinition.d.cts +12 -0
- package/dist/OperationDefinition.d.cts.map +1 -0
- package/dist/OperationDefinition.d.mts +12 -0
- package/dist/OperationDefinition.d.mts.map +1 -0
- package/dist/OperationDefinition.mjs +13 -0
- package/dist/OperationDefinition.mjs.map +1 -0
- package/dist/OperationPath.cjs +148 -0
- package/dist/OperationPath.d.cts +60 -0
- package/dist/OperationPath.d.cts.map +1 -0
- package/dist/OperationPath.d.mts +60 -0
- package/dist/OperationPath.d.mts.map +1 -0
- package/dist/OperationPath.mjs +138 -0
- package/dist/OperationPath.mjs.map +1 -0
- package/dist/{Presence-gWrmGBeu.cjs → Presence.cjs} +4 -39
- package/dist/{Presence-N8u7Eppr.d.mts → Presence.d.cts} +2 -2
- package/dist/Presence.d.cts.map +1 -0
- package/dist/{Presence-DKKP4v5X.d.cts → Presence.d.mts} +2 -2
- package/dist/Presence.d.mts.map +1 -0
- package/dist/{Presence-DdMVKcOv.mjs → Presence.mjs} +3 -28
- package/dist/Presence.mjs.map +1 -0
- package/dist/Primitive.cjs +52 -0
- package/dist/Primitive.d.cts +20 -0
- package/dist/Primitive.d.cts.map +1 -0
- package/dist/Primitive.d.mts +20 -0
- package/dist/Primitive.d.mts.map +1 -0
- package/dist/Primitive.mjs +48 -0
- package/dist/Primitive.mjs.map +1 -0
- package/dist/ProxyEnvironment.cjs +34 -0
- package/dist/ProxyEnvironment.d.cts +31 -0
- package/dist/ProxyEnvironment.d.cts.map +1 -0
- package/dist/ProxyEnvironment.d.mts +31 -0
- package/dist/ProxyEnvironment.d.mts.map +1 -0
- package/dist/ProxyEnvironment.mjs +29 -0
- package/dist/ProxyEnvironment.mjs.map +1 -0
- package/dist/Transaction.cjs +66 -0
- package/dist/Transaction.d.cts +56 -0
- package/dist/Transaction.d.cts.map +1 -0
- package/dist/Transaction.d.mts +56 -0
- package/dist/Transaction.d.mts.map +1 -0
- package/dist/Transaction.mjs +58 -0
- package/dist/Transaction.mjs.map +1 -0
- package/dist/Transform.cjs +11 -0
- package/dist/Transform.d.cts +21 -0
- package/dist/Transform.d.cts.map +1 -0
- package/dist/Transform.d.mts +21 -0
- package/dist/Transform.d.mts.map +1 -0
- package/dist/Transform.mjs +6 -0
- package/dist/Transform.mjs.map +1 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.cjs +14 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.mjs +14 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.cjs +27 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.mjs +27 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPrimitive.cjs +16 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPrimitive.mjs +16 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPropertyKey.cjs +11 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPropertyKey.mjs +11 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/typeof.cjs +18 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/typeof.mjs +12 -0
- package/dist/_virtual/rolldown_runtime.cjs +43 -0
- package/dist/{chunk-CLMFDpHK.mjs → _virtual/rolldown_runtime.mjs} +1 -1
- package/dist/client/ClientDocument.cjs +590 -0
- package/dist/client/ClientDocument.d.cts +161 -0
- package/dist/client/ClientDocument.d.cts.map +1 -0
- package/dist/client/ClientDocument.d.mts +162 -0
- package/dist/client/ClientDocument.d.mts.map +1 -0
- package/dist/client/ClientDocument.mjs +586 -0
- package/dist/client/ClientDocument.mjs.map +1 -0
- package/dist/client/Rebase.cjs +204 -0
- package/dist/client/Rebase.d.cts +87 -0
- package/dist/client/Rebase.d.cts.map +1 -0
- package/dist/client/Rebase.d.mts +88 -0
- package/dist/client/Rebase.d.mts.map +1 -0
- package/dist/client/Rebase.mjs +198 -0
- package/dist/client/Rebase.mjs.map +1 -0
- package/dist/client/StateMonitor.cjs +133 -0
- package/dist/client/StateMonitor.d.cts +122 -0
- package/dist/client/StateMonitor.d.cts.map +1 -0
- package/dist/client/StateMonitor.d.mts +122 -0
- package/dist/client/StateMonitor.d.mts.map +1 -0
- package/dist/client/StateMonitor.mjs +129 -0
- package/dist/client/StateMonitor.mjs.map +1 -0
- package/dist/client/Transport.cjs +11 -0
- package/dist/client/Transport.d.cts +237 -0
- package/dist/client/Transport.d.cts.map +1 -0
- package/dist/client/Transport.d.mts +237 -0
- package/dist/client/Transport.d.mts.map +1 -0
- package/dist/client/Transport.mjs +6 -0
- package/dist/client/Transport.mjs.map +1 -0
- package/dist/client/WebSocketTransport.cjs +396 -0
- package/dist/client/WebSocketTransport.d.cts +29 -0
- package/dist/client/WebSocketTransport.d.cts.map +1 -0
- package/dist/client/WebSocketTransport.d.mts +29 -0
- package/dist/client/WebSocketTransport.d.mts.map +1 -0
- package/dist/client/WebSocketTransport.mjs +392 -0
- package/dist/client/WebSocketTransport.mjs.map +1 -0
- package/dist/client/errors.cjs +135 -0
- package/dist/client/errors.d.cts +87 -0
- package/dist/client/errors.d.cts.map +1 -0
- package/dist/client/errors.d.mts +87 -0
- package/dist/client/errors.d.mts.map +1 -0
- package/dist/client/errors.mjs +127 -0
- package/dist/client/errors.mjs.map +1 -0
- package/dist/client/index.cjs +22 -1424
- package/dist/client/index.d.cts +8 -692
- package/dist/client/index.d.mts +8 -692
- package/dist/client/index.mjs +9 -1413
- package/dist/index.cjs +20 -2973
- package/dist/index.d.cts +12 -419
- package/dist/index.d.mts +12 -419
- package/dist/index.mjs +13 -2968
- package/dist/primitives/Array.cjs +302 -0
- package/dist/primitives/Array.d.cts +95 -0
- package/dist/primitives/Array.d.cts.map +1 -0
- package/dist/primitives/Array.d.mts +95 -0
- package/dist/primitives/Array.d.mts.map +1 -0
- package/dist/primitives/Array.mjs +301 -0
- package/dist/primitives/Array.mjs.map +1 -0
- package/dist/primitives/Boolean.cjs +95 -0
- package/dist/primitives/Boolean.d.cts +44 -0
- package/dist/primitives/Boolean.d.cts.map +1 -0
- package/dist/primitives/Boolean.d.mts +44 -0
- package/dist/primitives/Boolean.d.mts.map +1 -0
- package/dist/primitives/Boolean.mjs +94 -0
- package/dist/primitives/Boolean.mjs.map +1 -0
- package/dist/primitives/Either.cjs +200 -0
- package/dist/primitives/Either.d.cts +113 -0
- package/dist/primitives/Either.d.cts.map +1 -0
- package/dist/primitives/Either.d.mts +113 -0
- package/dist/primitives/Either.d.mts.map +1 -0
- package/dist/primitives/Either.mjs +199 -0
- package/dist/primitives/Either.mjs.map +1 -0
- package/dist/primitives/Lazy.cjs +46 -0
- package/dist/primitives/Lazy.d.cts +46 -0
- package/dist/primitives/Lazy.d.cts.map +1 -0
- package/dist/primitives/Lazy.d.mts +46 -0
- package/dist/primitives/Lazy.d.mts.map +1 -0
- package/dist/primitives/Lazy.mjs +46 -0
- package/dist/primitives/Lazy.mjs.map +1 -0
- package/dist/primitives/Literal.cjs +91 -0
- package/dist/primitives/Literal.d.cts +46 -0
- package/dist/primitives/Literal.d.cts.map +1 -0
- package/dist/primitives/Literal.d.mts +46 -0
- package/dist/primitives/Literal.d.mts.map +1 -0
- package/dist/primitives/Literal.mjs +90 -0
- package/dist/primitives/Literal.mjs.map +1 -0
- package/dist/primitives/Number.cjs +115 -0
- package/dist/primitives/Number.d.cts +54 -0
- package/dist/primitives/Number.d.cts.map +1 -0
- package/dist/primitives/Number.d.mts +54 -0
- package/dist/primitives/Number.d.mts.map +1 -0
- package/dist/primitives/Number.mjs +114 -0
- package/dist/primitives/Number.mjs.map +1 -0
- package/dist/primitives/String.cjs +127 -0
- package/dist/primitives/String.d.cts +56 -0
- package/dist/primitives/String.d.cts.map +1 -0
- package/dist/primitives/String.d.mts +56 -0
- package/dist/primitives/String.d.mts.map +1 -0
- package/dist/primitives/String.mjs +126 -0
- package/dist/primitives/String.mjs.map +1 -0
- package/dist/primitives/Struct.cjs +207 -0
- package/dist/primitives/Struct.d.cts +96 -0
- package/dist/primitives/Struct.d.cts.map +1 -0
- package/dist/primitives/Struct.d.mts +97 -0
- package/dist/primitives/Struct.d.mts.map +1 -0
- package/dist/primitives/Struct.mjs +206 -0
- package/dist/primitives/Struct.mjs.map +1 -0
- package/dist/primitives/Tree.cjs +575 -0
- package/dist/primitives/Tree.d.cts +185 -0
- package/dist/primitives/Tree.d.cts.map +1 -0
- package/dist/primitives/Tree.d.mts +185 -0
- package/dist/primitives/Tree.d.mts.map +1 -0
- package/dist/primitives/Tree.mjs +574 -0
- package/dist/primitives/Tree.mjs.map +1 -0
- package/dist/primitives/TreeNode.cjs +73 -0
- package/dist/primitives/TreeNode.d.cts +92 -0
- package/dist/primitives/TreeNode.d.cts.map +1 -0
- package/dist/primitives/TreeNode.d.mts +93 -0
- package/dist/primitives/TreeNode.d.mts.map +1 -0
- package/dist/primitives/TreeNode.mjs +72 -0
- package/dist/primitives/TreeNode.mjs.map +1 -0
- package/dist/primitives/Union.cjs +170 -0
- package/dist/primitives/Union.d.cts +81 -0
- package/dist/primitives/Union.d.cts.map +1 -0
- package/dist/primitives/Union.d.mts +81 -0
- package/dist/primitives/Union.d.mts.map +1 -0
- package/dist/primitives/Union.mjs +169 -0
- package/dist/primitives/Union.mjs.map +1 -0
- package/dist/primitives/shared.cjs +60 -0
- package/dist/primitives/shared.d.cts +147 -0
- package/dist/primitives/shared.d.cts.map +1 -0
- package/dist/primitives/shared.d.mts +147 -0
- package/dist/primitives/shared.d.mts.map +1 -0
- package/dist/primitives/shared.mjs +58 -0
- package/dist/primitives/shared.mjs.map +1 -0
- package/dist/server/ServerDocument.cjs +110 -0
- package/dist/server/ServerDocument.d.cts +98 -0
- package/dist/server/ServerDocument.d.cts.map +1 -0
- package/dist/server/ServerDocument.d.mts +99 -0
- package/dist/server/ServerDocument.d.mts.map +1 -0
- package/dist/server/ServerDocument.mjs +106 -0
- package/dist/server/ServerDocument.mjs.map +1 -0
- package/dist/server/errors.cjs +85 -0
- package/dist/server/errors.d.cts +53 -0
- package/dist/server/errors.d.cts.map +1 -0
- package/dist/server/errors.d.mts +53 -0
- package/dist/server/errors.d.mts.map +1 -0
- package/dist/server/errors.mjs +81 -0
- package/dist/server/errors.mjs.map +1 -0
- package/dist/server/index.cjs +9 -185
- package/dist/server/index.d.cts +3 -148
- package/dist/server/index.d.mts +3 -148
- package/dist/server/index.mjs +3 -181
- package/dist/types/index.cjs +16 -0
- package/dist/types/index.d.cts +16 -0
- package/dist/types/index.d.cts.map +1 -0
- package/dist/types/index.d.mts +16 -0
- package/dist/types/index.d.mts.map +1 -0
- package/dist/types/index.mjs +12 -0
- package/dist/types/index.mjs.map +1 -0
- package/dist/utils/tree-helpers.cjs +443 -0
- package/dist/utils/tree-helpers.d.cts +280 -0
- package/dist/utils/tree-helpers.d.cts.map +1 -0
- package/dist/utils/tree-helpers.d.mts +280 -0
- package/dist/utils/tree-helpers.d.mts.map +1 -0
- package/dist/utils/tree-helpers.mjs +439 -0
- package/dist/utils/tree-helpers.mjs.map +1 -0
- package/package.json +2 -2
- package/tsdown.config.ts +1 -1
- package/dist/Document-ChuFrTk1.cjs +0 -571
- package/dist/Document-CwiAFTIq.mjs +0 -438
- package/dist/Document-CwiAFTIq.mjs.map +0 -1
- package/dist/Presence-DKKP4v5X.d.cts.map +0 -1
- package/dist/Presence-DdMVKcOv.mjs.map +0 -1
- package/dist/Presence-N8u7Eppr.d.mts.map +0 -1
- package/dist/Primitive-DqQFc3Gu.d.mts +0 -1180
- package/dist/Primitive-DqQFc3Gu.d.mts.map +0 -1
- package/dist/Primitive-awpEjnKL.d.cts +0 -1180
- package/dist/Primitive-awpEjnKL.d.cts.map +0 -1
- package/dist/client/index.d.cts.map +0 -1
- package/dist/client/index.d.mts.map +0 -1
- package/dist/client/index.mjs.map +0 -1
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/server/index.d.cts.map +0 -1
- package/dist/server/index.d.mts.map +0 -1
- package/dist/server/index.mjs.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Boolean.mjs","names":["OperationDefinition.make","Operation.fromDefinition","OperationPath.pathsOverlap"],"sources":["../../src/primitives/Boolean.ts"],"sourcesContent":["import { Schema } from \"effect\";\nimport * as OperationDefinition from \"../OperationDefinition\";\nimport * as Operation from \"../Operation\";\nimport * as OperationPath from \"../OperationPath\";\nimport * as ProxyEnvironment from \"../ProxyEnvironment\";\nimport * as Transform from \"../Transform\";\nimport type { Primitive, PrimitiveInternal, MaybeUndefined, Validator, NeedsValue } from \"./shared\";\nimport { runValidators, isCompatibleOperation, ValidationError } from \"./shared\";\n\n\ntype InferSetInput<TRequired extends boolean = false, THasDefault extends boolean = false> = NeedsValue<boolean, TRequired, THasDefault>\ntype InferUpdateInput<TRequired extends boolean = false, THasDefault extends boolean = false> = NeedsValue<boolean, TRequired, THasDefault>\n\nexport interface BooleanProxy<TRequired extends boolean = false, THasDefault extends boolean = false> {\n /** Gets the current boolean value */\n get(): MaybeUndefined<boolean, TRequired, THasDefault>;\n /** Sets the boolean value, generating a boolean.set operation */\n set(value: InferSetInput<TRequired, THasDefault>): void;\n /** This is the same as set. Updates the boolean value, generating a boolean.set operation */\n update(value: InferUpdateInput<TRequired, THasDefault>): void;\n /** Returns a readonly snapshot of the boolean value for rendering */\n toSnapshot(): MaybeUndefined<boolean, TRequired, THasDefault>;\n}\n\ninterface BooleanPrimitiveSchema {\n readonly required: boolean;\n readonly defaultValue: boolean | undefined;\n readonly validators: readonly Validator<boolean>[];\n}\n\nexport class BooleanPrimitive<TRequired extends boolean = false, THasDefault extends boolean = false> implements Primitive<boolean, BooleanProxy<TRequired, THasDefault>, TRequired, THasDefault, InferSetInput<TRequired, THasDefault>, InferUpdateInput<TRequired, THasDefault>> {\n readonly _tag = \"BooleanPrimitive\" as const;\n readonly _State!: boolean;\n readonly _Proxy!: BooleanProxy<TRequired, THasDefault>;\n readonly _TRequired!: TRequired;\n readonly _THasDefault!: THasDefault;\n readonly TUpdateInput!: InferUpdateInput<TRequired, THasDefault>;\n readonly TSetInput!: InferSetInput<TRequired, THasDefault>;\n\n private readonly _schema: BooleanPrimitiveSchema;\n\n private readonly _opDefinitions = {\n set: OperationDefinition.make({\n kind: \"boolean.set\" as const,\n payload: Schema.Boolean,\n target: Schema.Boolean,\n apply: (payload) => payload,\n }),\n };\n\n constructor(schema: BooleanPrimitiveSchema) {\n this._schema = schema;\n }\n\n /** Mark this boolean as required */\n required(): BooleanPrimitive<true, THasDefault> {\n return new BooleanPrimitive({\n ...this._schema,\n required: true,\n });\n }\n\n /** Set a default value for this boolean */\n default(defaultValue: boolean): BooleanPrimitive<TRequired, true> {\n return new BooleanPrimitive({\n ...this._schema,\n defaultValue,\n });\n }\n\n /** Add a custom validation rule */\n refine(fn: (value: boolean) => boolean, message: string): BooleanPrimitive<TRequired, THasDefault> {\n return new BooleanPrimitive({\n ...this._schema,\n validators: [...this._schema.validators, { validate: fn, message }],\n });\n }\n\n readonly _internal: PrimitiveInternal<boolean, BooleanProxy<TRequired, THasDefault>> = {\n createProxy: (env: ProxyEnvironment.ProxyEnvironment, operationPath: OperationPath.OperationPath): BooleanProxy<TRequired, THasDefault> => {\n const defaultValue = this._schema.defaultValue;\n return {\n get: (): MaybeUndefined<boolean, TRequired, THasDefault> => {\n const state = env.getState(operationPath) as boolean | undefined;\n return (state ?? defaultValue) as MaybeUndefined<boolean, TRequired, THasDefault>;\n },\n set: (value: InferSetInput<TRequired, THasDefault>) => {\n env.addOperation(\n Operation.fromDefinition(operationPath, this._opDefinitions.set, value)\n );\n },\n update: (value: InferUpdateInput<TRequired, THasDefault>) => {\n env.addOperation(\n Operation.fromDefinition(operationPath, this._opDefinitions.set, value)\n );\n },\n toSnapshot: (): MaybeUndefined<boolean, TRequired, THasDefault> => {\n const state = env.getState(operationPath) as boolean | undefined;\n return (state ?? defaultValue) as MaybeUndefined<boolean, TRequired, THasDefault>;\n },\n };\n },\n\n applyOperation: (_state: boolean | undefined, operation: Operation.Operation<any, any, any>): boolean => {\n if (operation.kind !== \"boolean.set\") {\n throw new ValidationError(`BooleanPrimitive cannot apply operation of kind: ${operation.kind}`);\n }\n\n const payload = operation.payload;\n if (typeof payload !== \"boolean\") {\n throw new ValidationError(`BooleanPrimitive.set requires a boolean payload, got: ${typeof payload}`);\n }\n\n // Run validators\n runValidators(payload, this._schema.validators);\n\n return payload;\n },\n\n getInitialState: (): boolean | undefined => {\n return this._schema.defaultValue;\n },\n\n transformOperation: (\n clientOp: Operation.Operation<any, any, any>,\n serverOp: Operation.Operation<any, any, any>\n ): Transform.TransformResult => {\n // If paths don't overlap, no transformation needed\n if (!OperationPath.pathsOverlap(clientOp.path, serverOp.path)) {\n return { type: \"transformed\", operation: clientOp };\n }\n\n // For same path, client wins (last-write-wins)\n return { type: \"transformed\", operation: clientOp };\n },\n };\n}\n\n/** Creates a new BooleanPrimitive */\nexport const Boolean = (): BooleanPrimitive<false, false> =>\n new BooleanPrimitive({ required: false, defaultValue: undefined, validators: [] });\n\n"],"mappings":";;;;;;;;;AA8BA,IAAa,mBAAb,MAAa,iBAAsQ;CAoBjR,YAAY,QAAgC;wBAnBnC,QAAO;wBACP;wBACA;wBACA;wBACA;wBACA;wBACA;wBAEQ;wBAEA,kBAAiB,EAChC,KAAKA,KAAyB;GAC5B,MAAM;GACN,SAAS,OAAO;GAChB,QAAQ,OAAO;GACf,QAAQ,YAAY;GACrB,CAAC,EACH;wBA8BQ,aAA8E;GACrF,cAAc,KAAwC,kBAAqF;IACzI,MAAM,eAAe,KAAK,QAAQ;AAClC,WAAO;KACL,WAA4D;MAC1D,MAAM,QAAQ,IAAI,SAAS,cAAc;AACzC,aAAQ,6CAAS;;KAEnB,MAAM,UAAiD;AACrD,UAAI,aACFC,eAAyB,eAAe,KAAK,eAAe,KAAK,MAAM,CACxE;;KAEH,SAAS,UAAoD;AAC3D,UAAI,aACFA,eAAyB,eAAe,KAAK,eAAe,KAAK,MAAM,CACxE;;KAEH,kBAAmE;MACjE,MAAM,QAAQ,IAAI,SAAS,cAAc;AACzC,aAAQ,6CAAS;;KAEpB;;GAGH,iBAAiB,QAA6B,cAA2D;AACvG,QAAI,UAAU,SAAS,cACrB,OAAM,IAAI,gBAAgB,oDAAoD,UAAU,OAAO;IAGjG,MAAM,UAAU,UAAU;AAC1B,QAAI,OAAO,YAAY,UACrB,OAAM,IAAI,gBAAgB,yDAAyD,OAAO,UAAU;AAItG,kBAAc,SAAS,KAAK,QAAQ,WAAW;AAE/C,WAAO;;GAGT,uBAA4C;AAC1C,WAAO,KAAK,QAAQ;;GAGtB,qBACE,UACA,aAC8B;AAE9B,QAAI,CAACC,aAA2B,SAAS,MAAM,SAAS,KAAK,CAC3D,QAAO;KAAE,MAAM;KAAe,WAAW;KAAU;AAIrD,WAAO;KAAE,MAAM;KAAe,WAAW;KAAU;;GAEtD;AApFC,OAAK,UAAU;;;CAIjB,WAAgD;AAC9C,SAAO,IAAI,mDACN,KAAK,gBACR,UAAU,QACV;;;CAIJ,QAAQ,cAA0D;AAChE,SAAO,IAAI,mDACN,KAAK,gBACR,gBACA;;;CAIJ,OAAO,IAAiC,SAA2D;AACjG,SAAO,IAAI,mDACN,KAAK,gBACR,YAAY,CAAC,GAAG,KAAK,QAAQ,YAAY;GAAE,UAAU;GAAI;GAAS,CAAC,IACnE;;;;AAgEN,MAAa,gBACX,IAAI,iBAAiB;CAAE,UAAU;CAAO,cAAc;CAAW,YAAY,EAAE;CAAE,CAAC"}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_defineProperty = require('../_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.cjs');
|
|
3
|
+
const require_objectSpread2 = require('../_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.cjs');
|
|
4
|
+
const require_shared = require('./shared.cjs');
|
|
5
|
+
const require_OperationDefinition = require('../OperationDefinition.cjs');
|
|
6
|
+
const require_OperationPath = require('../OperationPath.cjs');
|
|
7
|
+
const require_Operation = require('../Operation.cjs');
|
|
8
|
+
let effect = require("effect");
|
|
9
|
+
|
|
10
|
+
//#region src/primitives/Either.ts
|
|
11
|
+
var EitherPrimitive = class EitherPrimitive {
|
|
12
|
+
constructor(schema) {
|
|
13
|
+
require_defineProperty._defineProperty(this, "_tag", "EitherPrimitive");
|
|
14
|
+
require_defineProperty._defineProperty(this, "_State", void 0);
|
|
15
|
+
require_defineProperty._defineProperty(this, "_Proxy", void 0);
|
|
16
|
+
require_defineProperty._defineProperty(this, "_TRequired", void 0);
|
|
17
|
+
require_defineProperty._defineProperty(this, "_THasDefault", void 0);
|
|
18
|
+
require_defineProperty._defineProperty(this, "TUpdateInput", void 0);
|
|
19
|
+
require_defineProperty._defineProperty(this, "TSetInput", void 0);
|
|
20
|
+
require_defineProperty._defineProperty(this, "_schema", void 0);
|
|
21
|
+
require_defineProperty._defineProperty(this, "_opDefinitions", { set: require_OperationDefinition.make({
|
|
22
|
+
kind: "either.set",
|
|
23
|
+
payload: effect.Schema.Unknown,
|
|
24
|
+
target: effect.Schema.Unknown,
|
|
25
|
+
apply: (payload) => payload
|
|
26
|
+
}) });
|
|
27
|
+
require_defineProperty._defineProperty(this, "_internal", {
|
|
28
|
+
createProxy: (env, operationPath) => {
|
|
29
|
+
const defaultValue = this._schema.defaultValue;
|
|
30
|
+
return {
|
|
31
|
+
get: () => {
|
|
32
|
+
const state = env.getState(operationPath);
|
|
33
|
+
return state !== null && state !== void 0 ? state : defaultValue;
|
|
34
|
+
},
|
|
35
|
+
set: (value) => {
|
|
36
|
+
env.addOperation(require_Operation.fromDefinition(operationPath, this._opDefinitions.set, value));
|
|
37
|
+
},
|
|
38
|
+
update: (value) => {
|
|
39
|
+
env.addOperation(require_Operation.fromDefinition(operationPath, this._opDefinitions.set, value));
|
|
40
|
+
},
|
|
41
|
+
match: (handlers) => {
|
|
42
|
+
const currentState = env.getState(operationPath);
|
|
43
|
+
const effectiveState = currentState !== null && currentState !== void 0 ? currentState : defaultValue;
|
|
44
|
+
if (effectiveState === void 0) return void 0;
|
|
45
|
+
const valueType = this._getValueType(effectiveState);
|
|
46
|
+
if (!valueType) return void 0;
|
|
47
|
+
switch (valueType) {
|
|
48
|
+
case "string":
|
|
49
|
+
var _handlers$string;
|
|
50
|
+
return (_handlers$string = handlers.string) === null || _handlers$string === void 0 ? void 0 : _handlers$string.call(handlers, effectiveState);
|
|
51
|
+
case "number":
|
|
52
|
+
var _handlers$number;
|
|
53
|
+
return (_handlers$number = handlers.number) === null || _handlers$number === void 0 ? void 0 : _handlers$number.call(handlers, effectiveState);
|
|
54
|
+
case "boolean":
|
|
55
|
+
var _handlers$boolean;
|
|
56
|
+
return (_handlers$boolean = handlers.boolean) === null || _handlers$boolean === void 0 ? void 0 : _handlers$boolean.call(handlers, effectiveState);
|
|
57
|
+
case "literal":
|
|
58
|
+
var _handlers$literal;
|
|
59
|
+
return (_handlers$literal = handlers.literal) === null || _handlers$literal === void 0 ? void 0 : _handlers$literal.call(handlers, effectiveState);
|
|
60
|
+
default: return;
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
toSnapshot: () => {
|
|
64
|
+
const state = env.getState(operationPath);
|
|
65
|
+
return state !== null && state !== void 0 ? state : defaultValue;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
},
|
|
69
|
+
applyOperation: (_state, operation) => {
|
|
70
|
+
if (operation.kind !== "either.set") throw new require_shared.ValidationError(`EitherPrimitive cannot apply operation of kind: ${operation.kind}`);
|
|
71
|
+
const payload = operation.payload;
|
|
72
|
+
this._validateAndApplyToVariant(payload, operation.path);
|
|
73
|
+
return payload;
|
|
74
|
+
},
|
|
75
|
+
getInitialState: () => {
|
|
76
|
+
return this._schema.defaultValue;
|
|
77
|
+
},
|
|
78
|
+
transformOperation: (clientOp, serverOp) => {
|
|
79
|
+
if (!require_OperationPath.pathsOverlap(clientOp.path, serverOp.path)) return {
|
|
80
|
+
type: "transformed",
|
|
81
|
+
operation: clientOp
|
|
82
|
+
};
|
|
83
|
+
return {
|
|
84
|
+
type: "transformed",
|
|
85
|
+
operation: clientOp
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
this._schema = schema;
|
|
90
|
+
}
|
|
91
|
+
/** Mark this either as required */
|
|
92
|
+
required() {
|
|
93
|
+
return new EitherPrimitive(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, this._schema), {}, { required: true }));
|
|
94
|
+
}
|
|
95
|
+
/** Set a default value for this either */
|
|
96
|
+
default(defaultValue) {
|
|
97
|
+
return new EitherPrimitive(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, this._schema), {}, { defaultValue }));
|
|
98
|
+
}
|
|
99
|
+
/** Get the variants */
|
|
100
|
+
get variants() {
|
|
101
|
+
return this._schema.variants;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Determine the type category of a value based on the variants
|
|
105
|
+
*/
|
|
106
|
+
_getValueType(value) {
|
|
107
|
+
const valueType = typeof value;
|
|
108
|
+
for (const variant of this._schema.variants) if (variant._tag === "LiteralPrimitive") {
|
|
109
|
+
if (value === variant.literal) return "literal";
|
|
110
|
+
}
|
|
111
|
+
if (valueType === "string") {
|
|
112
|
+
for (const variant of this._schema.variants) if (variant._tag === "StringPrimitive") return "string";
|
|
113
|
+
}
|
|
114
|
+
if (valueType === "number") {
|
|
115
|
+
for (const variant of this._schema.variants) if (variant._tag === "NumberPrimitive") return "number";
|
|
116
|
+
}
|
|
117
|
+
if (valueType === "boolean") {
|
|
118
|
+
for (const variant of this._schema.variants) if (variant._tag === "BooleanPrimitive") return "boolean";
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Find the matching variant for a value.
|
|
123
|
+
* For literals, matches exact value. For other types, matches by typeof.
|
|
124
|
+
*/
|
|
125
|
+
_findMatchingVariant(value) {
|
|
126
|
+
const valueType = typeof value;
|
|
127
|
+
for (const variant of this._schema.variants) if (variant._tag === "LiteralPrimitive") {
|
|
128
|
+
if (value === variant.literal) return variant;
|
|
129
|
+
}
|
|
130
|
+
if (valueType === "string") {
|
|
131
|
+
for (const variant of this._schema.variants) if (variant._tag === "StringPrimitive") return variant;
|
|
132
|
+
}
|
|
133
|
+
if (valueType === "number") {
|
|
134
|
+
for (const variant of this._schema.variants) if (variant._tag === "NumberPrimitive") return variant;
|
|
135
|
+
}
|
|
136
|
+
if (valueType === "boolean") {
|
|
137
|
+
for (const variant of this._schema.variants) if (variant._tag === "BooleanPrimitive") return variant;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Get the operation kind for a variant
|
|
142
|
+
*/
|
|
143
|
+
_getVariantOperationKind(variant) {
|
|
144
|
+
switch (variant._tag) {
|
|
145
|
+
case "StringPrimitive": return "string.set";
|
|
146
|
+
case "NumberPrimitive": return "number.set";
|
|
147
|
+
case "BooleanPrimitive": return "boolean.set";
|
|
148
|
+
case "LiteralPrimitive": return "literal.set";
|
|
149
|
+
default: return "unknown.set";
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Validate a value against the matching variant, including running its validators.
|
|
154
|
+
* Throws ValidationError if the value doesn't match any variant or fails validation.
|
|
155
|
+
*/
|
|
156
|
+
_validateAndApplyToVariant(value, path) {
|
|
157
|
+
const matchingVariant = this._findMatchingVariant(value);
|
|
158
|
+
if (!matchingVariant) throw new require_shared.ValidationError(`EitherPrimitive.set requires a value matching one of: ${this._schema.variants.map((v) => v._tag).join(", ")}, got: ${typeof value}`);
|
|
159
|
+
const syntheticOp = {
|
|
160
|
+
kind: this._getVariantOperationKind(matchingVariant),
|
|
161
|
+
path,
|
|
162
|
+
payload: value
|
|
163
|
+
};
|
|
164
|
+
matchingVariant._internal.applyOperation(void 0, syntheticOp);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* Creates a new EitherPrimitive with the given scalar variant types.
|
|
169
|
+
* Validators defined on the variants are applied when validating values.
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```typescript
|
|
173
|
+
* // String or number
|
|
174
|
+
* const value = Either(String(), Number());
|
|
175
|
+
*
|
|
176
|
+
* // String, number, or boolean
|
|
177
|
+
* const status = Either(String(), Number(), Boolean()).default("pending");
|
|
178
|
+
*
|
|
179
|
+
* // With literal types
|
|
180
|
+
* const mode = Either(Literal("auto"), Literal("manual"), Number());
|
|
181
|
+
*
|
|
182
|
+
* // With validators - validates string length and number range
|
|
183
|
+
* const constrained = Either(
|
|
184
|
+
* String().min(2).max(50),
|
|
185
|
+
* Number().max(255)
|
|
186
|
+
* );
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
function Either(...variants) {
|
|
190
|
+
if (variants.length === 0) throw new require_shared.ValidationError("Either requires at least one variant");
|
|
191
|
+
return new EitherPrimitive({
|
|
192
|
+
required: false,
|
|
193
|
+
defaultValue: void 0,
|
|
194
|
+
variants
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
//#endregion
|
|
199
|
+
exports.Either = Either;
|
|
200
|
+
exports.EitherPrimitive = EitherPrimitive;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { InferState, MaybeUndefined, NeedsValue, Primitive, PrimitiveInternal } from "./shared.cjs";
|
|
2
|
+
import { StringPrimitive } from "./String.cjs";
|
|
3
|
+
import { BooleanPrimitive } from "./Boolean.cjs";
|
|
4
|
+
import { NumberPrimitive } from "./Number.cjs";
|
|
5
|
+
import { LiteralPrimitive, LiteralValue } from "./Literal.cjs";
|
|
6
|
+
|
|
7
|
+
//#region src/primitives/Either.d.ts
|
|
8
|
+
type InferSetInput<TVariants extends readonly ScalarPrimitive[], TRequired extends boolean = false, THasDefault extends boolean = false> = NeedsValue<InferEitherState<TVariants>, TRequired, THasDefault>;
|
|
9
|
+
type InferUpdateInput<TVariants extends readonly ScalarPrimitive[], TRequired extends boolean = false, THasDefault extends boolean = false> = NeedsValue<InferEitherState<TVariants>, TRequired, THasDefault>;
|
|
10
|
+
/**
|
|
11
|
+
* Scalar primitives that can be used as variants in Either
|
|
12
|
+
*/
|
|
13
|
+
type ScalarPrimitive = StringPrimitive<any, any> | NumberPrimitive<any, any> | BooleanPrimitive<any, any> | LiteralPrimitive<any, any, any>;
|
|
14
|
+
/**
|
|
15
|
+
* Infer the union state type from a tuple of scalar primitives
|
|
16
|
+
*/
|
|
17
|
+
type InferEitherState<TVariants extends readonly ScalarPrimitive[]> = InferState<TVariants[number]>;
|
|
18
|
+
/**
|
|
19
|
+
* Infer the union snapshot type from a tuple of scalar primitives
|
|
20
|
+
*/
|
|
21
|
+
type InferEitherSnapshot<TVariants extends readonly ScalarPrimitive[]> = InferState<TVariants[number]>;
|
|
22
|
+
/**
|
|
23
|
+
* Match handlers for Either - optional handlers for each scalar type
|
|
24
|
+
*/
|
|
25
|
+
interface EitherMatchHandlers<R> {
|
|
26
|
+
string?: (value: string) => R;
|
|
27
|
+
number?: (value: number) => R;
|
|
28
|
+
boolean?: (value: boolean) => R;
|
|
29
|
+
literal?: (value: LiteralValue) => R;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Proxy for accessing Either values
|
|
33
|
+
*/
|
|
34
|
+
interface EitherProxy<TVariants extends readonly ScalarPrimitive[], TRequired extends boolean = false, THasDefault extends boolean = false> {
|
|
35
|
+
/** Gets the current value */
|
|
36
|
+
get(): MaybeUndefined<InferEitherState<TVariants>, TRequired, THasDefault>;
|
|
37
|
+
/** Sets the value to any of the allowed variant types */
|
|
38
|
+
set(value: InferSetInput<TVariants, TRequired, THasDefault>): void;
|
|
39
|
+
/** This is the same as set. Updates the value, generating an either.set operation */
|
|
40
|
+
update(value: InferUpdateInput<TVariants, TRequired, THasDefault>): void;
|
|
41
|
+
/** Pattern match on the value type */
|
|
42
|
+
match<R>(handlers: EitherMatchHandlers<R>): R | undefined;
|
|
43
|
+
/** Returns a readonly snapshot of the value for rendering */
|
|
44
|
+
toSnapshot(): MaybeUndefined<InferEitherSnapshot<TVariants>, TRequired, THasDefault>;
|
|
45
|
+
}
|
|
46
|
+
interface EitherPrimitiveSchema<TVariants extends readonly ScalarPrimitive[]> {
|
|
47
|
+
readonly required: boolean;
|
|
48
|
+
readonly defaultValue: InferEitherState<TVariants> | undefined;
|
|
49
|
+
readonly variants: TVariants;
|
|
50
|
+
}
|
|
51
|
+
declare class EitherPrimitive<TVariants extends readonly ScalarPrimitive[], TRequired extends boolean = false, THasDefault extends boolean = false> implements Primitive<InferEitherState<TVariants>, EitherProxy<TVariants, TRequired, THasDefault>, TRequired, THasDefault, InferSetInput<TVariants, TRequired, THasDefault>, InferUpdateInput<TVariants, TRequired, THasDefault>> {
|
|
52
|
+
readonly _tag: "EitherPrimitive";
|
|
53
|
+
readonly _State: InferEitherState<TVariants>;
|
|
54
|
+
readonly _Proxy: EitherProxy<TVariants, TRequired, THasDefault>;
|
|
55
|
+
readonly _TRequired: TRequired;
|
|
56
|
+
readonly _THasDefault: THasDefault;
|
|
57
|
+
readonly TUpdateInput: InferUpdateInput<TVariants, TRequired, THasDefault>;
|
|
58
|
+
readonly TSetInput: InferSetInput<TVariants, TRequired, THasDefault>;
|
|
59
|
+
private readonly _schema;
|
|
60
|
+
private readonly _opDefinitions;
|
|
61
|
+
constructor(schema: EitherPrimitiveSchema<TVariants>);
|
|
62
|
+
/** Mark this either as required */
|
|
63
|
+
required(): EitherPrimitive<TVariants, true, THasDefault>;
|
|
64
|
+
/** Set a default value for this either */
|
|
65
|
+
default(defaultValue: InferEitherState<TVariants>): EitherPrimitive<TVariants, TRequired, true>;
|
|
66
|
+
/** Get the variants */
|
|
67
|
+
get variants(): TVariants;
|
|
68
|
+
/**
|
|
69
|
+
* Determine the type category of a value based on the variants
|
|
70
|
+
*/
|
|
71
|
+
private _getValueType;
|
|
72
|
+
/**
|
|
73
|
+
* Find the matching variant for a value.
|
|
74
|
+
* For literals, matches exact value. For other types, matches by typeof.
|
|
75
|
+
*/
|
|
76
|
+
private _findMatchingVariant;
|
|
77
|
+
/**
|
|
78
|
+
* Get the operation kind for a variant
|
|
79
|
+
*/
|
|
80
|
+
private _getVariantOperationKind;
|
|
81
|
+
/**
|
|
82
|
+
* Validate a value against the matching variant, including running its validators.
|
|
83
|
+
* Throws ValidationError if the value doesn't match any variant or fails validation.
|
|
84
|
+
*/
|
|
85
|
+
private _validateAndApplyToVariant;
|
|
86
|
+
readonly _internal: PrimitiveInternal<InferEitherState<TVariants>, EitherProxy<TVariants, TRequired, THasDefault>>;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Creates a new EitherPrimitive with the given scalar variant types.
|
|
90
|
+
* Validators defined on the variants are applied when validating values.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* // String or number
|
|
95
|
+
* const value = Either(String(), Number());
|
|
96
|
+
*
|
|
97
|
+
* // String, number, or boolean
|
|
98
|
+
* const status = Either(String(), Number(), Boolean()).default("pending");
|
|
99
|
+
*
|
|
100
|
+
* // With literal types
|
|
101
|
+
* const mode = Either(Literal("auto"), Literal("manual"), Number());
|
|
102
|
+
*
|
|
103
|
+
* // With validators - validates string length and number range
|
|
104
|
+
* const constrained = Either(
|
|
105
|
+
* String().min(2).max(50),
|
|
106
|
+
* Number().max(255)
|
|
107
|
+
* );
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
declare function Either<TVariants extends readonly ScalarPrimitive[]>(...variants: TVariants): EitherPrimitive<TVariants, false, false>;
|
|
111
|
+
//#endregion
|
|
112
|
+
export { Either, EitherMatchHandlers, EitherPrimitive, EitherProxy, InferEitherSnapshot, InferEitherState, ScalarPrimitive };
|
|
113
|
+
//# sourceMappingURL=Either.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Either.d.cts","names":[],"sources":["../../src/primitives/Either.ts"],"sourcesContent":[],"mappings":";;;;;;;KAiBK,yCAAyC,6FAA6F,WAAW,iBAAiB,YAAY,WAAW;KACzL,4CAA4C,6FAA6F,WAAW,iBAAiB,YAAY,WAAW;AAPtI;;;AAM2F,KAM1I,eAAA,GACR,eAPkJ,CAAA,GAAA,EAAA,GAAA,CAAA,GAQlJ,eARkJ,CAAA,GAAA,EAAA,GAAA,CAAA,GASlJ,gBATkJ,CAAA,GAAA,EAAA,GAAA,CAAA,GAUlJ,gBAVkJ,CAAA,GAAA,EAAA,GAAA,EAAA,GAAA,CAAA;;;;AAAD,KAezI,gBAfyI,CAAA,kBAAA,SAe7F,eAf6F,EAAA,CAAA,GAgBnJ,UAhBmJ,C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AAA,EA0C+B,WA1C/B,CAAA;EAGyB;EAApB,OAAA,CAAA,YAAA,EA+CG,gBA/CH,CA+CoB,SA/CpB,CAAA,CAAA,EA+CiC,eA/CjC,CA+CiD,SA/CjD,EA+C4D,SA/C5D,EAAA,IAAA,CAAA;EAAyB;EAGK,IAAA,QAAA,CAAA,CAAA,EAoDjC,SApDiC;EAApB;;;EAAf,QAAA,aAAA;EAAc;AAC7B;;;EAIwB,QAAA,oBAAA;EACJ;;AAGrB;EAAwD,QAAA,wBAAA;EAChB;;;;EAA8C,QAAA,0BAAA;EAAlC,SAAA,SAAA,EAoL9B,iBApL8B,CAoLZ,gBApLY,CAoLK,SApLL,CAAA,EAoLiB,WApLjB,CAoL6B,SApL7B,EAoLwC,SApLxC,EAoLmD,WApLnD,CAAA,CAAA;;;;;;;;;;;;;;;;;;;;;;;;AAQJ,iBAuRhC,MAvRgC,CAAA,kBAAA,SAuRE,eAvRF,EAAA,CAAA,CAAA,GAAA,QAAA,EAwRjC,SAxRiC,CAAA,EAyR7C,eAzR6C,CAyR7B,SAzR6B,EAAA,KAAA,EAAA,KAAA,CAAA"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { InferState, MaybeUndefined, NeedsValue, Primitive, PrimitiveInternal } from "./shared.mjs";
|
|
2
|
+
import { StringPrimitive } from "./String.mjs";
|
|
3
|
+
import { BooleanPrimitive } from "./Boolean.mjs";
|
|
4
|
+
import { NumberPrimitive } from "./Number.mjs";
|
|
5
|
+
import { LiteralPrimitive, LiteralValue } from "./Literal.mjs";
|
|
6
|
+
|
|
7
|
+
//#region src/primitives/Either.d.ts
|
|
8
|
+
type InferSetInput<TVariants extends readonly ScalarPrimitive[], TRequired extends boolean = false, THasDefault extends boolean = false> = NeedsValue<InferEitherState<TVariants>, TRequired, THasDefault>;
|
|
9
|
+
type InferUpdateInput<TVariants extends readonly ScalarPrimitive[], TRequired extends boolean = false, THasDefault extends boolean = false> = NeedsValue<InferEitherState<TVariants>, TRequired, THasDefault>;
|
|
10
|
+
/**
|
|
11
|
+
* Scalar primitives that can be used as variants in Either
|
|
12
|
+
*/
|
|
13
|
+
type ScalarPrimitive = StringPrimitive<any, any> | NumberPrimitive<any, any> | BooleanPrimitive<any, any> | LiteralPrimitive<any, any, any>;
|
|
14
|
+
/**
|
|
15
|
+
* Infer the union state type from a tuple of scalar primitives
|
|
16
|
+
*/
|
|
17
|
+
type InferEitherState<TVariants extends readonly ScalarPrimitive[]> = InferState<TVariants[number]>;
|
|
18
|
+
/**
|
|
19
|
+
* Infer the union snapshot type from a tuple of scalar primitives
|
|
20
|
+
*/
|
|
21
|
+
type InferEitherSnapshot<TVariants extends readonly ScalarPrimitive[]> = InferState<TVariants[number]>;
|
|
22
|
+
/**
|
|
23
|
+
* Match handlers for Either - optional handlers for each scalar type
|
|
24
|
+
*/
|
|
25
|
+
interface EitherMatchHandlers<R> {
|
|
26
|
+
string?: (value: string) => R;
|
|
27
|
+
number?: (value: number) => R;
|
|
28
|
+
boolean?: (value: boolean) => R;
|
|
29
|
+
literal?: (value: LiteralValue) => R;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Proxy for accessing Either values
|
|
33
|
+
*/
|
|
34
|
+
interface EitherProxy<TVariants extends readonly ScalarPrimitive[], TRequired extends boolean = false, THasDefault extends boolean = false> {
|
|
35
|
+
/** Gets the current value */
|
|
36
|
+
get(): MaybeUndefined<InferEitherState<TVariants>, TRequired, THasDefault>;
|
|
37
|
+
/** Sets the value to any of the allowed variant types */
|
|
38
|
+
set(value: InferSetInput<TVariants, TRequired, THasDefault>): void;
|
|
39
|
+
/** This is the same as set. Updates the value, generating an either.set operation */
|
|
40
|
+
update(value: InferUpdateInput<TVariants, TRequired, THasDefault>): void;
|
|
41
|
+
/** Pattern match on the value type */
|
|
42
|
+
match<R>(handlers: EitherMatchHandlers<R>): R | undefined;
|
|
43
|
+
/** Returns a readonly snapshot of the value for rendering */
|
|
44
|
+
toSnapshot(): MaybeUndefined<InferEitherSnapshot<TVariants>, TRequired, THasDefault>;
|
|
45
|
+
}
|
|
46
|
+
interface EitherPrimitiveSchema<TVariants extends readonly ScalarPrimitive[]> {
|
|
47
|
+
readonly required: boolean;
|
|
48
|
+
readonly defaultValue: InferEitherState<TVariants> | undefined;
|
|
49
|
+
readonly variants: TVariants;
|
|
50
|
+
}
|
|
51
|
+
declare class EitherPrimitive<TVariants extends readonly ScalarPrimitive[], TRequired extends boolean = false, THasDefault extends boolean = false> implements Primitive<InferEitherState<TVariants>, EitherProxy<TVariants, TRequired, THasDefault>, TRequired, THasDefault, InferSetInput<TVariants, TRequired, THasDefault>, InferUpdateInput<TVariants, TRequired, THasDefault>> {
|
|
52
|
+
readonly _tag: "EitherPrimitive";
|
|
53
|
+
readonly _State: InferEitherState<TVariants>;
|
|
54
|
+
readonly _Proxy: EitherProxy<TVariants, TRequired, THasDefault>;
|
|
55
|
+
readonly _TRequired: TRequired;
|
|
56
|
+
readonly _THasDefault: THasDefault;
|
|
57
|
+
readonly TUpdateInput: InferUpdateInput<TVariants, TRequired, THasDefault>;
|
|
58
|
+
readonly TSetInput: InferSetInput<TVariants, TRequired, THasDefault>;
|
|
59
|
+
private readonly _schema;
|
|
60
|
+
private readonly _opDefinitions;
|
|
61
|
+
constructor(schema: EitherPrimitiveSchema<TVariants>);
|
|
62
|
+
/** Mark this either as required */
|
|
63
|
+
required(): EitherPrimitive<TVariants, true, THasDefault>;
|
|
64
|
+
/** Set a default value for this either */
|
|
65
|
+
default(defaultValue: InferEitherState<TVariants>): EitherPrimitive<TVariants, TRequired, true>;
|
|
66
|
+
/** Get the variants */
|
|
67
|
+
get variants(): TVariants;
|
|
68
|
+
/**
|
|
69
|
+
* Determine the type category of a value based on the variants
|
|
70
|
+
*/
|
|
71
|
+
private _getValueType;
|
|
72
|
+
/**
|
|
73
|
+
* Find the matching variant for a value.
|
|
74
|
+
* For literals, matches exact value. For other types, matches by typeof.
|
|
75
|
+
*/
|
|
76
|
+
private _findMatchingVariant;
|
|
77
|
+
/**
|
|
78
|
+
* Get the operation kind for a variant
|
|
79
|
+
*/
|
|
80
|
+
private _getVariantOperationKind;
|
|
81
|
+
/**
|
|
82
|
+
* Validate a value against the matching variant, including running its validators.
|
|
83
|
+
* Throws ValidationError if the value doesn't match any variant or fails validation.
|
|
84
|
+
*/
|
|
85
|
+
private _validateAndApplyToVariant;
|
|
86
|
+
readonly _internal: PrimitiveInternal<InferEitherState<TVariants>, EitherProxy<TVariants, TRequired, THasDefault>>;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Creates a new EitherPrimitive with the given scalar variant types.
|
|
90
|
+
* Validators defined on the variants are applied when validating values.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* // String or number
|
|
95
|
+
* const value = Either(String(), Number());
|
|
96
|
+
*
|
|
97
|
+
* // String, number, or boolean
|
|
98
|
+
* const status = Either(String(), Number(), Boolean()).default("pending");
|
|
99
|
+
*
|
|
100
|
+
* // With literal types
|
|
101
|
+
* const mode = Either(Literal("auto"), Literal("manual"), Number());
|
|
102
|
+
*
|
|
103
|
+
* // With validators - validates string length and number range
|
|
104
|
+
* const constrained = Either(
|
|
105
|
+
* String().min(2).max(50),
|
|
106
|
+
* Number().max(255)
|
|
107
|
+
* );
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
declare function Either<TVariants extends readonly ScalarPrimitive[]>(...variants: TVariants): EitherPrimitive<TVariants, false, false>;
|
|
111
|
+
//#endregion
|
|
112
|
+
export { Either, EitherMatchHandlers, EitherPrimitive, EitherProxy, InferEitherSnapshot, InferEitherState, ScalarPrimitive };
|
|
113
|
+
//# sourceMappingURL=Either.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Either.d.mts","names":[],"sources":["../../src/primitives/Either.ts"],"sourcesContent":[],"mappings":";;;;;;;KAiBK,yCAAyC,6FAA6F,WAAW,iBAAiB,YAAY,WAAW;KAC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yBD,WAzBC;EAAW,SAAA,YAAA,EA0BZ,gBA1BY,CA0BK,SA1BL,EA0BgB,SA1BhB,EA0B2B,WA1B3B,CAAA;EAAW,SAAA,SAAA,EA2B1B,aA3B0B,CA2BZ,SA3BY,EA2BD,SA3BC,EA2BU,WA3BV,CAAA;EAApC,iBAAA,OAAA;EAGoB,iBAAA,cAAA;EAAW,WAAA,CAAA,MAAA,EAqCtB,qBArCsB,CAqCA,SArCA,CAAA;EAAW;EAAvC,QAAA,CAAA,CAAA,EA0CF,eA1CE,CA0Cc,SA1Cd,EAAA,IAAA,EA0C+B,WA1C/B,CAAA;EAGyB;EAApB,OAAA,CAAA,YAAA,EA+CG,gBA/CH,CA+CoB,SA/CpB,CAAA,CAAA,EA+CiC,eA/CjC,CA+CiD,SA/CjD,EA+C4D,SA/C5D,EAAA,IAAA,CAAA;EAAyB;EAGK,IAAA,QAAA,CAAA,CAAA,EAoDjC,SApDiC;EAApB;;;EAAf,QAAA,aAAA;EAAc;AAC7B;;;EAIwB,QAAA,oBAAA;EACJ;;AAGrB;EAAwD,QAAA,wBAAA;EAChB;;;;EAA8C,QAAA,0BAAA;EAAlC,SAAA,SAAA,EAoL9B,iBApL8B,CAoLZ,gBApLY,CAoLK,SApLL,CAAA,EAoLiB,WApLjB,CAoL6B,SApL7B,EAoLwC,SApLxC,EAoLmD,WApLnD,CAAA,CAAA;;;;;;;;;;;;;;;;;;;;;;;;AAQJ,iBAuRhC,MAvRgC,CAAA,kBAAA,SAuRE,eAvRF,EAAA,CAAA,CAAA,GAAA,QAAA,EAwRjC,SAxRiC,CAAA,EAyR7C,eAzR6C,CAyR7B,SAzR6B,EAAA,KAAA,EAAA,KAAA,CAAA"}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { _defineProperty } from "../_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.mjs";
|
|
2
|
+
import { _objectSpread2 } from "../_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.mjs";
|
|
3
|
+
import { ValidationError } from "./shared.mjs";
|
|
4
|
+
import { make } from "../OperationDefinition.mjs";
|
|
5
|
+
import { pathsOverlap } from "../OperationPath.mjs";
|
|
6
|
+
import { fromDefinition } from "../Operation.mjs";
|
|
7
|
+
import { Schema } from "effect";
|
|
8
|
+
|
|
9
|
+
//#region src/primitives/Either.ts
|
|
10
|
+
var EitherPrimitive = class EitherPrimitive {
|
|
11
|
+
constructor(schema) {
|
|
12
|
+
_defineProperty(this, "_tag", "EitherPrimitive");
|
|
13
|
+
_defineProperty(this, "_State", void 0);
|
|
14
|
+
_defineProperty(this, "_Proxy", void 0);
|
|
15
|
+
_defineProperty(this, "_TRequired", void 0);
|
|
16
|
+
_defineProperty(this, "_THasDefault", void 0);
|
|
17
|
+
_defineProperty(this, "TUpdateInput", void 0);
|
|
18
|
+
_defineProperty(this, "TSetInput", void 0);
|
|
19
|
+
_defineProperty(this, "_schema", void 0);
|
|
20
|
+
_defineProperty(this, "_opDefinitions", { set: make({
|
|
21
|
+
kind: "either.set",
|
|
22
|
+
payload: Schema.Unknown,
|
|
23
|
+
target: Schema.Unknown,
|
|
24
|
+
apply: (payload) => payload
|
|
25
|
+
}) });
|
|
26
|
+
_defineProperty(this, "_internal", {
|
|
27
|
+
createProxy: (env, operationPath) => {
|
|
28
|
+
const defaultValue = this._schema.defaultValue;
|
|
29
|
+
return {
|
|
30
|
+
get: () => {
|
|
31
|
+
const state = env.getState(operationPath);
|
|
32
|
+
return state !== null && state !== void 0 ? state : defaultValue;
|
|
33
|
+
},
|
|
34
|
+
set: (value) => {
|
|
35
|
+
env.addOperation(fromDefinition(operationPath, this._opDefinitions.set, value));
|
|
36
|
+
},
|
|
37
|
+
update: (value) => {
|
|
38
|
+
env.addOperation(fromDefinition(operationPath, this._opDefinitions.set, value));
|
|
39
|
+
},
|
|
40
|
+
match: (handlers) => {
|
|
41
|
+
const currentState = env.getState(operationPath);
|
|
42
|
+
const effectiveState = currentState !== null && currentState !== void 0 ? currentState : defaultValue;
|
|
43
|
+
if (effectiveState === void 0) return void 0;
|
|
44
|
+
const valueType = this._getValueType(effectiveState);
|
|
45
|
+
if (!valueType) return void 0;
|
|
46
|
+
switch (valueType) {
|
|
47
|
+
case "string":
|
|
48
|
+
var _handlers$string;
|
|
49
|
+
return (_handlers$string = handlers.string) === null || _handlers$string === void 0 ? void 0 : _handlers$string.call(handlers, effectiveState);
|
|
50
|
+
case "number":
|
|
51
|
+
var _handlers$number;
|
|
52
|
+
return (_handlers$number = handlers.number) === null || _handlers$number === void 0 ? void 0 : _handlers$number.call(handlers, effectiveState);
|
|
53
|
+
case "boolean":
|
|
54
|
+
var _handlers$boolean;
|
|
55
|
+
return (_handlers$boolean = handlers.boolean) === null || _handlers$boolean === void 0 ? void 0 : _handlers$boolean.call(handlers, effectiveState);
|
|
56
|
+
case "literal":
|
|
57
|
+
var _handlers$literal;
|
|
58
|
+
return (_handlers$literal = handlers.literal) === null || _handlers$literal === void 0 ? void 0 : _handlers$literal.call(handlers, effectiveState);
|
|
59
|
+
default: return;
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
toSnapshot: () => {
|
|
63
|
+
const state = env.getState(operationPath);
|
|
64
|
+
return state !== null && state !== void 0 ? state : defaultValue;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
},
|
|
68
|
+
applyOperation: (_state, operation) => {
|
|
69
|
+
if (operation.kind !== "either.set") throw new ValidationError(`EitherPrimitive cannot apply operation of kind: ${operation.kind}`);
|
|
70
|
+
const payload = operation.payload;
|
|
71
|
+
this._validateAndApplyToVariant(payload, operation.path);
|
|
72
|
+
return payload;
|
|
73
|
+
},
|
|
74
|
+
getInitialState: () => {
|
|
75
|
+
return this._schema.defaultValue;
|
|
76
|
+
},
|
|
77
|
+
transformOperation: (clientOp, serverOp) => {
|
|
78
|
+
if (!pathsOverlap(clientOp.path, serverOp.path)) return {
|
|
79
|
+
type: "transformed",
|
|
80
|
+
operation: clientOp
|
|
81
|
+
};
|
|
82
|
+
return {
|
|
83
|
+
type: "transformed",
|
|
84
|
+
operation: clientOp
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
this._schema = schema;
|
|
89
|
+
}
|
|
90
|
+
/** Mark this either as required */
|
|
91
|
+
required() {
|
|
92
|
+
return new EitherPrimitive(_objectSpread2(_objectSpread2({}, this._schema), {}, { required: true }));
|
|
93
|
+
}
|
|
94
|
+
/** Set a default value for this either */
|
|
95
|
+
default(defaultValue) {
|
|
96
|
+
return new EitherPrimitive(_objectSpread2(_objectSpread2({}, this._schema), {}, { defaultValue }));
|
|
97
|
+
}
|
|
98
|
+
/** Get the variants */
|
|
99
|
+
get variants() {
|
|
100
|
+
return this._schema.variants;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Determine the type category of a value based on the variants
|
|
104
|
+
*/
|
|
105
|
+
_getValueType(value) {
|
|
106
|
+
const valueType = typeof value;
|
|
107
|
+
for (const variant of this._schema.variants) if (variant._tag === "LiteralPrimitive") {
|
|
108
|
+
if (value === variant.literal) return "literal";
|
|
109
|
+
}
|
|
110
|
+
if (valueType === "string") {
|
|
111
|
+
for (const variant of this._schema.variants) if (variant._tag === "StringPrimitive") return "string";
|
|
112
|
+
}
|
|
113
|
+
if (valueType === "number") {
|
|
114
|
+
for (const variant of this._schema.variants) if (variant._tag === "NumberPrimitive") return "number";
|
|
115
|
+
}
|
|
116
|
+
if (valueType === "boolean") {
|
|
117
|
+
for (const variant of this._schema.variants) if (variant._tag === "BooleanPrimitive") return "boolean";
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Find the matching variant for a value.
|
|
122
|
+
* For literals, matches exact value. For other types, matches by typeof.
|
|
123
|
+
*/
|
|
124
|
+
_findMatchingVariant(value) {
|
|
125
|
+
const valueType = typeof value;
|
|
126
|
+
for (const variant of this._schema.variants) if (variant._tag === "LiteralPrimitive") {
|
|
127
|
+
if (value === variant.literal) return variant;
|
|
128
|
+
}
|
|
129
|
+
if (valueType === "string") {
|
|
130
|
+
for (const variant of this._schema.variants) if (variant._tag === "StringPrimitive") return variant;
|
|
131
|
+
}
|
|
132
|
+
if (valueType === "number") {
|
|
133
|
+
for (const variant of this._schema.variants) if (variant._tag === "NumberPrimitive") return variant;
|
|
134
|
+
}
|
|
135
|
+
if (valueType === "boolean") {
|
|
136
|
+
for (const variant of this._schema.variants) if (variant._tag === "BooleanPrimitive") return variant;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Get the operation kind for a variant
|
|
141
|
+
*/
|
|
142
|
+
_getVariantOperationKind(variant) {
|
|
143
|
+
switch (variant._tag) {
|
|
144
|
+
case "StringPrimitive": return "string.set";
|
|
145
|
+
case "NumberPrimitive": return "number.set";
|
|
146
|
+
case "BooleanPrimitive": return "boolean.set";
|
|
147
|
+
case "LiteralPrimitive": return "literal.set";
|
|
148
|
+
default: return "unknown.set";
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Validate a value against the matching variant, including running its validators.
|
|
153
|
+
* Throws ValidationError if the value doesn't match any variant or fails validation.
|
|
154
|
+
*/
|
|
155
|
+
_validateAndApplyToVariant(value, path) {
|
|
156
|
+
const matchingVariant = this._findMatchingVariant(value);
|
|
157
|
+
if (!matchingVariant) throw new ValidationError(`EitherPrimitive.set requires a value matching one of: ${this._schema.variants.map((v) => v._tag).join(", ")}, got: ${typeof value}`);
|
|
158
|
+
const syntheticOp = {
|
|
159
|
+
kind: this._getVariantOperationKind(matchingVariant),
|
|
160
|
+
path,
|
|
161
|
+
payload: value
|
|
162
|
+
};
|
|
163
|
+
matchingVariant._internal.applyOperation(void 0, syntheticOp);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
/**
|
|
167
|
+
* Creates a new EitherPrimitive with the given scalar variant types.
|
|
168
|
+
* Validators defined on the variants are applied when validating values.
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```typescript
|
|
172
|
+
* // String or number
|
|
173
|
+
* const value = Either(String(), Number());
|
|
174
|
+
*
|
|
175
|
+
* // String, number, or boolean
|
|
176
|
+
* const status = Either(String(), Number(), Boolean()).default("pending");
|
|
177
|
+
*
|
|
178
|
+
* // With literal types
|
|
179
|
+
* const mode = Either(Literal("auto"), Literal("manual"), Number());
|
|
180
|
+
*
|
|
181
|
+
* // With validators - validates string length and number range
|
|
182
|
+
* const constrained = Either(
|
|
183
|
+
* String().min(2).max(50),
|
|
184
|
+
* Number().max(255)
|
|
185
|
+
* );
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
function Either(...variants) {
|
|
189
|
+
if (variants.length === 0) throw new ValidationError("Either requires at least one variant");
|
|
190
|
+
return new EitherPrimitive({
|
|
191
|
+
required: false,
|
|
192
|
+
defaultValue: void 0,
|
|
193
|
+
variants
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
//#endregion
|
|
198
|
+
export { Either, EitherPrimitive };
|
|
199
|
+
//# sourceMappingURL=Either.mjs.map
|