Skip to content

Commit

Permalink
feat: setValue: move state parameter to first position for uncurr…
Browse files Browse the repository at this point in the history
…ied overload
  • Loading branch information
MrWolfZ committed Apr 15, 2018
1 parent e7e4896 commit 1a69795
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This release requires TypeScript >=2.8.0 for the conditional type support.
* `removeGroupControl`: move `state` parameter to first position for uncurried overload ([a9035ce](https://github.com/MrWolfZ/ngrx-forms/commit/a9035ce))
* `setErrors`: rework to support different parameter combinations for errors (i.e. single error object, array of error objects, and rest parameters) and move `state` parameter to first position for uncurried overload ([15ea555](https://github.com/MrWolfZ/ngrx-forms/commit/15ea555))
* `setUserDefinedProperty`: move `state` parameter to first position for uncurried overload ([520c384](https://github.com/MrWolfZ/ngrx-forms/commit/520c384))
* `setValue`: move `state` parameter to first position for uncurried overload

#### Features

Expand Down
8 changes: 4 additions & 4 deletions src/update-function/set-value.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ describe(setValue.name, () => {
});

it('should call reducer for controls uncurried', () => {
const resultState = setValue('A', INITIAL_STATE.controls.inner);
const resultState = setValue(INITIAL_STATE.controls.inner, 'A');
expect(resultState).not.toBe(INITIAL_STATE.controls.inner);
});

it('should call reducer for groups uncurried', () => {
const resultState = setValue({ inner: 'A', inner5: INITIAL_STATE.value.inner5 }, INITIAL_STATE);
const resultState = setValue(INITIAL_STATE, { inner: 'A', inner5: INITIAL_STATE.value.inner5 });
expect(resultState).not.toBe(INITIAL_STATE);
});

it('should call reducer for arrays uncurried', () => {
const resultState = setValue(['A'], INITIAL_STATE.controls.inner5);
const resultState = setValue(INITIAL_STATE.controls.inner5, ['A']);
expect(resultState).not.toBe(INITIAL_STATE.controls.inner5);
});

Expand All @@ -47,7 +47,7 @@ describe(setValue.name, () => {

it('should work inside an updateGroup uncurried', () => {
const resultState = updateGroup<typeof INITIAL_STATE.value>(INITIAL_STATE, {
inner: inner => setValue<string>('A', inner),
inner: inner => setValue<string>(inner, 'A'),
});

expect(resultState).not.toEqual(INITIAL_STATE);
Expand Down
38 changes: 8 additions & 30 deletions src/update-function/set-value.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SetValueAction } from '../actions';
import { AbstractControlState, FormArrayState, FormControlState, FormControlValueTypes, FormGroupState, InferredControlState } from '../state';
import { AbstractControlState, InferredControlState, isFormState } from '../state';
import { abstractControlReducer, ensureState } from './util';

/**
Expand All @@ -11,39 +11,17 @@ import { abstractControlReducer, ensureState } from './util';
export function setValue<TValue>(value: TValue): (state: AbstractControlState<TValue>) => InferredControlState<TValue>;

/**
* This update function takes a value and a form control state and sets the
* value of the state.
*/
export function setValue<TValue extends FormControlValueTypes>(value: TValue, state: FormControlState<TValue>): FormControlState<TValue>;

/**
* This update function takes a value and a form array state and sets the
* value of the state. This will also update the values of all children
* including adding and removing children on the fly for added/removed
* items.
*/
export function setValue<TValue>(value: TValue, state: FormArrayState<TValue>): FormArrayState<TValue>;

/**
* This update function takes a value and a form group state and sets the
* value of the state. This will also update the values of all children
* including adding and removing children on the fly for added/removed
* properties.
*/
export function setValue<TValue>(value: TValue, state: FormGroupState<TValue>): FormGroupState<TValue>;

/**
* This update function takes a value and a form state and sets the value
* of the state. Setting the value of a group or array will also update the
* This update function takes a form state and a value and sets the value of
* the state. Setting the value of a group or array will also update the
* values of all children including adding and removing children on the fly
* for added/removed properties/items.
*/
export function setValue<TValue>(value: TValue, state: AbstractControlState<TValue>): AbstractControlState<TValue>;
export function setValue<TValue>(state: AbstractControlState<TValue>, value: TValue): InferredControlState<TValue>;

export function setValue<TValue>(value: TValue, state?: AbstractControlState<TValue>) {
if (!!state) {
return abstractControlReducer(state, new SetValueAction(state.id, value));
export function setValue<TValue>(valueOrState: TValue | AbstractControlState<TValue>, value?: TValue) {
if (isFormState(valueOrState)) {
return abstractControlReducer(valueOrState, new SetValueAction(valueOrState.id, value));
}

return (s: AbstractControlState<TValue>) => setValue(value, ensureState(s));
return (s: AbstractControlState<TValue>) => setValue(ensureState(s), valueOrState);
}

0 comments on commit 1a69795

Please sign in to comment.