Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Entity): add removeMany by predicate #900

Merged
merged 3 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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