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

Class OptionalImpl

packages/effect/src/Optic.ts:968–1121  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

966export interface Traversal<in out S, in out A> extends Optional<S, ReadonlyArray<A>> {}
967
968class OptionalImpl<S, A> implements Optional<S, A> {
969 /** @internal */
970 readonly node: Node
971 readonly getResult: (s: S) => Result.Result<A, SchemaIssue.Issue>
972 readonly replaceResult: (a: A, s: S) => Result.Result<S, SchemaIssue.Issue>
973 constructor(
974 node: Node,
975 getResult: (s: S) => Result.Result<A, SchemaIssue.Issue>,
976 replaceResult: (a: A, s: S) => Result.Result<S, SchemaIssue.Issue>
977 ) {
978 this.node = node
979 this.getResult = getResult
980 this.replaceResult = replaceResult
981 }
982 replace(a: A, s: S): S {
983 return Result.getOrElse(this.replaceResult(a, s), () => s)
984 }
985 modify(f: (a: A) => A): (s: S) => S {
986 return (s) => Result.getOrElse(Result.flatMap(this.getResult(s), (a) => this.replaceResult(f(a), s)), () => s)
987 }
988 compose(that: any): any {
989 return make(compose(this.node, that.node))
990 }
991 key(key: PropertyKey): any {
992 return make(compose(this.node, [new PathNode([key])]))
993 }
994 optionalKey(key: PropertyKey): any {
995 return make(
996 compose(
997 this.node,
998 primitiveNode(
999 "Lens",
1000 (s) => s[key],
1001 (a, s) => {
1002 const copy = cloneShallow(s)
1003 if (a === undefined) {
1004 if (Array.isArray(copy) && typeof key === "number") {
1005 copy.splice(key, 1)
1006 } else {
1007 delete copy[key]
1008 }
1009 } else {
1010 InternalRecord.assignProperty(copy, key, a)
1011 }
1012 return copy
1013 }
1014 )
1015 )
1016 )
1017 }
1018 check(...checks: readonly [SchemaAST.Check<any>, ...Array<SchemaAST.Check<any>>]): any {
1019 return make(compose(this.node, [new CheckNode(checks)]))
1020 }
1021 refine<B extends A>(refinement: (a: A) => a is B, annotations?: Schema.Annotations.Filter): any {
1022 return make(compose(this.node, [new CheckNode([SchemaAST.makeFilterByGuard(refinement, annotations)])]))
1023 }
1024 tag(tag: string): any {
1025 const err = Result.fail(new SchemaIssue.InvalidValue({ expected: `${JSON.stringify(tag)} tag` }))

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected