Skip to content

Commit

Permalink
feat(entity): add support for predicate to removeMany (#900)
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver authored and brandonroberts committed Oct 26, 2018
1 parent ab56aac commit d7daa2f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
18 changes: 17 additions & 1 deletion modules/entity/spec/sorted_state_adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('Sorted State Adapter', () => {
});
});

it('should let you remove many entities from the state', () => {
it('should let you remove many entities by id from the state', () => {
const withAll = adapter.addAll(
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
state
Expand All @@ -118,6 +118,22 @@ describe('Sorted State Adapter', () => {
});
});

it('should let you remove many entities by a predicate from the state', () => {
const withAll = adapter.addAll(
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
state
);

const withoutMany = adapter.removeMany(p => p.id.startsWith('a'), withAll);

expect(withoutMany).toEqual({
ids: [TheGreatGatsby.id],
entities: {
[TheGreatGatsby.id]: TheGreatGatsby,
},
});
});

it('should let you remove all entities from the state', () => {
const withAll = adapter.addAll(
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
Expand Down
18 changes: 17 additions & 1 deletion modules/entity/spec/unsorted_state_adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('Unsorted State Adapter', () => {
});
});

it('should let you remove many entities from the state', () => {
it('should let you remove many entities by id from the state', () => {
const withAll = adapter.addAll(
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
state
Expand All @@ -117,6 +117,22 @@ describe('Unsorted State Adapter', () => {
});
});

it('should let you remove many entities by a predicate from the state', () => {
const withAll = adapter.addAll(
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
state
);

const withoutMany = adapter.removeMany(p => p.id.startsWith('a'), withAll);

expect(withoutMany).toEqual({
ids: [TheGreatGatsby.id],
entities: {
[TheGreatGatsby.id]: TheGreatGatsby,
},
});
});

it('should let you remove all entities from the state', () => {
const withAll = adapter.addAll(
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
Expand Down
3 changes: 3 additions & 0 deletions modules/entity/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface UpdateNum<T> {

export type Update<T> = UpdateStr<T> | UpdateNum<T>;

export type Predicate<T> = (entity: T) => boolean;

export interface EntityState<T> {
ids: string[] | number[];
entities: Dictionary<T>;
Expand All @@ -48,6 +50,7 @@ export interface EntityStateAdapter<T> {

removeMany<S extends EntityState<T>>(keys: string[], state: S): S;
removeMany<S extends EntityState<T>>(keys: number[], state: S): S;
removeMany<S extends EntityState<T>>(predicate: Predicate<T>, state: S): S;

removeAll<S extends EntityState<T>>(state: S): S;

Expand Down
23 changes: 19 additions & 4 deletions modules/entity/src/unsorted_state_adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { EntityState, EntityStateAdapter, IdSelector, Update } from './models';
import {
EntityState,
EntityStateAdapter,
IdSelector,
Update,
Predicate,
} from './models';
import { createStateOperator, DidMutate } from './state_adapter';
import { selectIdValue } from './utils';

Expand Down Expand Up @@ -49,11 +55,20 @@ export function createUnsortedStateAdapter<T>(selectId: IdSelector<T>): any {
}

function removeManyMutably(keys: T[], state: R): DidMutate;
function removeManyMutably(keys: any[], state: any): DidMutate {
function removeManyMutably(predicate: Predicate<T>, state: R): DidMutate;
function removeManyMutably(
keysOrPredicate: any[] | Predicate<T>,
state: any
): DidMutate {
const keys =
keysOrPredicate instanceof Array
? keysOrPredicate
: state.ids.filter((key: any) => keysOrPredicate(state.entities[key]));

const didMutate =
keys
.filter(key => key in state.entities)
.map(key => delete state.entities[key]).length > 0;
.filter((key: any) => key in state.entities)
.map((key: any) => delete state.entities[key]).length > 0;

if (didMutate) {
state.ids = state.ids.filter((id: any) => id in state.entities);
Expand Down

0 comments on commit d7daa2f

Please sign in to comment.