UNPKG

@rx-angular/cdk

Version:

@rx-angular/cdk is a Component Development Kit for ergonomic and highly performant angular applications. It helps to to build Large scale applications, UI libs, state management, rendering systems and much more. Furthermore the unique way of mixing reacti

75 lines (74 loc) 2.4 kB
import { ComparableData } from '../interfaces/comparable-data-type'; /** * @description * Updates one or multiple items in an array T[]. * For comparison you can provide key, array of keys or a custom comparison function that should return true if items match. * If no comparison is provided, an equality check is used by default. * Returns a shallow copy of the array T[] and updated items, does not mutate the original array. * * @example * // Update with comparison function * * const creatures = [{id: 1, type: 'cat'}, {id: 2, type: 'dog'}]; * * const newCat = {id: 1, type: 'lion'}; * * const updatedCreatures = update(creatures, newCat, (a, b) => a.id === b.id); * * // updatedCreatures will be: * // [{id: 1, type: 'lion'}, {id: 2, type: 'dog'}]; * * @example * // Update with key * * const creatures = [{id: 1, type: 'cat'}, {id: 2, type: 'dog'}]; * * const newCat = {id: 1, type: 'lion'}; * * const updatedCreatures = update(creatures, newCat, 'id'); * * // updatedCreatures will be: * // [{id: 1, type: 'lion'}, {id: 2, type: 'dog'}]; * * @example * // Update with array of keys * * const creatures = [{id: 1, type: 'cat', name: 'Bella'}, {id: 2, type: 'dog', name: 'Sparky'}]; * * const newCat = {id: 1, type: 'lion', name: 'Bella'}; * * const updatedCreatures = update(creatures, newCat, ['id', 'name']); * * // updatedCreatures will be: * // [{id: 1, type: 'lion', name: 'Bella'}, {id: 2, type: 'dog', name: 'Sparky'}]; * * @example * // Usage with RxState * * export class ListComponent { * * readonly updateCreature$ = new Subject<Creature>(); * * constructor(private state: RxState<ComponentState>) { * // Reactive implementation * state.connect( * 'creatures', * this.updateCreature$, * ({ creatures }, creatureToUpdate) => { * return update(creatures, creatureToUpdate, (a, b) => a.id === b.id); * } * ); * } * * // Imperative implementation * updateCreature(creatureToUpdate: Creature): void { * this.state.set({ creatures: update(this.state.get().creatures, creatureToUpdate, (a, b) => a.id === b.id)}); * } * } * * @returns T[] * * @docsPage update * @docsCategory transformation-helpers */ export declare function update<T extends object>(source: T[], updates: Partial<T>[] | Partial<T>, compare?: ComparableData<T>): T[];