Skip to content

Commit

Permalink
Merge branch 'display-filters-for-authority-value-fixes_contribute-7.…
Browse files Browse the repository at this point in the history
…6' into display-filters-for-authority-value-fixes_contribute-main
  • Loading branch information
alexandrevryghem committed Jan 22, 2024
2 parents eee7267 + 392fca7 commit 3cb5d62
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/app/shared/search/models/search-options.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { URLCombiner } from '../../../core/url-combiner/url-combiner';
import { SearchFilter } from './search-filter.model';
import { DSpaceObjectType } from '../../../core/shared/dspace-object-type.model';
import { ViewMode } from '../../../core/shared/view-mode.model';
import { stripDisplayValueFromFilterValue } from '../search.utils';

/**
* This model class represents all parameters needed to request information about a certain search request
Expand Down Expand Up @@ -58,7 +59,7 @@ export class SearchOptions {
this.filters.forEach((filter: SearchFilter) => {
filter.values.forEach((value) => {
const filterValue = value.includes(',') ? `${value}` : value + (filter.operator ? ',' + filter.operator : '');
args.push(`${filter.key}=${this.encodeFilterQueryValue(filterValue)}`);
args.push(`${filter.key}=${this.encodeFilterQueryValue(stripDisplayValueFromFilterValue(filterValue))}`);
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { InputSuggestion } from '../../../../input-suggestions/input-suggestions
import { SearchOptions } from '../../../models/search-options.model';
import { SEARCH_CONFIG_SERVICE } from '../../../../../my-dspace-page/my-dspace-page.component';
import { currentPath } from '../../../../utils/route.utils';
import { getFacetValueForType, stripOperatorFromFilterValue } from '../../../search.utils';
import { getFacetValueForType, getDisplayValueFromFilterValue } from '../../../search.utils';
import { createPendingRemoteDataObject } from '../../../../remote-data.utils';
import { FacetValues } from '../../../models/facet-values.model';

Expand Down Expand Up @@ -236,7 +236,7 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
return {
displayValue: this.getDisplayValue(facet, data),
query: this.getFacetValue(facet),
value: stripOperatorFromFilterValue(this.getFacetValue(facet))
value: getDisplayValueFromFilterValue(this.getFacetValue(facet)),
};
});
}
Expand Down Expand Up @@ -328,7 +328,7 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
if (hasValue(fValue)) {
return fValue;
}
const filterValue = stripOperatorFromFilterValue(value);
const filterValue = getDisplayValueFromFilterValue(value);
return Object.assign(new FacetValue(), { label: filterValue, value: filterValue });
});
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { SearchService } from '../../../../core/shared/search/search.service';
import { currentPath } from '../../../utils/route.utils';
import { PaginationService } from '../../../../core/pagination/pagination.service';
import { SearchConfigurationService } from '../../../../core/shared/search/search-configuration.service';
import { stripOperatorFromFilterValue } from '../../search.utils';
import { getDisplayValueFromFilterValue } from '../../search.utils';

@Component({
selector: 'ds-search-label',
Expand Down Expand Up @@ -85,7 +85,7 @@ export class SearchLabelComponent implements OnInit {
// const pattern = /,[^,]*$/g;
const pattern = /,authority*$/g;
value = value.replace(pattern, '');
return stripOperatorFromFilterValue(value);
return getDisplayValueFromFilterValue(value);
}

private getFilterName(): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class SearchLabelsComponent {
const labels = {};
Object.keys(params)
.forEach((key) => {
labels[key] = [...params[key].map((value) => value)];
labels[key] = [...params[key].map((value: string) => value)];
});
return labels;
})
Expand Down
46 changes: 36 additions & 10 deletions src/app/shared/search/search.utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { FacetValue } from './models/facet-value.model';
import { SearchFilterConfig } from './models/search-filter-config.model';
import {
addDisplayValueToFilterValue,
addOperatorToFilterValue,
escapeRegExp,
getDisplayValueFromFilterValue,
getFacetValueForType,
stripOperatorFromFilterValue
SEARCH_AUTHORITY_VALUE_SEPARATOR,
stripDisplayValueFromFilterValue,
} from './search.utils';

describe('Search Utils', () => {
Expand All @@ -24,10 +27,10 @@ describe('Search Utils', () => {
}
});
facetValueWithSearchHrefAuthority = Object.assign(new FacetValue(), {
label: 'Value with search href',
value: 'Value with search href',
authorityKey: 'uuid',
}
);
});
facetValueWithoutSearchHref = Object.assign(new FacetValue(), {
value: 'Value without search href'
});
Expand All @@ -40,27 +43,40 @@ describe('Search Utils', () => {
expect(getFacetValueForType(facetValueWithSearchHref, searchFilterConfig)).toEqual('Value with search href,equals');
});

it('should retrieve the correct value from the Facet', () => {
expect(getFacetValueForType(facetValueWithSearchHrefAuthority, searchFilterConfig)).toEqual('uuid,authority');
it('should retrieve the value from the Facet when it is defined', () => {
expect(getFacetValueForType(facetValueWithSearchHrefAuthority, searchFilterConfig)).toEqual('Value with search href||uuid,authority');
});

it('should return the facet value with an equals operator by default', () => {
expect(getFacetValueForType(facetValueWithoutSearchHref, searchFilterConfig)).toEqual('Value without search href,equals');
});
});

describe('stripOperatorFromFilterValue', () => {
describe('getDisplayValueFromFilterValue', () => {
it('should strip equals operator from the value', () => {
expect(stripOperatorFromFilterValue('value,equals')).toEqual('value');
expect(getDisplayValueFromFilterValue('value,equals')).toEqual('value');
});

it('should strip query operator from the value', () => {
expect(stripOperatorFromFilterValue('value,query')).toEqual('value');
expect(getDisplayValueFromFilterValue('value,query')).toEqual('value');
});

it('should strip authority operator from the value', () => {
expect(stripOperatorFromFilterValue('value,authority')).toEqual('value');
expect(getDisplayValueFromFilterValue('value,authority')).toEqual('value');
});

it('should not strip a the part after the last , from a value if it isn\'t a valid operator', () => {
expect(stripOperatorFromFilterValue('value,invalid_operator')).toEqual('value,invalid_operator');
expect(getDisplayValueFromFilterValue('value,invalid_operator')).toEqual('value,invalid_operator');
});

it('should strip the display value from the value', () => {
expect(getDisplayValueFromFilterValue(`display${SEARCH_AUTHORITY_VALUE_SEPARATOR}raw`)).toBe('display');
});
});

describe('stripDisplayValueFromFilterValue', () => {
it('should remove the display value from the value', () => {
expect(stripDisplayValueFromFilterValue(`display${SEARCH_AUTHORITY_VALUE_SEPARATOR}raw,authority`)).toBe('raw,authority');
});
});

Expand All @@ -74,6 +90,16 @@ describe('Search Utils', () => {
});
});

describe('addDisplayValueToFilterValue', () => {
it('should add the display value to the value', () => {
expect(addDisplayValueToFilterValue('977e3a5b83a89f0ea6ae8671ae097f6b28bbf403', 'display value')).toEqual(`display value${SEARCH_AUTHORITY_VALUE_SEPARATOR}977e3a5b83a89f0ea6ae8671ae097f6b28bbf403`);
});

it('should not add the display value to the value if empty', () => {
expect(addDisplayValueToFilterValue('977e3a5b83a89f0ea6ae8671ae097f6b28bbf403', '')).toEqual('977e3a5b83a89f0ea6ae8671ae097f6b28bbf403');
});
});

describe(`escapeRegExp`, () => {
it(`should escape all occurrences of '.' in the input string`, () => {
const input = `a.string.with.a.number.of.'.'s.in.it`;
Expand Down
55 changes: 45 additions & 10 deletions src/app/shared/search/search.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { FacetValue } from './models/facet-value.model';
import { SearchFilterConfig } from './models/search-filter-config.model';
import { isNotEmpty } from '../empty.util';

export const SEARCH_AUTHORITY_VALUE_SEPARATOR = '||';

/**
* Get a facet's value by matching its parameter in the search href, this will include the operator of the facet value
* If the {@link FacetValue} doesn't contain a search link, its raw value will be returned as a fallback
Expand All @@ -16,11 +18,14 @@ export function getFacetValueForType(facetValue: FacetValue, searchFilterConfig:
return decodeURIComponent(values[1]);
}
}
let operator = 'equals';
let value = facetValue.value;
if (facetValue.authorityKey) {
return addOperatorToFilterValue(facetValue.authorityKey, 'authority');
operator = 'authority';
value = addDisplayValueToFilterValue(facetValue.authorityKey, facetValue.label);
}

return addOperatorToFilterValue(facetValue.value, 'equals');
return addOperatorToFilterValue(value, operator);
}

/**
Expand All @@ -33,24 +38,54 @@ export function escapeRegExp(input: string): string {
}

/**
* Strip the operator (equals, query or authority) from a filter value.
* @param value The value from which the operator should be stripped.
* Strip the operator (equals, query or authority) from a filter value, and it strips the raw value from a filter value
*
* @param value The filter value to parse
*/
export function stripOperatorFromFilterValue(value: string) {
export function getDisplayValueFromFilterValue(value: string): string {
if (value.match(new RegExp(`.+,(equals|query|authority)$`))) {
return value.substring(0, value.lastIndexOf(','));
value = value.substring(0, value.lastIndexOf(','));
}
if (value.indexOf(SEARCH_AUTHORITY_VALUE_SEPARATOR) > -1) {
value = value.substring(0, value.indexOf(SEARCH_AUTHORITY_VALUE_SEPARATOR));
}
return value;
}

/**
* Strips the display value from a filter value
*
* @param value The filter value to parse
*/
export function stripDisplayValueFromFilterValue(value: string): string {
if (value.indexOf(SEARCH_AUTHORITY_VALUE_SEPARATOR) > -1) {
value = value.substring(value.indexOf(SEARCH_AUTHORITY_VALUE_SEPARATOR) + SEARCH_AUTHORITY_VALUE_SEPARATOR.length);
}
return value;
}

/**
* Add an operator to a string
* @param value
* @param operator
* Add the operator to the value (when not already present)
*
* @param value The value to update
* @param operator The operator to add
*/
export function addOperatorToFilterValue(value: string, operator: string) {
export function addOperatorToFilterValue(value: string, operator: string): string {
if (!value.match(new RegExp(`^.+,(equals|query|authority)$`))) {
return `${value},${operator}`;
}
return value;
}

/**
* Adds the display value when it differs from the actual value
*
* @param value The value to update
* @param displayValue The fallback display value to add
*/
export function addDisplayValueToFilterValue(value: string, displayValue: string): string {
if (isNotEmpty(displayValue) && displayValue !== value) {
return displayValue + SEARCH_AUTHORITY_VALUE_SEPARATOR + value;
}
return value;
}

0 comments on commit 3cb5d62

Please sign in to comment.