vinmonopolet-ts
Version:
Extracts information on products, categories and stores from Vinmonopolet
32 lines (31 loc) • 968 B
JavaScript
const identity = (val) => val;
const baseQuery = ":relevance:visibleInSearch:true:";
const reduceQuery = (query = "") => {
return query.indexOf(baseQuery) === 0
? query.substr(baseQuery.length)
: query;
};
class FacetValue {
name;
count;
query;
constructor(value, filter = identity) {
this.name = filter(value.name);
this.count = value.count;
this.query = reduceQuery(value.query && value.query.query.value);
}
static cooerce(facetVal) {
if (facetVal instanceof FacetValue) {
return facetVal;
}
const val = String(facetVal);
if (!/^\w+:.+/i.test(val)) {
throw new Error("Facet value string must be in <facet>:<value> format");
}
return new FacetValue({ query: { query: { value: val } } });
}
}
FacetValue.prototype.toString = function () {
return this.query;
};
export default FacetValue;