0.1.4 #5
@@ -1,4 +1,4 @@
|
||||
import { Effectable, Option, Schema, Subscribable, SubscriptionRef, type ParseResult, type Pipeable } from "effect"
|
||||
import { Effect, Option, Pipeable, Schema, Subscribable, SubscriptionRef, type ParseResult } from "effect"
|
||||
import type * as PropertyPath from "./PropertyPath.js"
|
||||
|
||||
|
||||
@@ -11,13 +11,31 @@ extends Pipeable.Pipeable {
|
||||
readonly valueRef: SubscriptionRef.SubscriptionRef<A>
|
||||
readonly errorSubscribable: Subscribable.Subscribable<Option.Option<ParseResult.ParseError>>
|
||||
|
||||
useRef<P extends PropertyPath.Paths<A>>(path: P): SubscriptionRef.SubscriptionRef<PropertyPath.ValueFromPath<A, P>>
|
||||
useIssues(path: PropertyPath.Paths<A>): readonly ParseResult.ParseIssue[]
|
||||
useRef<P extends PropertyPath.Paths<A>>(path: P): Effect.Effect<SubscriptionRef.SubscriptionRef<PropertyPath.ValueFromPath<A, P>>>
|
||||
useIssues(path: PropertyPath.Paths<A>): Effect.Effect<readonly ParseResult.ParseIssue[]>
|
||||
}
|
||||
|
||||
class FormImpl<in out A, in out I, out R>
|
||||
extends Effectable.Class<> implements Form<A, I, R> {
|
||||
extends Pipeable.Class() implements Form<A, I, R> {
|
||||
readonly [TypeId]: TypeId = TypeId
|
||||
|
||||
constructor(
|
||||
readonly schema: Schema.Schema<A, I, R>,
|
||||
readonly valueRef: SubscriptionRef.SubscriptionRef<A>,
|
||||
readonly errorSubscribable: Subscribable.Subscribable<Option.Option<ParseResult.ParseError>>
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
useRef<P extends PropertyPath.Paths<A>>(
|
||||
path: P
|
||||
): Effect.Effect<SubscriptionRef.SubscriptionRef<PropertyPath.ValueFromPath<A, P>>, never, never> {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
|
||||
useIssues(path: PropertyPath.Paths<A>): Effect.Effect<readonly ParseResult.ParseIssue[]> {
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
118
packages/effect-fc/src/types/Schema.ts
Normal file
118
packages/effect-fc/src/types/Schema.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
import { Array, Function, Option, Predicate, Schema } from "effect"
|
||||
|
||||
|
||||
type Prev = readonly [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
|
||||
export type Paths<S, D extends number = 5, Seen = never> = readonly [] | (
|
||||
D extends never ? readonly [] :
|
||||
S extends Seen ? readonly [] :
|
||||
S extends Schema.Union<infer Members> ? UnionPaths<Members, D, Seen | S> :
|
||||
S extends Schema.TupleType<infer Elements, infer Rest> ? FormField.TupleFormField<S, Elements, Rest> :
|
||||
S extends Schema.Array$<infer Value> ? FormField.ArrayFormField<S, Value> :
|
||||
S extends Schema.Struct<infer Fields> ? FormField.StructFormField<S, Fields> :
|
||||
S extends Schema.Schema.Any ? FormField.GenericFormField<S> :
|
||||
S extends Schema.PropertySignature<
|
||||
infer TypeToken,
|
||||
infer Type,
|
||||
infer Key,
|
||||
infer EncodedToken,
|
||||
infer Encoded,
|
||||
infer HasDefault,
|
||||
infer R
|
||||
> ? FormField.PropertySignatureFormField<S, TypeToken, Type, Key, EncodedToken, Encoded, HasDefault, R> :
|
||||
never
|
||||
)
|
||||
|
||||
export type UnionPaths<Members extends ReadonlyArray<Schema.Schema.All>, D extends number, Seen> = {
|
||||
[K in keyof T as K extends number ? K : never]:
|
||||
| readonly [K]
|
||||
| readonly [K, ...Paths<T[K], Prev[D], Seen>]
|
||||
} extends infer O
|
||||
? O[keyof O]
|
||||
: never
|
||||
|
||||
export type ArrayPaths<T extends readonly any[], D extends number, Seen> = {
|
||||
[K in keyof T as K extends number ? K : never]:
|
||||
| readonly [K]
|
||||
| readonly [K, ...Paths<T[K], Prev[D], Seen>]
|
||||
} extends infer O
|
||||
? O[keyof O]
|
||||
: never
|
||||
|
||||
export type ObjectPaths<T extends object, D extends number, Seen> = {
|
||||
[K in keyof T as K extends string | number | symbol ? K : never]-?:
|
||||
NonNullable<T[K]> extends infer V
|
||||
? readonly [K] | readonly [K, ...Paths<V, Prev[D], Seen>]
|
||||
: never
|
||||
} extends infer O
|
||||
? O[keyof O]
|
||||
: never
|
||||
|
||||
export type ValueFromPath<T, P extends readonly any[]> = P extends [infer Head, ...infer Tail]
|
||||
? Head extends keyof T
|
||||
? ValueFromPath<T[Head], Tail>
|
||||
: T extends readonly any[]
|
||||
? Head extends number
|
||||
? ValueFromPath<T[number], Tail>
|
||||
: never
|
||||
: never
|
||||
: T
|
||||
|
||||
export type AnyPath = readonly PropertyKey[]
|
||||
|
||||
|
||||
export const unsafeGet: {
|
||||
<T, const P extends Paths<T>>(path: P): (self: T) => ValueFromPath<T, P>
|
||||
<T, const P extends Paths<T>>(self: T, path: P): ValueFromPath<T, P>
|
||||
} = Function.dual(2, <T, const P extends Paths<T>>(self: T, path: P): ValueFromPath<T, P> =>
|
||||
path.reduce((acc: any, key: any) => acc?.[key], self)
|
||||
)
|
||||
|
||||
export const get: {
|
||||
<T, const P extends Paths<T>>(path: P): (self: T) => Option.Option<ValueFromPath<T, P>>
|
||||
<T, const P extends Paths<T>>(self: T, path: P): Option.Option<ValueFromPath<T, P>>
|
||||
} = Function.dual(2, <T, const P extends Paths<T>>(self: T, path: P): Option.Option<ValueFromPath<T, P>> =>
|
||||
path.reduce(
|
||||
(acc: Option.Option<any>, key: any): Option.Option<any> => Option.isSome(acc)
|
||||
? Predicate.hasProperty(acc.value, key)
|
||||
? Option.some(acc.value[key])
|
||||
: Option.none()
|
||||
: acc,
|
||||
|
||||
Option.some(self),
|
||||
)
|
||||
)
|
||||
|
||||
export const immutableSet: {
|
||||
<T, const P extends Paths<T>>(path: P, value: ValueFromPath<T, P>): (self: T) => ValueFromPath<T, P>
|
||||
<T, const P extends Paths<T>>(self: T, path: P, value: ValueFromPath<T, P>): Option.Option<T>
|
||||
} = Function.dual(3, <T, const P extends Paths<T>>(self: T, path: P, value: ValueFromPath<T, P>): Option.Option<T> => {
|
||||
const key = Array.head(path as AnyPath)
|
||||
if (Option.isNone(key))
|
||||
return Option.some(value as T)
|
||||
if (!Predicate.hasProperty(self, key.value))
|
||||
return Option.none()
|
||||
|
||||
const child = immutableSet<any, any>(self[key.value], Option.getOrThrow(Array.tail(path as AnyPath)), value)
|
||||
if (Option.isNone(child))
|
||||
return child
|
||||
|
||||
if (Array.isArray(self))
|
||||
return typeof key.value === "number"
|
||||
? Option.some([
|
||||
...self.slice(0, key.value),
|
||||
child.value,
|
||||
...self.slice(key.value + 1),
|
||||
] as T)
|
||||
: Option.none()
|
||||
|
||||
if (typeof self === "object")
|
||||
return Option.some(
|
||||
Object.assign(
|
||||
Object.create(Object.getPrototypeOf(self)),
|
||||
{ ...self, [key.value]: child.value },
|
||||
)
|
||||
)
|
||||
|
||||
return Option.none()
|
||||
})
|
||||
Reference in New Issue
Block a user