Skip to content

Commit

Permalink
Apply suggestions, improve UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
dmbrooke committed Aug 22, 2023
1 parent 56c894f commit 8e45029
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,10 @@ describe('search parameter manager', () => {
});

it('is possible to access every relevant search parameter using #state.parameters given a certain initial state', () => {
const facetValues = [buildMockFacetValueRequest({state: 'selected'})];
const facetValues = [
buildMockFacetValueRequest({state: 'selected'}),
buildMockFacetValueRequest({state: 'excluded'}),
];
engine.state.facetSet = {
author: buildMockFacetSlice({
request: buildMockFacetRequest({currentValues: facetValues}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ export function getCoreActiveSearchParameters(
...getQ(state),
...getTab(state),
...getSortCriteria(state),
...getSelectedFacets(state),
...getExcludedFacets(state),
...getFacets(state, getSelectedValues, 'f'),
...getFacets(state, getExcludedValues, 'fExcluded'),
...getCategoryFacets(state),
...getNumericFacets(state),
...getDateFacets(state),
Expand Down Expand Up @@ -186,36 +186,24 @@ function getSortCriteria(state: Partial<SearchParametersState>) {
return shouldInclude ? {sortCriteria} : {};
}

function getSelectedFacets(state: Partial<SearchParametersState>) {
if (state.facetSet === undefined) {
return {};
}

const f = Object.entries(state.facetSet)
.filter(([facetId]) => state.facetOptions?.facets[facetId]?.enabled ?? true)
.map(([facetId, {request}]) => {
const selectedValues = getSelectedValues(request.currentValues);
return selectedValues.length ? {[facetId]: selectedValues} : {};
})
.reduce((acc, obj) => ({...acc, ...obj}), {});

return Object.keys(f).length ? {f} : {};
}

function getExcludedFacets(state: Partial<SearchParametersState>) {
function getFacets(
state: Partial<SearchParametersState>,
valuesSelector: (currentValues: FacetValueRequest[]) => string[],
out: keyof SearchParameters
) {
if (state.facetSet === undefined) {
return {};
}

const fExcluded = Object.entries(state.facetSet)
const facets = Object.entries(state.facetSet)
.filter(([facetId]) => state.facetOptions?.facets[facetId]?.enabled ?? true)
.map(([facetId, {request}]) => {
const excludedValues = getExcludedValues(request.currentValues);
return excludedValues.length ? {[facetId]: excludedValues} : {};
const facetValues = valuesSelector(request.currentValues);
return facetValues.length ? {[facetId]: facetValues} : {};
})
.reduce((acc, obj) => ({...acc, ...obj}), {});

return Object.keys(fExcluded).length ? {fExcluded} : {};
return Object.keys(facets).length ? {[out]: facets} : {};
}

function getSelectedValues(values: FacetValueRequest[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ describe('buildSearchParameterSerializer', () => {

it('can serialize and deserialize all search parameters', () => {
const f = {author: ['a', 'b']};
const fExcluded = {source: ['a', 'b']};
const cf = {geography: ['a', 'b']};
const nf = {
size: [buildNumericRange({start: 0, end: 10, state: 'selected'})],
Expand All @@ -493,7 +494,15 @@ describe('buildSearchParameterSerializer', () => {
};
const sf = {fileType: ['a', 'b']};
const af = {documenttype: ['s', 'sd']};
const parameters = buildMockSearchParameters({f, cf, nf, df, sf, af});
const parameters = buildMockSearchParameters({
f,
cf,
nf,
df,
sf,
af,
fExcluded,
});

const {serialize, deserialize} = buildSearchParameterSerializer();
const serialized = serialize(parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,16 @@ function serializeRangeFacets(

function deserialize(fragment: string): SearchParameters {
const parts = fragment.split(delimiter);
const keyValuePairs = parts
const test = parts
.map((part) => splitOnFirstEqual(part))
.map(preprocessObjectPairs)
.filter(isValidPair)
.map(cast);
.map(preprocessObjectPairs);

const keyValuePairs = test.filter(isValidPair).map(cast);

return keyValuePairs.reduce((acc: SearchParameters, pair) => {
const [key, val] = pair;

if (keyHasObjectValue(key)) {
if (keyHasObjectValue(key) || isSpecificFacetKey(key)) {
const mergedValues = {...acc[key], ...(val as object)};
return {...acc, [key]: mergedValues};
}
Expand All @@ -154,7 +154,7 @@ function splitOnFirstEqual(str: string) {

function preprocessObjectPairs(pair: string[]) {
const [key, val] = pair;
const objectKey = /^(f|cf|nf|df|sf|af)-(.+)$/;
const objectKey = /^(f|fExcluded|cf|nf|df|sf|af)-(.+)$/;
const result = objectKey.exec(key);

if (!result) {
Expand Down Expand Up @@ -289,7 +289,7 @@ function cast<K extends SearchParameterKey>(pair: [K, string]): [K, unknown] {
return [key, parseInt(value)];
}

if (keyHasObjectValue(key)) {
if (keyHasObjectValue(key) || isSpecificFacetKey(key)) {
return [key, castUnknownObject(value)];
}

Expand All @@ -307,10 +307,15 @@ function castUnknownObject(value: string) {
return ret;
}

function keyHasObjectValue(
function keyHasObjectValue(key: SearchParameterKey): key is 'af' | 'sf' {
const keys = ['af', 'sf'];
return keys.includes(key);
}

function isSpecificFacetKey(
key: SearchParameterKey
): key is 'f' | 'fExcluded' | 'cf' | 'nf' | 'df' | 'sf' | 'af' {
const keys = ['f', 'fExcluded', 'cf', 'nf', 'df', 'sf', 'af'];
): key is 'f' | 'cf' | 'df' | 'nf' | 'fExcluded' {
const keys = ['f', 'cf', 'df', 'nf', 'fExcluded'];
return keys.includes(key);
}

Expand Down

0 comments on commit 8e45029

Please sign in to comment.