immutable-lens
Advanced tools
Comparing version 0.3.4 to 0.4.0
@@ -1,13 +0,11 @@ | ||
export interface NotAnArray { | ||
reduceRight?: 'NotAnArray'; | ||
} | ||
export declare type PlainObject<T> = T extends any[] ? never : T extends (...args: any[]) => any ? never : T extends object ? T : never; | ||
export declare type Updater<T> = (value: T) => T; | ||
export declare type FieldValues<T> = object & NotAnArray & Partial<T>; | ||
export declare type FieldUpdaters<T> = object & NotAnArray & { | ||
export declare type FieldValues<T> = Partial<PlainObject<T>>; | ||
export declare type FieldUpdaters<T> = PlainObject<{ | ||
[K in keyof T]?: Updater<T[K]>; | ||
}; | ||
}>; | ||
export declare type FieldsUpdater<T> = (value: T) => FieldValues<T>; | ||
export declare type FieldLenses<T, Composition> = object & NotAnArray & { | ||
export declare type FieldLenses<T, Composition> = PlainObject<{ | ||
[K in keyof Composition]: Lens<T, Composition[K]>; | ||
}; | ||
}>; | ||
export interface Lens<Source, Target> { | ||
@@ -18,4 +16,4 @@ readonly path: string; | ||
focusIndex<Item>(this: Lens<Source, Item[]>, index: number): Lens<Source, Item | undefined>; | ||
recompose<Composition>(this: Lens<Source, Target & object & NotAnArray>, fields: FieldLenses<Target, Composition>): Lens<Source, Composition>; | ||
focusPath<K extends keyof Target>(this: Lens<Source, Target & NotAnArray>, key: K): Lens<Source, Target[K]>; | ||
recompose<Composition>(this: Lens<Source, PlainObject<Target>>, fields: FieldLenses<Target, Composition>): Lens<Source, Composition>; | ||
focusPath<K extends keyof Target>(this: Lens<Source, PlainObject<Target>>, key: K): Lens<Source, Target[K]>; | ||
focusPath<K1 extends keyof Target, K2 extends keyof Target[K1]>(key1: K1, key2: K2): Lens<Source, Target[K1][K2]>; | ||
@@ -31,6 +29,6 @@ focusPath<K1 extends keyof Target, K2 extends keyof Target[K1], K3 extends keyof Target[K1][K2]>(key1: K1, key2: K2, key3: K3): Lens<Source, Target[K1][K2][K3]>; | ||
update(updater: Updater<Target>): Updater<Source>; | ||
setFields(this: Lens<Source, Target & NotAnArray>, newValues: FieldValues<Target>): Updater<Source>; | ||
setFields(this: Lens<Source, Target & NotAnArray>): (newValues: FieldValues<Target>) => Updater<Source>; | ||
updateFields(this: Lens<Source, Target & NotAnArray>, updaters: FieldUpdaters<Target>): Updater<Source>; | ||
updatePartial(this: Lens<Source, Target & NotAnArray>, fieldsUpdater: FieldsUpdater<Target>): Updater<Source>; | ||
setFields(this: Lens<Source, PlainObject<Target>>, newValues: FieldValues<Target>): Updater<Source>; | ||
setFields(this: Lens<Source, PlainObject<Target>>): (newValues: FieldValues<Target>) => Updater<Source>; | ||
updateFields(this: Lens<Source, PlainObject<Target>>, updaters: FieldUpdaters<Target>): Updater<Source>; | ||
updatePartial(this: Lens<Source, PlainObject<Target>>, fieldsUpdater: FieldsUpdater<Target>): Updater<Source>; | ||
pipe(...updaters: Array<Updater<Target>>): Updater<Source>; | ||
@@ -37,0 +35,0 @@ defaultTo<SafeTarget extends Target>(this: Lens<Source, SafeTarget | undefined>, value: SafeTarget): Lens<Source, SafeTarget>; |
{ | ||
"name": "immutable-lens", | ||
"version": "0.3.4", | ||
"version": "0.4.0", | ||
"description": "Type-safe Lens API for immutable updates in complex data structures", | ||
@@ -41,4 +41,4 @@ "keywords": [ | ||
"devDependencies": { | ||
"@types/chai": "^4.1.3", | ||
"@types/mocha": "^5.2.1", | ||
"@types/chai": "^4.1.4", | ||
"@types/mocha": "^5.2.2", | ||
"chai": "^4.1.2", | ||
@@ -48,11 +48,11 @@ "chalk": "^2.4.1", | ||
"mocha": "^5.2.0", | ||
"prettier": "^1.13.4", | ||
"prettier": "^1.13.5", | ||
"ramda": "^0.25.0", | ||
"shx": "^0.2.2", | ||
"ts-node": "^6.1.0", | ||
"shx": "^0.3.0", | ||
"ts-node": "^6.1.1", | ||
"tslint": "^5.10.0", | ||
"tslint-config-prettier": "^1.13.0", | ||
"tslint-plugin-prettier": "^1.3.0", | ||
"typescript": "^2.9.1" | ||
"typescript": "^2.9.2" | ||
} | ||
} |
@@ -8,3 +8,2 @@ import { cherryPick } from './cherryPick' | ||
Lens, | ||
NotAnArray, | ||
UnfocusedLens, | ||
@@ -56,3 +55,3 @@ Updater, | ||
private focusOn<K extends keyof Target>( | ||
this: Lens<Source, Target & NotAnArray>, | ||
this: Lens<Source, Target>, | ||
key: K, | ||
@@ -59,0 +58,0 @@ ): Lens<Source, Target[K]> { |
@@ -124,6 +124,1 @@ import { createLens } from '../src/createLens' | ||
lens.recompose(() => null) | ||
// Updating field values with unknown prop @shouldNotButDoesCompile | ||
lens.updatePartial(state => ({ | ||
unknown: 'unknown', | ||
})) |
@@ -1,18 +0,16 @@ | ||
export interface NotAnArray { | ||
reduceRight?: 'NotAnArray' | ||
} | ||
export type PlainObject<T> = T extends any[] | ||
? never | ||
: T extends (...args: any[]) => any ? never : T extends object ? T : never | ||
export type Updater<T> = (value: T) => T | ||
export type FieldValues<T> = object & NotAnArray & Partial<T> | ||
export type FieldValues<T> = Partial<PlainObject<T>> | ||
export type FieldUpdaters<T> = object & | ||
NotAnArray & | ||
{ [K in keyof T]?: Updater<T[K]> } | ||
export type FieldUpdaters<T> = PlainObject<{ [K in keyof T]?: Updater<T[K]> }> | ||
export type FieldsUpdater<T> = (value: T) => FieldValues<T> | ||
export type FieldLenses<T, Composition> = object & | ||
NotAnArray & | ||
export type FieldLenses<T, Composition> = PlainObject< | ||
{ [K in keyof Composition]: Lens<T, Composition[K]> } | ||
> | ||
@@ -42,3 +40,3 @@ export interface Lens<Source, Target> { | ||
recompose<Composition>( | ||
this: Lens<Source, Target & object & NotAnArray>, | ||
this: Lens<Source, PlainObject<Target>>, | ||
fields: FieldLenses<Target, Composition>, | ||
@@ -48,3 +46,3 @@ ): Lens<Source, Composition> | ||
focusPath<K extends keyof Target>( | ||
this: Lens<Source, Target & NotAnArray>, | ||
this: Lens<Source, PlainObject<Target>>, | ||
key: K, | ||
@@ -145,3 +143,3 @@ ): Lens<Source, Target[K]> | ||
setFields( | ||
this: Lens<Source, Target & NotAnArray>, | ||
this: Lens<Source, PlainObject<Target>>, | ||
newValues: FieldValues<Target>, | ||
@@ -151,7 +149,7 @@ ): Updater<Source> | ||
setFields( | ||
this: Lens<Source, Target & NotAnArray>, | ||
this: Lens<Source, PlainObject<Target>>, | ||
): (newValues: FieldValues<Target>) => Updater<Source> | ||
updateFields( | ||
this: Lens<Source, Target & NotAnArray>, | ||
this: Lens<Source, PlainObject<Target>>, | ||
updaters: FieldUpdaters<Target>, | ||
@@ -161,3 +159,3 @@ ): Updater<Source> | ||
updatePartial( | ||
this: Lens<Source, Target & NotAnArray>, | ||
this: Lens<Source, PlainObject<Target>>, | ||
fieldsUpdater: FieldsUpdater<Target>, | ||
@@ -164,0 +162,0 @@ ): Updater<Source> |
@@ -27,4 +27,2 @@ import { createLens } from '../src/createLens' | ||
const source: Source = {} as any | ||
const lens = createLens<Source>() | ||
@@ -34,4 +32,2 @@ const counterLens = lens.focusPath('counter') | ||
const todoListLens = todoLens.focusPath('list') | ||
const todoListItemLens = todoListLens.focusIndex(0) | ||
const userLens = lens.focusPath('user') | ||
@@ -49,9 +45,5 @@ // Updating field values with primitive-focused lens @shouldNotCompile | ||
////////////////////////////////// | ||
// Should not but does compile // | ||
//////////////////////////////// | ||
// Updating field values with unknown prop @shouldNotButDoesCompile | ||
// Updating field values with unknown prop @shouldNotCompile | ||
lens.updatePartial(state => ({ | ||
unknown: 'unknown', | ||
})) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
117788
2386