Skip to content

Commit

Permalink
feat(entities): added a function getEntityByPredicate (#465)
Browse files Browse the repository at this point in the history
* feat: add function getEntityByPredicate

* feat(entities): some fix

* feat(queries): removed console.log from test file

---------

Co-authored-by: Sergey Rogachev <[email protected]>
  • Loading branch information
Sir-J and Sergey Rogachev authored Jul 7, 2023
1 parent c539add commit df17064
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/docs/features/entities-management/entities.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,16 @@ import { getEntity } from '@ngneat/elf-entities';
const todo = todosStore.query(getEntity(id));
```

### `getEntityByPredicate`

Get first entity from the store by predicate:

```ts
import { getEntityByPredicate } from '@ngneat/elf-entities';

const todo = todosStore.query(getEntityByPredicate(({ title }) => title = 'Elf'));
```

### `hasEntity`

Returns whether an entity exists:
Expand Down
1 change: 1 addition & 0 deletions packages/entities/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export { unionEntitiesAsMap as unionEntitiesAsMap } from './lib/union-entities-a
export * from './lib/active/active';
export {
getEntity,
getEntityByPredicate,
hasEntity,
getAllEntities,
getAllEntitiesApply,
Expand Down
40 changes: 40 additions & 0 deletions packages/entities/src/lib/queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
getEntity,
hasEntity,
getEntitiesIds,
getEntityByPredicate,
} from './queries';
import { addEntities } from './add.mutation';
import { UIEntitiesRef } from './entity.state';
Expand Down Expand Up @@ -56,6 +57,25 @@ describe('queries', () => {
expect(store.query(getEntity(1))).toEqual(todo);
});

it('should return an entity by predicate', () => {
const store = createEntitiesStore();

store.update(
addEntities([
createTodo(1),
{
id: 4,
title: `todo 2`,
completed: false,
},
createTodo(3),
])
);
expect(
store.query(getEntityByPredicate((el) => el.title === `todo 2`))?.id
).toEqual(4);
});

it('should work with ref', () => {
const store = createUIEntityStore();
expect(store.query(getEntity(1, { ref: UIEntitiesRef }))).toEqual(
Expand All @@ -66,6 +86,26 @@ describe('queries', () => {
store.update(addEntities(todo, { ref: UIEntitiesRef }));
expect(store.query(getEntity(1, { ref: UIEntitiesRef }))).toEqual(todo);
});

it('should find by predicate with ref', () => {
const store = createUIEntityStore();
expect(
store.query(
getEntityByPredicate((el) => el.id === 1, { ref: UIEntitiesRef })
)
).toEqual(undefined);

store.update(
addEntities([createUITodo(1), createUITodo(2), createUITodo(3)], {
ref: UIEntitiesRef,
})
);
expect(
store.query(
getEntityByPredicate((el) => el.id === 2, { ref: UIEntitiesRef })
)?.id
).toEqual(2);
});
});

describe('hasEntity', () => {
Expand Down
28 changes: 28 additions & 0 deletions packages/entities/src/lib/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
EntitiesState,
getEntityType,
getIdType,
ItemPredicate,
} from './entity.state';
import { Query } from '@ngneat/elf';

Expand Down Expand Up @@ -92,6 +93,33 @@ export function getEntity<
};
}

/**
*
* Get first entity by predicate
*
* @example
*
* store.query(getEntityByPredicate(({ title }) => title === 'Elf'))
*
*/
export function getEntityByPredicate<
S extends EntitiesState<Ref>,
Ref extends EntitiesRef = DefaultEntitiesRef
>(
predicate: ItemPredicate<getEntityType<S, Ref>>,
options: BaseEntityOptions<Ref> = {}
): Query<S, getEntityType<S, Ref> | undefined> {
return function (state) {
const { ref: { entitiesKey, idsKey } = defaultEntitiesRef } = options;
const entities = state[entitiesKey];
const id = state[idsKey].find((id: getIdType<S, Ref>) => {
return predicate(entities[id]);
});

return entities[id];
};
}

/**
*
* Check whether the entity exist
Expand Down

0 comments on commit df17064

Please sign in to comment.