-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
207 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const selectByIds = <S, K, Props = any>( | ||
selector: (state: S, ids: Props) => K, | ||
ids: Props, | ||
): ((state: S) => K) => state => selector(state, ids); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import {ENTITY_STATE, ENTITY_STATE_CUSTOM, STORE_SELECTOR} from './types'; | ||
|
||
export const stateKeys = < | ||
SELECTOR extends STORE_SELECTOR<any, any>, | ||
STORE extends SELECTOR extends STORE_SELECTOR<infer U, any> ? U : never, | ||
SLICE extends SELECTOR extends STORE_SELECTOR<any, infer U> ? U : never, | ||
E_KEY extends keyof SLICE, | ||
I_KEY extends keyof SLICE, | ||
ENTITY extends SLICE extends ENTITY_STATE_CUSTOM<E_KEY, I_KEY, infer U> ? U : never | ||
>( | ||
selector: SELECTOR, | ||
entitiesKey: E_KEY, | ||
idsKey: I_KEY, | ||
): STORE_SELECTOR<STORE, ENTITY_STATE<ENTITY>> => { | ||
const callback: STORE_SELECTOR<STORE, ENTITY_STATE<ENTITY>> = (s: STORE): ENTITY_STATE<ENTITY> => { | ||
const slice = selector(s); | ||
return { | ||
ids: slice[idsKey], | ||
entities: slice[entitiesKey], | ||
}; | ||
}; | ||
callback.idsKey = idsKey; | ||
callback.entitiesKey = entitiesKey; | ||
callback.originalSelector = selector; | ||
|
||
return callback; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { | ||
ngrxEntityRelationshipReducer, | ||
reduceGraph, | ||
relatedEntitySelector, | ||
rootEntitySelector, | ||
stateKeys, | ||
} from 'ngrx-entity-relationship'; | ||
|
||
describe('custom-keys', () => { | ||
interface Item { | ||
id: string; | ||
name: string; | ||
parentId?: string; | ||
parent?: Item; | ||
} | ||
|
||
const state: { | ||
records: { | ||
allIds: Array<string>; | ||
byId: Record<string, Item | undefined>; | ||
}; | ||
} = { | ||
records: { | ||
allIds: [], | ||
byId: {}, | ||
}, | ||
}; | ||
|
||
const selectItemsState = (s: typeof state) => s.records; | ||
const featureSelector = stateKeys(selectItemsState, 'byId', 'allIds'); | ||
|
||
const sItem = rootEntitySelector(featureSelector); | ||
const sItemParent = relatedEntitySelector(featureSelector, 'parentId', 'parent'); | ||
|
||
const selector = sItem(sItemParent()); | ||
|
||
it('handles custom values correctly', () => { | ||
const action = reduceGraph({ | ||
data: [ | ||
{ | ||
id: 'i1', | ||
name: '1', | ||
parentId: 'i2', | ||
parent: { | ||
id: 'i2', | ||
name: '2', | ||
}, | ||
}, | ||
], | ||
selector, | ||
}); | ||
|
||
const reducer = ngrxEntityRelationshipReducer<typeof state>(s => s); | ||
const testingState = reducer(state, action); | ||
|
||
// data has been reduced. | ||
expect(testingState).toEqual({ | ||
records: { | ||
allIds: ['i1', 'i2'], | ||
byId: { | ||
i1: { | ||
id: 'i1', | ||
name: '1', | ||
parentId: 'i2', | ||
}, | ||
i2: { | ||
id: 'i2', | ||
name: '2', | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// data has been reached. | ||
expect(selector(testingState, 'i1')).toEqual({ | ||
id: 'i1', | ||
name: '1', | ||
parentId: 'i2', | ||
parent: { | ||
id: 'i2', | ||
name: '2', | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {relatedEntity, rootEntities, rootEntity, selectByIds} from 'ngrx-entity-relationship'; | ||
|
||
describe('select-by-ids', () => { | ||
interface Item { | ||
id: string; | ||
name: string; | ||
parentId?: string; | ||
parent?: Item; | ||
} | ||
|
||
const state: { | ||
records: { | ||
ids: Array<string>; | ||
entities: Record<string, Item | undefined>; | ||
}; | ||
} = { | ||
records: { | ||
ids: ['i1', 'i2'], | ||
entities: { | ||
i1: { | ||
id: 'i1', | ||
name: '1', | ||
parentId: 'i2', | ||
}, | ||
i2: { | ||
id: 'i2', | ||
name: '2', | ||
}, | ||
}, | ||
}, | ||
}; | ||
const stateSelector = (s: typeof state) => s.records; | ||
const record = rootEntity(stateSelector, relatedEntity(stateSelector, 'parentId', 'parent')); | ||
const records = rootEntities(record); | ||
|
||
it('selects right entity', () => { | ||
const selector = selectByIds(record, 'i2'); | ||
expect(selector(state)).toEqual({ | ||
id: 'i2', | ||
name: '2', | ||
}); | ||
}); | ||
|
||
it('selects right entities', () => { | ||
const selector = selectByIds(records, ['i1']); | ||
expect(selector(state)).toEqual([ | ||
{ | ||
id: 'i1', | ||
name: '1', | ||
parentId: 'i2', | ||
parent: { | ||
id: 'i2', | ||
name: '2', | ||
}, | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters