-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from CartoDB/feat/filter-fns
feat: Add addFilter, removeFilter, clearFilters
- Loading branch information
Showing
5 changed files
with
262 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import {FilterType} from './constants'; | ||
import {Filter} from './types'; | ||
import {isEmptyObject} from './utils'; | ||
|
||
type FilterTypeOptions<T extends FilterType> = { | ||
type: T; | ||
column: string; | ||
} & Filter[T]; | ||
|
||
export type AddFilterOptions = | ||
| FilterTypeOptions<FilterType.IN> | ||
| FilterTypeOptions<FilterType.BETWEEN> | ||
| FilterTypeOptions<FilterType.CLOSED_OPEN> | ||
| FilterTypeOptions<FilterType.TIME> | ||
| FilterTypeOptions<FilterType.STRING_SEARCH>; | ||
|
||
/** | ||
* Adds a {@link Filter} to the filter set. Any previous filters with the same | ||
* `column` and `type` will be replaced. | ||
*/ | ||
export function addFilter( | ||
filters: Record<string, Filter>, | ||
{column, type, values, owner}: AddFilterOptions | ||
): Record<string, Filter> { | ||
if (!filters[column]) { | ||
filters[column] = {}; | ||
} | ||
|
||
const filter = {values, owner} as FilterTypeOptions<typeof type>; | ||
(filters[column][type] as FilterTypeOptions<typeof type>) = filter; | ||
|
||
return filters; | ||
} | ||
|
||
export type RemoveFilterOptions = { | ||
column: string; | ||
owner?: string; | ||
}; | ||
|
||
/** | ||
* Removes one or more {@link Filter filters} from the filter set. If only | ||
* `column` is specified, then all filters on that column are removed. If both | ||
* `column` and `owner` are specified, then only filters for that column | ||
* associated with the owner are removed. | ||
*/ | ||
export function removeFilter( | ||
filters: Record<string, Filter>, | ||
{column, owner}: RemoveFilterOptions | ||
): Record<string, Filter> { | ||
const filter = filters[column]; | ||
if (!filter) { | ||
return filters; | ||
} | ||
|
||
if (owner) { | ||
for (const type in FilterType) { | ||
if (owner === filter[type as FilterType]?.owner) { | ||
delete filter[type as FilterType]; | ||
} | ||
} | ||
} | ||
|
||
if (!owner || isEmptyObject(filter)) { | ||
delete filters[column]; | ||
} | ||
|
||
return filters; | ||
} | ||
|
||
/** | ||
* Clears all {@link Filter filters} from the filter set. | ||
*/ | ||
export function clearFilters( | ||
filters: Record<string, Filter> | ||
): Record<string, Filter> { | ||
for (const column of Object.keys(filters)) { | ||
delete filters[column]; | ||
} | ||
return filters; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './client.js'; | ||
export * from './constants.js'; | ||
export * from './filters.js'; | ||
export * from './sources/index.js'; | ||
export * from './types.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import {expect, test} from 'vitest'; | ||
import { | ||
GroupDateType, | ||
FilterType, | ||
clearFilters, | ||
addFilter, | ||
removeFilter, | ||
} from '@carto/api-client'; | ||
|
||
test('addFilter', () => { | ||
let filters = {}; | ||
|
||
filters = addFilter(filters, { | ||
column: 'column_a', | ||
type: FilterType.IN, | ||
values: [1, 2], | ||
}); | ||
|
||
expect(filters).toMatchObject({ | ||
column_a: { | ||
[FilterType.IN]: {values: [1, 2]}, | ||
}, | ||
}); | ||
|
||
filters = addFilter(filters, { | ||
column: 'column_b', | ||
type: FilterType.IN, | ||
values: [3, 4], | ||
owner: 'my-widget', | ||
}); | ||
|
||
expect(filters).toMatchObject({ | ||
column_a: { | ||
[FilterType.IN]: {values: [1, 2]}, | ||
}, | ||
column_b: { | ||
[FilterType.IN]: {values: [3, 4], owner: 'my-widget'}, | ||
}, | ||
}); | ||
|
||
filters = addFilter(filters, { | ||
column: 'column_b', | ||
type: FilterType.BETWEEN, | ||
values: [[3, 4]], | ||
owner: 'my-widget-2', | ||
}); | ||
|
||
expect(filters).toMatchObject({ | ||
column_a: { | ||
[FilterType.IN]: {values: [1, 2]}, | ||
}, | ||
column_b: { | ||
[FilterType.IN]: {values: [3, 4], owner: 'my-widget'}, | ||
[FilterType.BETWEEN]: {values: [[3, 4]], owner: 'my-widget-2'}, | ||
}, | ||
}); | ||
|
||
filters = addFilter(filters, { | ||
column: 'column_b', | ||
type: FilterType.IN, | ||
values: ['a', 'b'], | ||
owner: 'my-widget-3', | ||
}); | ||
|
||
expect(filters).toMatchObject({ | ||
column_a: { | ||
[FilterType.IN]: {values: [1, 2]}, | ||
}, | ||
column_b: { | ||
[FilterType.IN]: {values: ['a', 'b'], owner: 'my-widget-3'}, | ||
[FilterType.BETWEEN]: {values: [[3, 4]], owner: 'my-widget-2'}, | ||
}, | ||
}); | ||
}); | ||
|
||
test('removeFilter', () => { | ||
let filters = {}; | ||
|
||
filters = removeFilter(filters, { | ||
column: 'no-such-column', | ||
owner: 'my-widget', | ||
}); | ||
|
||
expect(filters).toMatchObject({}); | ||
|
||
filters = { | ||
column_a: { | ||
[FilterType.IN]: {values: [1, 2]}, | ||
}, | ||
column_b: { | ||
[FilterType.IN]: {values: ['a', 'b'], owner: 'my-widget-3'}, | ||
[FilterType.BETWEEN]: {values: [[3, 4]], owner: 'my-widget-2'}, | ||
}, | ||
}; | ||
|
||
filters = removeFilter(filters, { | ||
column: 'column_b', | ||
owner: 'my-widget-3', | ||
}); | ||
|
||
expect(filters).toMatchObject({ | ||
column_a: { | ||
[FilterType.IN]: {values: [1, 2]}, | ||
}, | ||
column_b: { | ||
[FilterType.BETWEEN]: {values: [[3, 4]], owner: 'my-widget-2'}, | ||
}, | ||
}); | ||
|
||
filters = removeFilter(filters, { | ||
column: 'column_b', | ||
owner: 'no-such-owner', | ||
}); | ||
|
||
expect(filters).toMatchObject({ | ||
column_a: { | ||
[FilterType.IN]: {values: [1, 2]}, | ||
}, | ||
column_b: { | ||
[FilterType.BETWEEN]: {values: [[3, 4]], owner: 'my-widget-2'}, | ||
}, | ||
}); | ||
|
||
filters = removeFilter(filters, { | ||
column: 'column_b', | ||
}); | ||
|
||
expect(filters).toMatchObject({ | ||
column_a: { | ||
[FilterType.IN]: {values: [1, 2]}, | ||
}, | ||
}); | ||
|
||
filters = removeFilter(filters, { | ||
column: 'no-such-column', | ||
}); | ||
|
||
expect(filters).toMatchObject({ | ||
column_a: { | ||
[FilterType.IN]: {values: [1, 2]}, | ||
}, | ||
}); | ||
}); | ||
|
||
test('clearFilters', () => { | ||
let filters = clearFilters({}); | ||
|
||
expect(filters).toMatchObject({}); | ||
|
||
filters = clearFilters({ | ||
column_a: {[FilterType.IN]: {values: [1, 2]}}, | ||
column_b: { | ||
[FilterType.IN]: {values: [3, 4]}, | ||
[FilterType.BETWEEN]: {values: [[0, 1]]}, | ||
}, | ||
}); | ||
|
||
expect(filters).toMatchObject({}); | ||
}); |