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

59 lines (58 loc) 1.76 kB
/** * @description * Inserts one or multiple items to an array T[]. * Returns a shallow copy of the updated array T[], and does not mutate the original one. * * @example * // Inserting single value * * const creatures = [{id: 1, type: 'cat'}, {id: 2, type: 'dog'}]; * * const updatedCreatures = insert(creatures, {id: 3, type: 'parrot'}); * * // updatedCreatures will be: * // [{id: 1, type: 'cat'}, {id: 2, type: 'dog}, {id: 3, type: 'parrot}]; * * @example * // Inserting multiple values * * const creatures = [{id: 1, type: 'cat'}, {id: 2, type: 'dog'}]; * * const updatedCreatures = insert(creatures, [{id: 3, type: 'parrot'}, {id: 4, type: 'hamster'}]); * * // updatedCreatures will be: * // [{id: 1, type: 'cat'}, {id: 2, type: 'dog'}, {id: 3, type: 'parrot'}, {id: 4, type: 'hamster'}]; * * @example * // Usage with RxState * * export class ListComponent { * * readonly insertCreature$ = new Subject<void>(); * * constructor(private state: RxState<ComponentState>) { * // Reactive implementation * state.connect( * 'creatures', * this.insertCreature$, * ({ creatures }) => { * const creatureToAdd = {id: generateId(), name: 'newCreature', type: 'dinosaur' }; * return insert(creatures, creatureToAdd); * } * ); * } * * // Imperative implementation * insertCeature(): void { * const creatureToAdd = {id: generateId(), name: 'newCreature', type: 'dinosaur' }; * this.state.set({ creatures: insert(this.state.get().creatures, creatureToAdd)}); * } * } * * * @returns T[] * * @docsPage insert * @docsCategory transformation-helpers */ export declare function insert<T>(source: T[], updates: T | T[]): T[];