set/update fallbacks, tests, tslint coverage

This commit is contained in:
Stepan Burguchev
2017-07-10 09:25:33 +03:00
parent ccd26750c8
commit bd54249da7
3 changed files with 16 additions and 10 deletions

View File

@@ -36,6 +36,7 @@ declare namespace SeamlessImmutable {
interface IImmutableObject<T> {
set<K extends keyof T>(property: K, value: T[K]): ImmutableObject<T>;
set<TValue>(property: string, value: TValue): ImmutableObject<T>;
setIn<K extends keyof T>
(propertyPath: [ K ], value: T[K]): ImmutableObject<T>;
setIn<K extends keyof T, L extends keyof T[K]>
@@ -54,6 +55,7 @@ declare namespace SeamlessImmutable {
merge(part: DeepPartial<T>, config?: MergeConfig): ImmutableObject<T>;
update<K extends keyof T>(property: K, updaterFunction: (value: T[K], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject<T>;
update<TValue>(property: string, updaterFunction: (value: TValue, ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject<T>;
updateIn<K extends keyof T>
(propertyPath: [ K ], updaterFunction: (value: T[K], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject<T>;
updateIn<K extends keyof T, L extends keyof T[K]>

View File

@@ -5,7 +5,7 @@ import * as Immutable from 'seamless-immutable';
interface User {
firstName: string;
lastName: string;
};
}
interface Address {
line1: string;
@@ -23,7 +23,7 @@ interface ExtendedUser extends User {
interface User {
firstName: string;
lastName: string;
};
}
const arrayOfNumbers: Immutable.ImmutableArray<number> = Immutable.from([0, 2]);
const user: Immutable.ImmutableObject<User> = Immutable.from({
@@ -83,28 +83,31 @@ interface ExtendedUser extends User {
};
// set: property name is strongly checked
const updatedUser: Immutable.ImmutableObject<User> = immutableUser.set('firstName', 'Whirlwind');
const updatedUser01: Immutable.ImmutableObject<User> = immutableUser.set('firstName', 'Whirlwind');
const updatedUser02: Immutable.ImmutableObject<User> = immutableUser.set(data.propertyId, 'Whirlwind');
// setIn: property path is strongly checked for up to 5 arguments (helps with refactoring and intellisense)
// but will fall back to any[] if there are dynamic arguments on the way
const updatedUser2: Immutable.ImmutableObject<ExtendedUser> = immutableUserEx.setIn(['address', 'line1'], 'Small house');
const updatedUser3: Immutable.ImmutableObject<ExtendedUser> = immutableUserEx.setIn([ data.propertyId, 'line1' ], 'Small house');
const updatedUser11: Immutable.ImmutableObject<ExtendedUser> = immutableUserEx.setIn(['address', 'line1'], 'Small house');
const updatedUser12: Immutable.ImmutableObject<ExtendedUser> = immutableUserEx.setIn([ data.propertyId, 'line1' ], 'Small house');
// asMutable
const mutableUser1: User = immutableUser.asMutable();
const mutableUser2: User = immutableUser.asMutable({ deep: true });
const mutableUser21: User = immutableUser.asMutable();
const mutableUser22: User = immutableUser.asMutable({ deep: true });
// merge: merged part is strongly checked as a deeply partial object
const mergedUser: Immutable.ImmutableObject<User> = immutableUserEx.merge({ address: { line1: 'Small house' }, firstName: 'Jack' });
// update: property name is strongly checked
const updatedUser4: Immutable.ImmutableObject<User> = immutableUser.update('firstName', x => x.toLowerCase() + ' Whirlwind');
const updatedUser41: Immutable.ImmutableObject<User> = immutableUser.update('firstName', x => x.toLowerCase() + ' Whirlwind');
// the type of the updated value must be explicity specified in case of fallback
const updatedUser42: Immutable.ImmutableObject<User> = immutableUser.update<string>(data.propertyId, x => x.toLowerCase() + ' Whirlwind');
// updateIn: property path is strongly checked for up to 5 arguments (helps with refactoring and intellisense)
// but will fall back to any[] if there are dynamic arguments on the way
const updatedUser5: Immutable.ImmutableObject<User> = immutableUserEx.updateIn([ 'address', 'line1' ], x => x.toLowerCase() + ' 43');
const updatedUser51: Immutable.ImmutableObject<User> = immutableUserEx.updateIn([ 'address', 'line1' ], x => x.toLowerCase() + ' 43');
// the type of the updated value must be explicity specified in case of fallback
const updatedUser6: Immutable.ImmutableObject<User> = immutableUserEx.updateIn<string>([ data.propertyId, 'line1' ], x => x.toLowerCase() + ' 43');
const updatedUser52: Immutable.ImmutableObject<User> = immutableUserEx.updateIn<string>([ data.propertyId, 'line1' ], x => x.toLowerCase() + ' 43');
// without: the return type must be specified explicitly or it will be `any`
const simpleUser1: Immutable.ImmutableObject<any> = immutableUserEx.without('address');

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }