State Operators
upsertItem
Usage
ctx.setState(
patch<FoodModel>({
foods: upsertItem<Food>(f => f.id === foodId, food)
})
);State Operator Code
import { Predicate } from '@ngxs/store/operators/internals';
import { StateOperator } from '@ngxs/store';
import { compose, updateItem, iif, insertItem, patch } from '@ngxs/store/operators';
export function upsertItem<T>(
selector: number | Predicate<T>,
upsertValue: T
): StateOperator<T[]> {
return compose<T[]>(
items => <T[]>(items || []),
iif<T[]>(
items => Number(selector) === selector,
iif<T[]>(
items => selector < items.length,
<StateOperator<T[]>>updateItem(selector, patch(upsertValue)),
<StateOperator<T[]>>insertItem(upsertValue, <number>selector)
),
iif<T[]>(
items => items.some(<any>selector),
<StateOperator<T[]>>updateItem(selector, patch(upsertValue)),
<StateOperator<T[]>>insertItem(upsertValue)
)
)
);
}Collaborate with your awesome operator!
Last updated

