Skip to content

Commit

Permalink
Remove unnecessary type assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
aynik committed Sep 5, 2021
1 parent 55d72fc commit b40b92c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "proxy-lens",
"version": "1.4.1",
"version": "1.4.2",
"description": "A type safe functional lens implemented via proxy",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
52 changes: 27 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ export type BaseLens<A, B> = {
get(target?: A): B
set(value: B, target?: A): A
let(value: B): ProxyLens<A, A>
peg(get: Getter<A, B>): ProxyLens<A, A>
mod(get: Getter<B, B>): ProxyLens<A, A>
iso<C>(get: Getter<B, C>, set: Setter<B, C>): ProxyLens<A, C>
peg(get: Getter<A, B> | ProxyLens<A, B>): ProxyLens<A, A>
mod(get: Getter<B, B> | ProxyLens<B, B>): ProxyLens<A, A>
iso<C>(
get: Getter<B, C> | ProxyLens<B, C>,
set: Setter<B, C>,
): ProxyLens<A, C>
}

type ExtractRecord<T> = Extract<T, { [key: string]: unknown }>
Expand All @@ -27,17 +30,22 @@ type ExtractArray<T> = Extract<T, ReadonlyArray<unknown>>
export type ArrayLens<A, B extends ArrayFrom<B>> = {
del(index: number): ProxyLens<A, A>
put(index: number, value: ArrayItem<B> | B): ProxyLens<A, A>
map(get_: Getter<ArrayItem<B>, ArrayItem<B>>): ProxyLens<A, A>
tap(get_?: Getter<ArrayItem<B>, boolean>): ProxyTraversal<A, B>
map(
get_:
| Getter<ArrayItem<B>, ArrayItem<B>>
| ProxyLens<ArrayItem<B>, ArrayItem<B>>,
): ProxyLens<A, A>
tap(
get_?: Getter<ArrayItem<B>, boolean> | ProxyLens<ArrayItem<B>, boolean>,
): ProxyTraversal<A, B>
}

type ArrayProxyLens<A, B extends ArrayFrom<B>> = ArrayLens<A, B> &
{
[I in keyof Omit<B, Exclude<keyof B, number>>]: ProxyLens<A, B[I]>
}

export type ProxyLens<A, B> = Getter<A, B> &
BaseProxyLens<A, B> &
export type ProxyLens<A, B> = BaseProxyLens<A, B> &
(ExtractArray<B> extends never
? unknown
: ArrayProxyLens<A, ExtractArray<ArrayFrom<B>>>)
Expand All @@ -60,18 +68,13 @@ type ArrayProxyTraversal<A, B extends ArrayFrom<B>> = ArrayLens<A, B> &
>
}

export type ProxyTraversal<A, B> = Getter<A, ArrayItem<B>> &
BaseProxyTraversal<A, ArrayItem<B>> &
export type ProxyTraversal<A, B> = BaseProxyTraversal<A, ArrayItem<B>> &
(ExtractArray<B> extends never
? unknown
: ArrayProxyTraversal<A, ExtractArray<ArrayFrom<ArrayItem<B>>>>)

function baseLens<A, B>(
get: Getter<A, B>,
set: Setter<A, B>,
root?: A,
): BaseLens<A, B> {
return {
function baseLens<A, B>(get: Getter<A, B>, set: Setter<A, B>, root?: A) {
return <BaseLens<A, B>>{
get: (target: A): B => get((target ?? root) as A),
set: (value: B, target: A): A => set(value, (target ?? root) as A),
let: (value: B): ProxyLens<A, A> =>
Expand All @@ -98,15 +101,15 @@ function baseLens<A, B>(
(value: C, target: A): A => set(set_(value, get(target)), target),
root,
),
} as BaseLens<A, B>
}
}

function arrayLens<A, B extends ArrayFrom<B>>(
get: Getter<A, ArrayFrom<B>>,
set: Setter<A, ArrayFrom<B>>,
root?: A,
): ArrayLens<A, ArrayFrom<B>> {
return {
) {
return <ArrayLens<A, ArrayFrom<B>>>{
del: (index: number): ProxyLens<A, A> =>
proxyLens<A, A>(
(target: A): A =>
Expand Down Expand Up @@ -198,20 +201,19 @@ function dispatch<A, B>(
}

function proxyLens<A, B>(
get: Getter<A, B> | Getter<A, ArrayFrom<B>>,
set: Setter<A, B> | Setter<A, ArrayFrom<B>>,
get: Getter<A, B>,
set: Setter<A, B>,
root?: A,
): ProxyLens<A, B> {
return new Proxy(get as ProxyLens<A, B>, {
apply: (_, __, args: (A | void)[]): B =>
(get as Getter<A, B>)(root ?? (args[0] as A)),
apply: (_, __, args: (A | void)[]): B => get(root ?? (args[0] as A)),
get: (_, key) =>
dispatch<A, B>(key, get, set, root) ||
proxyLens<A, B[keyof B]>(
(target: A): B[keyof B] =>
Object.assign(getTarget<B>(key), get(target))[key as keyof B],
(value: B[keyof B], target: A): A =>
(set as Setter<A, B>)(
set(
Object.assign(getTarget<B>(key), get(target), {
[key as keyof B]: value,
}),
Expand All @@ -223,8 +225,8 @@ function proxyLens<A, B>(
}

function proxyTraversal<A, B extends ArrayFrom<B>>(
get: Getter<A, ArrayFrom<B>> | Getter<A, ArrayFrom<ArrayFrom<B>>>,
set: Setter<A, ArrayFrom<B>> | Setter<A, ArrayFrom<ArrayFrom<B>>>,
get: Getter<A, ArrayFrom<B>>,
set: Setter<A, ArrayFrom<B>>,
root?: A,
): ProxyTraversal<A, ArrayFrom<B>> {
return new Proxy(get as ProxyTraversal<A, ArrayFrom<B>>, {
Expand Down

0 comments on commit b40b92c

Please sign in to comment.