Skip to content

Commit

Permalink
Rename EntityAdapter option sort to sortComparer
Browse files Browse the repository at this point in the history
  • Loading branch information
karptonite committed Sep 16, 2017
1 parent 9e1375d commit cc04880
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions docs/entity/adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ returned adapter provides many [methods](#adapter-methods) for performing operat
against the collection type. The method takes an object for configuration with 2 properties.

- `selectId`: A `method` for selecting the primary id for the collection
- `sort`: A compare function used to [sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) the collection. The comparer function is only needed if the collection needs to be sorted before being displayed. Set to `false` to use leave the collection unsorted, which is more performant during CRUD operations.
- `sortComparer`: A compare function used to [sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) the collection. The comparer function is only needed if the collection needs to be sorted before being displayed. Set to `false` to use leave the collection unsorted, which is more performant during CRUD operations.

Usage:

Expand All @@ -31,7 +31,7 @@ export function sortByName(a: User, b: User): number {

export const adapter: EntityAdapter<User> = createEntityAdapter<User>({
selectId: (user: User) => user.id,
sort: sortByName,
sortComparer: sortByName,
});
```

Expand Down
6 changes: 3 additions & 3 deletions example-app/app/books/reducers/books.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ export interface State extends EntityState<Book> {
* functions for single or multiple operations
* against the dictionary of records. The configuration
* object takes a record id selector function and
* a sort option whether to sort the records when performing
* operations
* a sortComparer option which is set to a compare
* function if the records are to be sorted.
*/
export const adapter: EntityAdapter<Book> = createEntityAdapter<Book>({
selectId: (book: Book) => book.id,
sort: false,
sortComparer: false,
});

/** getInitialState returns the default initial state
Expand Down
2 changes: 1 addition & 1 deletion modules/entity/spec/sorted_state_adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Sorted State Adapter', () => {
beforeEach(() => {
adapter = createEntityAdapter({
selectId: (book: BookModel) => book.id,
sort: (a, b) => a.title.localeCompare(b.title),
sortComparer: (a, b) => a.title.localeCompare(b.title),
});

state = { ids: [], entities: {} };
Expand Down
11 changes: 7 additions & 4 deletions modules/entity/src/create_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ import { createUnsortedStateAdapter } from './unsorted_state_adapter';

export function createEntityAdapter<T>(options: {
selectId: IdSelector<T>;
sort?: false | Comparer<T>;
sortComparer?: false | Comparer<T>;
}): EntityAdapter<T> {
const { selectId, sort }: EntityDefinition<T> = { sort: false, ...options };
const { selectId, sortComparer }: EntityDefinition<T> = {
sortComparer: false,
...options,
};

const stateFactory = createInitialStateFactory<T>();
const selectorsFactory = createSelectorsFactory<T>();
const stateAdapter = sort
? createSortedStateAdapter(selectId, sort)
const stateAdapter = sortComparer
? createSortedStateAdapter(selectId, sortComparer)
: createUnsortedStateAdapter(selectId);

return {
Expand Down
2 changes: 1 addition & 1 deletion modules/entity/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface EntityState<T> {

export interface EntityDefinition<T> {
selectId: IdSelector<T>;
sort: false | Comparer<T>;
sortComparer: false | Comparer<T>;
}

export interface EntityStateAdapter<T> {
Expand Down

0 comments on commit cc04880

Please sign in to comment.