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

81 lines (80 loc) 2.67 kB
import { ComparableData } from '../interfaces/comparable-data-type'; /** * @description * Removes one or multiple items from an array T[]. * For comparison you can provide a key, an array of keys or a custom comparison function that should return true if items match. * If no comparison data is provided, an equality check is used by default. * Returns a shallow copy of the updated array T[], and does not mutate the original one. * * @example * // Removing value without comparison data * * const items = [1,2,3,4,5]; * * const updatedItems = remove(items, [1,2,3]); * * // updatedItems will be: [4,5]; * * @example * // Removing values with comparison function * * const creatures = [{id: 1, type: 'cat'}, {id: 2, type: 'unicorn'}, {id: 3, type: 'kobold'}]; * * const nonExistingCreatures = [{id: 2, type: 'unicorn'}, {id: 3, type: 'kobold'}]; * * const realCreatures = remove(creatures, nonExistingCreatures, (a, b) => a.id === b.id); * * // realCreatures will be: [{id: 1, type: 'cat'}]; * * @example * // Removing values with key * * const creatures = [{id: 1, type: 'cat'}, {id: 2, type: 'unicorn'}, {id: 3, type: 'kobold'}]; * * const nonExistingCreatures = [{id: 2, type: 'unicorn'}, {id: 3, type: 'kobold'}]; * * const realCreatures = remove(creatures, nonExistingCreatures, 'id'); * * // realCreatures will be: [{id: 1, type: 'cat'}]; * * @example * // Removing values with array of keys * * const creatures = [{id: 1, type: 'cat'}, {id: 2, type: 'unicorn'}, {id: 3, type: 'kobold'}]; * * const nonExistingCreatures = [{id: 2, type: 'unicorn'}, {id: 3, type: 'kobold'}]; * * const realCreatures = remove(creatures, nonExistingCreatures, ['id', 'type']); * * // realCreatures will be: [{id: 1, type: 'cat'}]; * * @example * // Usage with RxState * * export class ListComponent { * * readonly removeCreature$ = new Subject<Creature>(); * * constructor(private state: RxState<ComponentState>) { * // Reactive implementation * state.connect( * 'creatures', * this.removeCreature$, * ({ creatures }, creatureToRemove) => { * return remove(creatures, creatureToRemove, (a, b) => a.id === b.id); * } * ); * } * * // Imperative implementation * removeCreature(creatureToRemove: Creature): void { * this.state.set({ creatures: remove(this.state.get().creatures, creatureToRemove, (a, b) => a.id === b.id)}); * } * } * * @returns T[] * * @docsPage remove * @docsCategory transformation-helpers */ export declare function remove<T>(source: T[], scrap: Partial<T>[] | Partial<T>, compare?: ComparableData<T>): T[];