Commit 286f8332 authored by Lyza Danger Gardner's avatar Lyza Danger Gardner Committed by Lyza Gardner

DRY out selectors with `truthyKeys`

Use utility function to derive "truthy" keys in internal maps
parent cd4b7a86
...@@ -45,6 +45,17 @@ TAB_SORTKEYS_AVAILABLE[uiConstants.TAB_ORPHANS] = [ ...@@ -45,6 +45,17 @@ TAB_SORTKEYS_AVAILABLE[uiConstants.TAB_ORPHANS] = [
'Location', 'Location',
]; ];
/**
* Utility function that returns all of the properties of an object whose
* value is `true`.
*
* @param {Object} obj
* @return {any[]}
*/
function truthyKeys(obj) {
return Object.keys(obj).filter(key => obj[key] === true);
}
function initialSelection(settings) { function initialSelection(settings) {
const selection = {}; const selection = {};
// TODO: Do not take into account existence of `settings.query` here // TODO: Do not take into account existence of `settings.query` here
...@@ -402,9 +413,7 @@ function setSortKey(key) { ...@@ -402,9 +413,7 @@ function setSortKey(key) {
/* Selectors */ /* Selectors */
function focusedAnnotations(state) { function focusedAnnotations(state) {
return Object.keys(state.selection.focused).filter( return truthyKeys(state.selection.focused);
id => state.selection.focused[id] === true
);
} }
/** Is the annotation referenced by `$tag` currently focused? */ /** Is the annotation referenced by `$tag` currently focused? */
...@@ -417,8 +426,7 @@ function isAnnotationFocused(state, $tag) { ...@@ -417,8 +426,7 @@ function isAnnotationFocused(state, $tag) {
*/ */
const hasSelectedAnnotations = createSelector( const hasSelectedAnnotations = createSelector(
state => state.selection.selected, state => state.selection.selected,
selection => selection => truthyKeys(selection).length > 0
Object.keys(selection).filter(id => selection[id] === true).length > 0
); );
/** De-select all annotations. */ /** De-select all annotations. */
...@@ -441,9 +449,7 @@ function clearSelection() { ...@@ -441,9 +449,7 @@ function clearSelection() {
const getFirstSelectedAnnotationId = createSelector( const getFirstSelectedAnnotationId = createSelector(
state => state.selection.selected, state => state.selection.selected,
selection => { selection => {
const selectedIds = Object.keys(selection).filter( const selectedIds = truthyKeys(selection);
id => selection[id] === true
);
return selectedIds.length ? selectedIds[0] : null; return selectedIds.length ? selectedIds[0] : null;
} }
); );
...@@ -543,7 +549,7 @@ const hasAppliedFilter = createSelector( ...@@ -543,7 +549,7 @@ const hasAppliedFilter = createSelector(
const selectedAnnotations = createSelector( const selectedAnnotations = createSelector(
state => state.selection.selected, state => state.selection.selected,
selection => Object.keys(selection).filter(id => selection[id] === true) selection => truthyKeys(selection)
); );
export default { export default {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment