New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@internetarchive/search-service

Package Overview
Dependencies
Maintainers
17
Versions
158
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@internetarchive/search-service - npm Package Compare versions

Comparing version 0.4.3-alpha.1 to 0.4.3-alpha.2

24

demo/app-root.ts

@@ -265,11 +265,9 @@ import { html, css, LitElement, TemplateResult, CSSResult, nothing } from 'lit';

const target = e.target as HTMLButtonElement;
const { field, value } = target.dataset;
const { field, value, constraint } = target.dataset;
if (field && value) {
this.filterMap = { ...this.filterMap };
delete this.filterMap[field][value];
if (Object.keys(this.filterMap[field]).length === 0) {
delete this.filterMap[field];
}
if (field && value && constraint) {
this.filterMap = new FilterMapBuilder()
.setFilterMap(this.filterMap)
.removeSingleFilter(field, value, constraint as FilterConstraint)
.build();
}

@@ -282,3 +280,10 @@ }

for (const [value, constraint] of Object.entries(filters)) {
filtersArray.push({ field, value, constraint });
// The constraint may be either a single item or an array
if (Array.isArray(constraint)) {
for (const subConstraint of constraint) {
filtersArray.push({ field, value, constraint: subConstraint });
}
} else {
filtersArray.push({ field, value, constraint });
}
}

@@ -310,2 +315,3 @@ }

data-value=${value}
data-constraint=${constraint}
@click=${this.removeFilterClicked}

@@ -312,0 +318,0 @@ >

@@ -187,9 +187,8 @@ import { __decorate } from "tslib";

const target = e.target;
const { field, value } = target.dataset;
if (field && value) {
this.filterMap = { ...this.filterMap };
delete this.filterMap[field][value];
if (Object.keys(this.filterMap[field]).length === 0) {
delete this.filterMap[field];
}
const { field, value, constraint } = target.dataset;
if (field && value && constraint) {
this.filterMap = new FilterMapBuilder()
.setFilterMap(this.filterMap)
.removeSingleFilter(field, value, constraint)
.build();
}

@@ -201,3 +200,11 @@ }

for (const [value, constraint] of Object.entries(filters)) {
filtersArray.push({ field, value, constraint });
// The constraint may be either a single item or an array
if (Array.isArray(constraint)) {
for (const subConstraint of constraint) {
filtersArray.push({ field, value, constraint: subConstraint });
}
}
else {
filtersArray.push({ field, value, constraint });
}
}

@@ -226,2 +233,3 @@ }

data-value=${value}
data-constraint=${constraint}
@click=${this.removeFilterClicked}

@@ -228,0 +236,0 @@ >

@@ -9,3 +9,4 @@ import { FilterConstraint, FilterMap } from './search-params';

* Adds a filter to the FilterMap under construction.
* The new filter may overwrite or modify existing filters already added to the builder.
* If existing constraint(s) already exist for this field and value, then the old and new constraints
* will be joined into a single array.
* @param field The field to filter on (e.g., 'subject', 'year', ...)

@@ -18,8 +19,15 @@ * @param value The value of the field to filter on (e.g., 'Cicero', '1920', ...)

/**
* Removes any filter currently associated with the given field and value.
* Removes a single filter currently associated with the given field, value, and constraint type.
* @param field The field to remove a filter for
* @param value The value to remove the filter for
* @param constraint The constraint type to remove for this field and value
*/
removeFilter(field: string, value: string): this;
removeSingleFilter(field: string, value: string, constraint: FilterConstraint): this;
/**
* Removes any filters currently associated with the given field and value.
* @param field The field to remove a filter for
* @param value The value to remove the filter for
*/
removeFilters(field: string, value: string): this;
/**
* Initializes the filter map under construction to have filters exactly equal to the given one.

@@ -32,3 +40,3 @@ * This will overwrite *all* existing filters already added to the builder.

* Adds all filters from an existing filter map to the one being built.
* Filters from the provided map may overwrite or modify existing filters already added to the builder.
* Filters from the provided map may overwrite existing filters already added to the builder.
* @param map The FilterMap to merge into the one being built.

@@ -35,0 +43,0 @@ */

@@ -1,8 +0,1 @@

import { FilterConstraint } from './search-params';
const NUMERIC_CONSTRAINTS = [
FilterConstraint.GREATER_OR_EQUAL,
FilterConstraint.GREATER_THAN,
FilterConstraint.LESS_OR_EQUAL,
FilterConstraint.LESS_THAN,
];
/**

@@ -17,3 +10,4 @@ * A utility class for building filter maps

* Adds a filter to the FilterMap under construction.
* The new filter may overwrite or modify existing filters already added to the builder.
* If existing constraint(s) already exist for this field and value, then the old and new constraints
* will be joined into a single array.
* @param field The field to filter on (e.g., 'subject', 'year', ...)

@@ -28,52 +22,59 @@ * @param value The value of the field to filter on (e.g., 'Cicero', '1920', ...)

}
// Edge case for numeric fields (i.e., year)
// Logically it would be valid for excluded values to overlap with gt/gte/lt/lte values.
// For instance, specifying 1950 <= year <= 2000, but also excluding year = 1950.
// But since we can only have one constraint for a given value, these would overwrite each other.
// To work around this, we adjust the resulting map to maintain the correct logic.
const currentConstraint = this.filterMap[field][value];
if (currentConstraint && constraint === FilterConstraint.EXCLUDE) {
if (currentConstraint === FilterConstraint.GREATER_OR_EQUAL) {
// gte and exclude on the same value -> gt
this.filterMap[field][value] = FilterConstraint.GREATER_THAN;
return this;
}
else if (currentConstraint === FilterConstraint.LESS_OR_EQUAL) {
// lte and exclude on the same value -> lt
this.filterMap[field][value] = FilterConstraint.LESS_THAN;
return this;
}
else if (currentConstraint === FilterConstraint.GREATER_THAN ||
currentConstraint === FilterConstraint.LESS_THAN) {
// gt/lt and exclude -> no additional filter
return this;
}
// If there are already constraints for this value, concat them into an array
if (this.filterMap[field][value]) {
const mergedConstraints = [].concat(this.filterMap[field][value], constraint);
// Ensure there are no duplicate constraints in the array
this.filterMap[field][value] = Array.from(new Set(mergedConstraints));
}
// An 'include' constraint should always take precedence over a gt/gte/lt/lte constraint on the same value.
if (currentConstraint === FilterConstraint.INCLUDE) {
if (NUMERIC_CONSTRAINTS.includes(constraint)) {
// Don't overwrite the existing 'include' constraint
return this;
}
else {
// Otherwise just use the provided value
this.filterMap[field][value] = constraint;
}
// Otherwise, overwrite any existing filter for this value
this.filterMap[field][value] = constraint;
return this;
}
/**
* Removes any filter currently associated with the given field and value.
* Removes a single filter currently associated with the given field, value, and constraint type.
* @param field The field to remove a filter for
* @param value The value to remove the filter for
* @param constraint The constraint type to remove for this field and value
*/
removeFilter(field, value) {
if (this.filterMap[field]) {
removeSingleFilter(field, value, constraint) {
var _a;
if (!((_a = this.filterMap[field]) === null || _a === void 0 ? void 0 : _a[value]))
return this;
const constraints = [].concat(this.filterMap[field][value]);
const constraintIndex = constraints.indexOf(constraint);
if (constraintIndex >= 0) {
constraints.splice(constraintIndex, 1);
}
// 2 or more constraints -> leave as array
// 1 constraint -> pull out single constraint
// 0 constraints -> delete the value entirely
this.filterMap[field][value] =
constraints.length === 1 ? constraints[0] : constraints;
if (constraints.length === 0) {
delete this.filterMap[field][value];
// If there are no remaining filters for this field, delete the whole field object.
if (Object.keys(this.filterMap[field]).length === 0) {
delete this.filterMap[field];
}
}
// If there are no remaining filters for this field, delete the whole field object.
if (Object.keys(this.filterMap[field]).length === 0) {
delete this.filterMap[field];
}
return this;
}
/**
* Removes any filters currently associated with the given field and value.
* @param field The field to remove a filter for
* @param value The value to remove the filter for
*/
removeFilters(field, value) {
if (!this.filterMap[field])
return this;
delete this.filterMap[field][value];
// If there are no remaining filters for this field, delete the whole field object.
if (Object.keys(this.filterMap[field]).length === 0) {
delete this.filterMap[field];
}
return this;
}
/**
* Initializes the filter map under construction to have filters exactly equal to the given one.

@@ -89,3 +90,3 @@ * This will overwrite *all* existing filters already added to the builder.

* Adds all filters from an existing filter map to the one being built.
* Filters from the provided map may overwrite or modify existing filters already added to the builder.
* Filters from the provided map may overwrite existing filters already added to the builder.
* @param map The FilterMap to merge into the one being built.

@@ -96,3 +97,11 @@ */

for (const [value, constraint] of Object.entries(filters)) {
this.addFilter(field, value, constraint);
// There may be either a single constraint or an array of them
if (Array.isArray(constraint)) {
for (const subConstraint of constraint) {
this.addFilter(field, value, subConstraint);
}
}
else {
this.addFilter(field, value, constraint);
}
}

@@ -99,0 +108,0 @@ }

@@ -104,9 +104,11 @@ export interface AggregateSearchParam {

/**
* A filter mapping a field value to the type of constraint that it should impose on results.
* A filter mapping a field value to the type of constraint(s) that it should impose on results.
* Multiple constraints for the same value may be provided as an array.
*
* Some examples (where the values are members of `FilterConstraint`):
* Some examples (where the property values are members of `FilterConstraint`):
* - `{ 'puppies': INCLUDE }`
* - `{ '1950': GREATER_OR_EQUAL, '1970': LESS_OR_EQUAL }`
* - `{ '1950': [ GREATER_OR_EQUAL, EXCLUDE ] }`
*/
export declare type FieldFilter = Record<string, FilterConstraint>;
export declare type FieldFilter = Record<string, FilterConstraint | FilterConstraint[]>;
/**

@@ -113,0 +115,0 @@ * A map of fields (e.g., 'year', 'subject', ...) to the filters that should be

@@ -23,2 +23,11 @@ import { expect } from '@open-wc/testing';

});
it('can add multiple constraints for one value', () => {
const builder = new FilterMapBuilder();
builder.addFilter('foo', 'bar', FilterConstraint.INCLUDE);
expect(builder.build()).to.deep.equal({ foo: { bar: 'inc' } });
builder.addFilter('foo', 'bar', FilterConstraint.GREATER_OR_EQUAL);
expect(builder.build()).to.deep.equal({
foo: { bar: ['inc', 'gte'] },
});
});
it('can remove filters', () => {

@@ -33,3 +42,3 @@ const builder = new FilterMapBuilder();

});
builder.removeFilter('foo', 'bar');
builder.removeFilters('foo', 'bar');
expect(builder.build()).to.deep.equal({

@@ -39,9 +48,37 @@ foo: { beep: 'gt' },

});
builder.removeFilter('foo', 'beep');
builder.removeFilters('foo', 'beep');
expect(builder.build()).to.deep.equal({ baz: { boop: 'exc' } });
builder.removeFilter('not', 'exist');
builder.removeFilters('not', 'exist');
expect(builder.build()).to.deep.equal({ baz: { boop: 'exc' } });
builder.removeFilter('baz', 'boop');
builder.removeFilters('baz', 'boop');
expect(builder.build()).to.deep.equal({});
});
it('can remove single filters by constraint type', () => {
const builder = new FilterMapBuilder();
builder.addFilter('foo', 'bar', FilterConstraint.INCLUDE);
builder.addFilter('foo', 'bar', FilterConstraint.GREATER_OR_EQUAL);
builder.addFilter('baz', 'boop', FilterConstraint.EXCLUDE);
expect(builder.build()).to.deep.equal({
foo: { bar: ['inc', 'gte'] },
baz: { boop: 'exc' },
});
builder.removeSingleFilter('foo', 'bar', FilterConstraint.GREATER_OR_EQUAL);
expect(builder.build()).to.deep.equal({
foo: { bar: 'inc' },
baz: { boop: 'exc' },
});
builder.removeSingleFilter('foo', 'bar', FilterConstraint.EXCLUDE);
expect(builder.build()).to.deep.equal({
foo: { bar: 'inc' },
baz: { boop: 'exc' },
});
builder.removeSingleFilter('foo', 'bar', FilterConstraint.INCLUDE);
expect(builder.build()).to.deep.equal({
baz: { boop: 'exc' },
});
builder.removeSingleFilter('baz', 'boop', FilterConstraint.EXCLUDE);
expect(builder.build()).to.deep.equal({});
builder.removeSingleFilter('not', 'exist', FilterConstraint.INCLUDE);
expect(builder.build()).to.deep.equal({});
});
it('can be initialized with an existing filter map', () => {

@@ -70,54 +107,17 @@ const builder = new FilterMapBuilder();

baz: {
boop: FilterConstraint.EXCLUDE,
boop: [FilterConstraint.EXCLUDE, FilterConstraint.LESS_OR_EQUAL],
},
};
builder.addFilter('foo', 'bar', FilterConstraint.GREATER_OR_EQUAL);
builder.addFilter('foo', 'beep', FilterConstraint.LESS_OR_EQUAL);
expect(builder.build()).to.deep.equal({ foo: { beep: 'lte' } });
expect(builder.build()).to.deep.equal({
foo: { bar: 'gte', beep: 'lte' },
});
builder.mergeFilterMap(filterMap);
expect(builder.build()).to.deep.equal({
foo: { bar: 'inc', beep: 'lte' },
baz: { boop: 'exc' },
foo: { bar: ['gte', 'inc'], beep: 'lte' },
baz: { boop: ['exc', 'lte'] },
});
});
it('correctly resolves overlap between exclude and gte constraints', () => {
const builder = new FilterMapBuilder();
builder.addFilter('year', '1950', FilterConstraint.GREATER_OR_EQUAL);
expect(builder.build()).to.deep.equal({ year: { 1950: 'gte' } });
builder.addFilter('year', '1950', FilterConstraint.EXCLUDE);
// Overwrites constraint with GREATER_THAN
expect(builder.build()).to.deep.equal({ year: { 1950: 'gt' } });
});
it('correctly resolves overlap between exclude and lte constraints', () => {
const builder = new FilterMapBuilder();
builder.addFilter('year', '1950', FilterConstraint.LESS_OR_EQUAL);
expect(builder.build()).to.deep.equal({ year: { 1950: 'lte' } });
builder.addFilter('year', '1950', FilterConstraint.EXCLUDE);
// Overwrites constraint with LESS_THAN
expect(builder.build()).to.deep.equal({ year: { 1950: 'lt' } });
});
it('correctly resolves overlap between exclude and gt constraints', () => {
const builder = new FilterMapBuilder();
builder.addFilter('year', '1950', FilterConstraint.GREATER_THAN);
expect(builder.build()).to.deep.equal({ year: { 1950: 'gt' } });
builder.addFilter('year', '1950', FilterConstraint.EXCLUDE);
// Leaves constraint unchanged
expect(builder.build()).to.deep.equal({ year: { 1950: 'gt' } });
});
it('correctly resolves overlap between exclude and lt constraints', () => {
const builder = new FilterMapBuilder();
builder.addFilter('year', '1950', FilterConstraint.LESS_THAN);
expect(builder.build()).to.deep.equal({ year: { 1950: 'lt' } });
builder.addFilter('year', '1950', FilterConstraint.EXCLUDE);
// Leaves constraint unchanged
expect(builder.build()).to.deep.equal({ year: { 1950: 'lt' } });
});
it('correctly resolves overlap between include and numeric constraints', () => {
const builder = new FilterMapBuilder();
builder.addFilter('year', '1950', FilterConstraint.INCLUDE);
expect(builder.build()).to.deep.equal({ year: { 1950: 'inc' } });
builder.addFilter('year', '1950', FilterConstraint.GREATER_THAN);
// Existing include constraint takes precedence
expect(builder.build()).to.deep.equal({ year: { 1950: 'inc' } });
});
});
//# sourceMappingURL=filter-map-builder.test.js.map
{
"name": "@internetarchive/search-service",
"version": "0.4.3-alpha.1",
"version": "0.4.3-alpha.2",
"description": "A search service for the Internet Archive",

@@ -5,0 +5,0 @@ "license": "AGPL-3.0-only",

import { FilterConstraint, FilterMap } from './search-params';
const NUMERIC_CONSTRAINTS = [
FilterConstraint.GREATER_OR_EQUAL,
FilterConstraint.GREATER_THAN,
FilterConstraint.LESS_OR_EQUAL,
FilterConstraint.LESS_THAN,
];
/**

@@ -18,3 +11,4 @@ * A utility class for building filter maps

* Adds a filter to the FilterMap under construction.
* The new filter may overwrite or modify existing filters already added to the builder.
* If existing constraint(s) already exist for this field and value, then the old and new constraints
* will be joined into a single array.
* @param field The field to filter on (e.g., 'subject', 'year', ...)

@@ -30,36 +24,16 @@ * @param value The value of the field to filter on (e.g., 'Cicero', '1920', ...)

// Edge case for numeric fields (i.e., year)
// Logically it would be valid for excluded values to overlap with gt/gte/lt/lte values.
// For instance, specifying 1950 <= year <= 2000, but also excluding year = 1950.
// But since we can only have one constraint for a given value, these would overwrite each other.
// To work around this, we adjust the resulting map to maintain the correct logic.
const currentConstraint = this.filterMap[field][value];
if (currentConstraint && constraint === FilterConstraint.EXCLUDE) {
if (currentConstraint === FilterConstraint.GREATER_OR_EQUAL) {
// gte and exclude on the same value -> gt
this.filterMap[field][value] = FilterConstraint.GREATER_THAN;
return this;
} else if (currentConstraint === FilterConstraint.LESS_OR_EQUAL) {
// lte and exclude on the same value -> lt
this.filterMap[field][value] = FilterConstraint.LESS_THAN;
return this;
} else if (
currentConstraint === FilterConstraint.GREATER_THAN ||
currentConstraint === FilterConstraint.LESS_THAN
) {
// gt/lt and exclude -> no additional filter
return this;
}
}
// If there are already constraints for this value, concat them into an array
if (this.filterMap[field][value]) {
const mergedConstraints = ([] as FilterConstraint[]).concat(
this.filterMap[field][value],
constraint
);
// An 'include' constraint should always take precedence over a gt/gte/lt/lte constraint on the same value.
if (currentConstraint === FilterConstraint.INCLUDE) {
if (NUMERIC_CONSTRAINTS.includes(constraint)) {
// Don't overwrite the existing 'include' constraint
return this;
}
// Ensure there are no duplicate constraints in the array
this.filterMap[field][value] = Array.from(new Set(mergedConstraints));
} else {
// Otherwise just use the provided value
this.filterMap[field][value] = constraint;
}
// Otherwise, overwrite any existing filter for this value
this.filterMap[field][value] = constraint;
return this;

@@ -69,14 +43,34 @@ }

/**
* Removes any filter currently associated with the given field and value.
* Removes a single filter currently associated with the given field, value, and constraint type.
* @param field The field to remove a filter for
* @param value The value to remove the filter for
* @param constraint The constraint type to remove for this field and value
*/
removeFilter(field: string, value: string): this {
if (this.filterMap[field]) {
removeSingleFilter(
field: string,
value: string,
constraint: FilterConstraint
): this {
if (!this.filterMap[field]?.[value]) return this;
const constraints = ([] as FilterConstraint[]).concat(
this.filterMap[field][value]
);
const constraintIndex = constraints.indexOf(constraint);
if (constraintIndex >= 0) {
constraints.splice(constraintIndex, 1);
}
// 2 or more constraints -> leave as array
// 1 constraint -> pull out single constraint
// 0 constraints -> delete the value entirely
this.filterMap[field][value] =
constraints.length === 1 ? constraints[0] : constraints;
if (constraints.length === 0) {
delete this.filterMap[field][value];
}
// If there are no remaining filters for this field, delete the whole field object.
if (Object.keys(this.filterMap[field]).length === 0) {
delete this.filterMap[field];
}
// If there are no remaining filters for this field, delete the whole field object.
if (Object.keys(this.filterMap[field]).length === 0) {
delete this.filterMap[field];
}

@@ -88,2 +82,20 @@

/**
* Removes any filters currently associated with the given field and value.
* @param field The field to remove a filter for
* @param value The value to remove the filter for
*/
removeFilters(field: string, value: string): this {
if (!this.filterMap[field]) return this;
delete this.filterMap[field][value];
// If there are no remaining filters for this field, delete the whole field object.
if (Object.keys(this.filterMap[field]).length === 0) {
delete this.filterMap[field];
}
return this;
}
/**
* Initializes the filter map under construction to have filters exactly equal to the given one.

@@ -100,3 +112,3 @@ * This will overwrite *all* existing filters already added to the builder.

* Adds all filters from an existing filter map to the one being built.
* Filters from the provided map may overwrite or modify existing filters already added to the builder.
* Filters from the provided map may overwrite existing filters already added to the builder.
* @param map The FilterMap to merge into the one being built.

@@ -107,3 +119,10 @@ */

for (const [value, constraint] of Object.entries(filters)) {
this.addFilter(field, value, constraint);
// There may be either a single constraint or an array of them
if (Array.isArray(constraint)) {
for (const subConstraint of constraint) {
this.addFilter(field, value, subConstraint);
}
} else {
this.addFilter(field, value, constraint);
}
}

@@ -110,0 +129,0 @@ }

@@ -117,9 +117,11 @@ export interface AggregateSearchParam {

/**
* A filter mapping a field value to the type of constraint that it should impose on results.
* A filter mapping a field value to the type of constraint(s) that it should impose on results.
* Multiple constraints for the same value may be provided as an array.
*
* Some examples (where the values are members of `FilterConstraint`):
* Some examples (where the property values are members of `FilterConstraint`):
* - `{ 'puppies': INCLUDE }`
* - `{ '1950': GREATER_OR_EQUAL, '1970': LESS_OR_EQUAL }`
* - `{ '1950': [ GREATER_OR_EQUAL, EXCLUDE ] }`
*/
export type FieldFilter = Record<string, FilterConstraint>;
export type FieldFilter = Record<string, FilterConstraint | FilterConstraint[]>;

@@ -126,0 +128,0 @@ /**

@@ -28,2 +28,13 @@ import { expect } from '@open-wc/testing';

it('can add multiple constraints for one value', () => {
const builder = new FilterMapBuilder();
builder.addFilter('foo', 'bar', FilterConstraint.INCLUDE);
expect(builder.build()).to.deep.equal({ foo: { bar: 'inc' } });
builder.addFilter('foo', 'bar', FilterConstraint.GREATER_OR_EQUAL);
expect(builder.build()).to.deep.equal({
foo: { bar: ['inc', 'gte'] },
});
});
it('can remove filters', () => {

@@ -39,3 +50,3 @@ const builder = new FilterMapBuilder();

builder.removeFilter('foo', 'bar');
builder.removeFilters('foo', 'bar');
expect(builder.build()).to.deep.equal({

@@ -46,12 +57,46 @@ foo: { beep: 'gt' },

builder.removeFilter('foo', 'beep');
builder.removeFilters('foo', 'beep');
expect(builder.build()).to.deep.equal({ baz: { boop: 'exc' } });
builder.removeFilter('not', 'exist');
builder.removeFilters('not', 'exist');
expect(builder.build()).to.deep.equal({ baz: { boop: 'exc' } });
builder.removeFilter('baz', 'boop');
builder.removeFilters('baz', 'boop');
expect(builder.build()).to.deep.equal({});
});
it('can remove single filters by constraint type', () => {
const builder = new FilterMapBuilder();
builder.addFilter('foo', 'bar', FilterConstraint.INCLUDE);
builder.addFilter('foo', 'bar', FilterConstraint.GREATER_OR_EQUAL);
builder.addFilter('baz', 'boop', FilterConstraint.EXCLUDE);
expect(builder.build()).to.deep.equal({
foo: { bar: ['inc', 'gte'] },
baz: { boop: 'exc' },
});
builder.removeSingleFilter('foo', 'bar', FilterConstraint.GREATER_OR_EQUAL);
expect(builder.build()).to.deep.equal({
foo: { bar: 'inc' },
baz: { boop: 'exc' },
});
builder.removeSingleFilter('foo', 'bar', FilterConstraint.EXCLUDE);
expect(builder.build()).to.deep.equal({
foo: { bar: 'inc' },
baz: { boop: 'exc' },
});
builder.removeSingleFilter('foo', 'bar', FilterConstraint.INCLUDE);
expect(builder.build()).to.deep.equal({
baz: { boop: 'exc' },
});
builder.removeSingleFilter('baz', 'boop', FilterConstraint.EXCLUDE);
expect(builder.build()).to.deep.equal({});
builder.removeSingleFilter('not', 'exist', FilterConstraint.INCLUDE);
expect(builder.build()).to.deep.equal({});
});
it('can be initialized with an existing filter map', () => {

@@ -82,65 +127,18 @@ const builder = new FilterMapBuilder();

baz: {
boop: FilterConstraint.EXCLUDE,
boop: [FilterConstraint.EXCLUDE, FilterConstraint.LESS_OR_EQUAL],
},
};
builder.addFilter('foo', 'bar', FilterConstraint.GREATER_OR_EQUAL);
builder.addFilter('foo', 'beep', FilterConstraint.LESS_OR_EQUAL);
expect(builder.build()).to.deep.equal({ foo: { beep: 'lte' } });
expect(builder.build()).to.deep.equal({
foo: { bar: 'gte', beep: 'lte' },
});
builder.mergeFilterMap(filterMap);
expect(builder.build()).to.deep.equal({
foo: { bar: 'inc', beep: 'lte' },
baz: { boop: 'exc' },
foo: { bar: ['gte', 'inc'], beep: 'lte' },
baz: { boop: ['exc', 'lte'] },
});
});
it('correctly resolves overlap between exclude and gte constraints', () => {
const builder = new FilterMapBuilder();
builder.addFilter('year', '1950', FilterConstraint.GREATER_OR_EQUAL);
expect(builder.build()).to.deep.equal({ year: { 1950: 'gte' } });
builder.addFilter('year', '1950', FilterConstraint.EXCLUDE);
// Overwrites constraint with GREATER_THAN
expect(builder.build()).to.deep.equal({ year: { 1950: 'gt' } });
});
it('correctly resolves overlap between exclude and lte constraints', () => {
const builder = new FilterMapBuilder();
builder.addFilter('year', '1950', FilterConstraint.LESS_OR_EQUAL);
expect(builder.build()).to.deep.equal({ year: { 1950: 'lte' } });
builder.addFilter('year', '1950', FilterConstraint.EXCLUDE);
// Overwrites constraint with LESS_THAN
expect(builder.build()).to.deep.equal({ year: { 1950: 'lt' } });
});
it('correctly resolves overlap between exclude and gt constraints', () => {
const builder = new FilterMapBuilder();
builder.addFilter('year', '1950', FilterConstraint.GREATER_THAN);
expect(builder.build()).to.deep.equal({ year: { 1950: 'gt' } });
builder.addFilter('year', '1950', FilterConstraint.EXCLUDE);
// Leaves constraint unchanged
expect(builder.build()).to.deep.equal({ year: { 1950: 'gt' } });
});
it('correctly resolves overlap between exclude and lt constraints', () => {
const builder = new FilterMapBuilder();
builder.addFilter('year', '1950', FilterConstraint.LESS_THAN);
expect(builder.build()).to.deep.equal({ year: { 1950: 'lt' } });
builder.addFilter('year', '1950', FilterConstraint.EXCLUDE);
// Leaves constraint unchanged
expect(builder.build()).to.deep.equal({ year: { 1950: 'lt' } });
});
it('correctly resolves overlap between include and numeric constraints', () => {
const builder = new FilterMapBuilder();
builder.addFilter('year', '1950', FilterConstraint.INCLUDE);
expect(builder.build()).to.deep.equal({ year: { 1950: 'inc' } });
builder.addFilter('year', '1950', FilterConstraint.GREATER_THAN);
// Existing include constraint takes precedence
expect(builder.build()).to.deep.equal({ year: { 1950: 'inc' } });
});
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc