@carto/api-client 0.0.42 → 0.0.44

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/filters.ts ADDED
@@ -0,0 +1,129 @@
1
+ import {FilterType} from './constants';
2
+ import {Filter} from './types';
3
+ import {isEmptyObject} from './utils';
4
+
5
+ type FilterTypeOptions<T extends FilterType> = {
6
+ type: T;
7
+ column: string;
8
+ } & Filter[T];
9
+
10
+ export type AddFilterOptions =
11
+ | FilterTypeOptions<FilterType.IN>
12
+ | FilterTypeOptions<FilterType.BETWEEN>
13
+ | FilterTypeOptions<FilterType.CLOSED_OPEN>
14
+ | FilterTypeOptions<FilterType.TIME>
15
+ | FilterTypeOptions<FilterType.STRING_SEARCH>;
16
+
17
+ /**
18
+ * Adds a {@link Filter} to the filter set. Any previous filters with the same
19
+ * `column` and `type` will be replaced.
20
+ */
21
+ export function addFilter(
22
+ filters: Record<string, Filter>,
23
+ {column, type, values, owner}: AddFilterOptions
24
+ ): Record<string, Filter> {
25
+ if (!filters[column]) {
26
+ filters[column] = {};
27
+ }
28
+
29
+ const filter = {values, owner} as FilterTypeOptions<typeof type>;
30
+ (filters[column][type] as FilterTypeOptions<typeof type>) = filter;
31
+
32
+ return filters;
33
+ }
34
+
35
+ export type RemoveFilterOptions = {
36
+ column: string;
37
+ owner?: string;
38
+ };
39
+
40
+ /**
41
+ * Removes one or more {@link Filter filters} from the filter set. If only
42
+ * `column` is specified, then all filters on that column are removed. If both
43
+ * `column` and `owner` are specified, then only filters for that column
44
+ * associated with the owner are removed.
45
+ */
46
+ export function removeFilter(
47
+ filters: Record<string, Filter>,
48
+ {column, owner}: RemoveFilterOptions
49
+ ): Record<string, Filter> {
50
+ const filter = filters[column];
51
+ if (!filter) {
52
+ return filters;
53
+ }
54
+
55
+ if (owner) {
56
+ for (const type of Object.values(FilterType)) {
57
+ if (owner === filter[type as FilterType]?.owner) {
58
+ delete filter[type as FilterType];
59
+ }
60
+ }
61
+ }
62
+
63
+ if (!owner || isEmptyObject(filter)) {
64
+ delete filters[column];
65
+ }
66
+
67
+ return filters;
68
+ }
69
+
70
+ /**
71
+ * Clears all {@link Filter filters} from the filter set.
72
+ */
73
+ export function clearFilters(
74
+ filters: Record<string, Filter>
75
+ ): Record<string, Filter> {
76
+ for (const column of Object.keys(filters)) {
77
+ delete filters[column];
78
+ }
79
+ return filters;
80
+ }
81
+
82
+ export type HasFilterOptions = {
83
+ column: string;
84
+ owner?: string;
85
+ };
86
+
87
+ export function hasFilter(
88
+ filters: Record<string, Filter>,
89
+ {column, owner}: HasFilterOptions
90
+ ): boolean {
91
+ const filter = filters[column];
92
+ if (!filter) {
93
+ return false;
94
+ }
95
+
96
+ if (!owner) {
97
+ return true;
98
+ }
99
+
100
+ for (const type of Object.values(FilterType)) {
101
+ if (owner === filter[type as FilterType]?.owner) {
102
+ return true;
103
+ }
104
+ }
105
+
106
+ return false;
107
+ }
108
+
109
+ export type GetFilterOptions<T extends FilterType> = {
110
+ column: string;
111
+ type: T;
112
+ owner?: string;
113
+ };
114
+
115
+ export function getFilter<T extends FilterType>(
116
+ filters: Record<string, Filter>,
117
+ {column, type, owner}: GetFilterOptions<T>
118
+ ): Filter[T] | null {
119
+ const filter = filters[column];
120
+ if (!filter) {
121
+ return null;
122
+ }
123
+
124
+ if (!owner || owner === filter[type as FilterType]?.owner) {
125
+ return filter[type] || null;
126
+ }
127
+
128
+ return null;
129
+ }
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './client.js';
2
2
  export * from './constants.js';
3
+ export * from './filters.js';
3
4
  export * from './sources/index.js';
4
5
  export * from './types.js';
package/src/utils.ts CHANGED
@@ -83,3 +83,10 @@ export class InvalidColumnError extends Error {
83
83
  );
84
84
  }
85
85
  }
86
+
87
+ export function isEmptyObject(object: object): boolean {
88
+ for (const _ in object) {
89
+ return false;
90
+ }
91
+ return true;
92
+ }