@talend/react-faceted-search-query-client
Version:
Faceted search Query client
95 lines (90 loc) • 3.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createTqlQuery = void 0;
var _daikonTqlClient = require("@talend/daikon-tql-client");
var _reactFacetedSearch = _interopRequireDefault(require("@talend/react-faceted-search"));
var _lodash = require("lodash");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const {
operatorNames
} = _reactFacetedSearch.default.helpers;
const getBadgeQueryValues = ({
properties
}) => [properties.attribute, properties.operator.name, properties.value];
const getBadgesQueryValues = badges => badges.map(getBadgeQueryValues);
const isValidValue = value => {
if (typeof value === 'string') {
return !(0, _lodash.isEmpty)(value);
}
return !Number.isNaN(value);
};
const keepValidValues = ({
properties
}) => {
if (Array.isArray(properties.value)) {
return properties.value.length && properties.value.every(({
id
}) => isValidValue(id));
}
return isValidValue(properties.value);
};
const removeBadgesWithEmptyValue = badges => badges.filter(keepValidValues);
const prepareBadges = (0, _lodash.flow)([removeBadgesWithEmptyValue, getBadgesQueryValues]);
/**
* Accept an instance of the query class and return a dictionary.
* Key is an operator and value is pointing to the corresponding instance class method.
* @param {Query} query instance of Query class
*/
const getTqlClassOperatorsDictionary = query => ({
[operatorNames.contains]: query.contains,
[operatorNames.containsIgnoreCase]: query.containsIgnoreCase,
[operatorNames.equals]: query.equal,
[operatorNames.in]: query.in,
[operatorNames.notEquals]: query.unequal,
[operatorNames.greaterThan]: query.greaterThan,
[operatorNames.greaterThanOrEquals]: query.greaterThanOrEqual,
[operatorNames.lessThan]: query.lessThan,
[operatorNames.lessThanOrEquals]: query.lessThanOrEqual,
[operatorNames.complies]: query.complies,
[operatorNames.wordComplies]: query.wordComplies
});
const formatValue = value => {
if (Array.isArray(value)) {
return value.map(val => val.id);
}
if (typeof value === 'string') {
return value.trim();
}
return value;
};
/**
* Evaluating the correct query method with the help of the dictionary and the operator.
*/
const fromBadgeToTql = badgesLength => (query, [category, operator, value], index) => {
const queryFunction = getTqlClassOperatorsDictionary(query)[operator];
const tqlQuery = queryFunction(category, formatValue(value));
if (index === badgesLength - 1) {
return tqlQuery;
}
return tqlQuery.and();
};
/**
* Create a proper tql query either from a collection of badgesFacetedPropTypes or a string
* @param {array[badgeFacetedPropTypes]} badges
* @see '../components/facetedSearch.propTypes.js'
*/
const createTqlQuery = facetedQuery => {
if (!facetedQuery || !facetedQuery.length) {
return '';
}
if (Array.isArray(facetedQuery)) {
const preparedBadges = prepareBadges(facetedQuery);
return preparedBadges.reduce(fromBadgeToTql(preparedBadges.length), new _daikonTqlClient.Query()).serialize();
}
return formatValue(facetedQuery);
};
// eslint-disable-next-line import/prefer-default-export
exports.createTqlQuery = createTqlQuery;
//# sourceMappingURL=tql.js.map