Skip to content

Commit

Permalink
feat: addGroupControl: move state parameter to first position for…
Browse files Browse the repository at this point in the history
… uncurried overload
  • Loading branch information
MrWolfZ committed Apr 15, 2018
1 parent 17e922e commit b6da5ee
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This release requires TypeScript >=2.8.0 for the conditional type support.
* remove `cast` utility function since it is obsolete due to proper control type inference
* change order of parameters for many update functions to more be consistent
* `addArrayControl`: move `state` parameter to first position for uncurried overload ([ab094b8](https://github.com/MrWolfZ/ngrx-forms/commit/ab094b8))
* `addGroupControl`: move `state` parameter to first position for uncurried overload

#### Features

Expand Down
2 changes: 1 addition & 1 deletion src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export class AddArrayControlAction<TValue> implements Action {
}
}

export class AddGroupControlAction<TValue extends KeyValue, TControlKey extends keyof TValue = string> implements Action {
export class AddGroupControlAction<TValue extends KeyValue, TControlKey extends keyof TValue = keyof TValue> implements Action {
static readonly TYPE = 'ngrx/forms/ADD_GROUP_CONTROL';
readonly type = AddGroupControlAction.TYPE;
readonly controlId: NgrxFormControlId;
Expand Down
2 changes: 1 addition & 1 deletion src/inferred-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function inferredStateReducer<TValue>(
action: Actions<any>,
): InferredControlState<TValue> {
if (isArrayState(state)) {
return formArrayReducerInternal<TValue>(state, action) as any;
return formArrayReducerInternal<TValue>(state, action as Actions<any[]>) as any;
}

if (isGroupState(state)) {
Expand Down
2 changes: 1 addition & 1 deletion src/update-function/add-group-control.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe(addGroupControl.name, () => {
});

it('should call reducer for groups uncurried', () => {
const resultState = addGroupControl('inner2', 'A', INITIAL_STATE);
const resultState = addGroupControl(INITIAL_STATE, 'inner2', 'A');
expect(resultState).not.toBe(INITIAL_STATE);
});

Expand Down
26 changes: 13 additions & 13 deletions src/update-function/add-group-control.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import { AddGroupControlAction } from '../actions';
import { formGroupReducer } from '../group/reducer';
import { FormGroupState, KeyValue } from '../state';
import { FormGroupState, isGroupState, KeyValue } from '../state';
import { ensureState } from './util';

/**
* This update function takes a name and a value and returns a projection function
* that adds a child control with the given name and value to a form group state.
*/
export function addGroupControl<TValue extends KeyValue, TControlKey extends keyof TValue = string>(
name: keyof TValue,
export function addGroupControl<TValue extends KeyValue, TControlKey extends keyof TValue = keyof TValue>(
name: TControlKey,
value: TValue[TControlKey],
): (state: FormGroupState<TValue>) => FormGroupState<TValue>;

/**
* This update function takes a name, a value, and a form group state and adds a child
* control with the given name and value to the form group state.
*/
export function addGroupControl<TValue extends KeyValue, TControlKey extends keyof TValue = string>(
name: keyof TValue,
value: TValue[TControlKey],
export function addGroupControl<TValue extends KeyValue, TControlKey extends keyof TValue = keyof TValue>(
state: FormGroupState<TValue>,
name: TControlKey,
value: TValue[TControlKey],
): FormGroupState<TValue>;

export function addGroupControl<TValue extends KeyValue, TControlKey extends keyof TValue = string>(
name: keyof TValue,
value: TValue[TControlKey],
state?: FormGroupState<TValue>,
export function addGroupControl<TValue extends KeyValue, TControlKey extends keyof TValue = keyof TValue>(
nameOrState: TControlKey | FormGroupState<TValue>,
valueOrName: TValue[TControlKey] | TControlKey,
value?: TValue[TControlKey],
) {
if (!!state) {
return formGroupReducer(state, new AddGroupControlAction<TValue, TControlKey>(state.id, name, value));
if (isGroupState(nameOrState)) {
return formGroupReducer(nameOrState, new AddGroupControlAction<TValue, TControlKey>(nameOrState.id, valueOrName as TControlKey, value!));
}

return (s: FormGroupState<TValue>) => addGroupControl(name, value, ensureState(s));
return (s: FormGroupState<TValue>) => addGroupControl(ensureState(s), nameOrState as TControlKey, valueOrName as TValue[TControlKey]);
}

0 comments on commit b6da5ee

Please sign in to comment.