289 lines
7.8 KiB
TypeScript
289 lines
7.8 KiB
TypeScript
import { DATA_TYPE, FILTER_COLUMNS, OPERATORS } from '@/lib/constants';
|
|
import type {
|
|
Filter,
|
|
Operator,
|
|
PropertyFilter,
|
|
QueryFilters,
|
|
QueryOptions,
|
|
EventPropertyFilter,
|
|
SessionPropertyFilter,
|
|
} from '@/lib/types';
|
|
|
|
const VALID_OPERATORS: Operator[] = Object.values(OPERATORS);
|
|
const VALID_EVENT_DATA_TYPES = Object.values(DATA_TYPE);
|
|
const EVENT_PROPERTY_FILTER_PARAM = /^epf\d+$/;
|
|
const SESSION_PROPERTY_FILTER_PARAM = /^spf\d+$/;
|
|
|
|
function isValidEventDataType(value: number): value is (typeof VALID_EVENT_DATA_TYPES)[number] {
|
|
return VALID_EVENT_DATA_TYPES.includes(value as (typeof VALID_EVENT_DATA_TYPES)[number]);
|
|
}
|
|
|
|
function resolveOperator(value?: string): Operator | undefined {
|
|
if (!value) {
|
|
return undefined;
|
|
}
|
|
|
|
return VALID_OPERATORS.find(operator => operator === value);
|
|
}
|
|
|
|
export function parseFilterValue(param: any) {
|
|
if (typeof param === 'string') {
|
|
const operatorValues = Object.values(OPERATORS).join('|');
|
|
|
|
const regex = new RegExp(`^(${operatorValues})\\.(.*)$`);
|
|
|
|
const [, operator, value] = param.match(regex) || [];
|
|
|
|
const resolvedOperator = resolveOperator(operator) ?? OPERATORS.equals;
|
|
const resolvedValue = value ?? param;
|
|
|
|
if (resolvedOperator === OPERATORS.equals || resolvedOperator === OPERATORS.notEquals) {
|
|
return { operator: resolvedOperator, value: resolvedValue.split(',') };
|
|
}
|
|
|
|
return { operator: resolvedOperator, value: resolvedValue };
|
|
}
|
|
|
|
if (Array.isArray(param)) {
|
|
return { operator: OPERATORS.equals, value: param };
|
|
}
|
|
|
|
return { operator: OPERATORS.equals, value: [param] };
|
|
}
|
|
|
|
export function isEqualsOperator(operator: any) {
|
|
return [OPERATORS.equals, OPERATORS.notEquals].includes(operator);
|
|
}
|
|
|
|
export function isSearchOperator(operator: any) {
|
|
return [
|
|
OPERATORS.contains,
|
|
OPERATORS.doesNotContain,
|
|
OPERATORS.regex,
|
|
OPERATORS.notRegex,
|
|
].includes(operator);
|
|
}
|
|
|
|
export function filtersObjectToArray(filters: QueryFilters, options: QueryOptions = {}): Filter[] {
|
|
if (!filters) {
|
|
return [];
|
|
}
|
|
|
|
return Object.keys(filters).reduce((arr, key) => {
|
|
const filter = filters[key];
|
|
|
|
if (filter === undefined || filter === null) {
|
|
return arr;
|
|
}
|
|
|
|
const baseName = key.replace(/\d+$/, '');
|
|
const paramName = key !== baseName ? key : undefined;
|
|
|
|
if (filter?.name && filter?.value !== undefined) {
|
|
return arr.concat({
|
|
...filter,
|
|
column: options?.columns?.[baseName] ?? FILTER_COLUMNS[baseName],
|
|
paramName: paramName ?? filter.paramName,
|
|
});
|
|
}
|
|
|
|
const { operator, value } = parseFilterValue(filter);
|
|
|
|
return arr.concat({
|
|
name: baseName,
|
|
paramName,
|
|
column: options?.columns?.[baseName] ?? FILTER_COLUMNS[baseName],
|
|
operator,
|
|
value,
|
|
prefix: options?.prefix,
|
|
});
|
|
}, []);
|
|
}
|
|
|
|
export function filtersArrayToObject(filters: Filter[]) {
|
|
const nameCounts: Record<string, number> = {};
|
|
return filters.reduce((obj, filter: Filter) => {
|
|
const { name, operator, value } = filter;
|
|
const count = nameCounts[name] ?? 0;
|
|
const key = count === 0 ? name : `${name}${count}`;
|
|
nameCounts[name] = count + 1;
|
|
|
|
obj[key] = `${operator}.${Array.isArray(value) ? value.join(',') : value}`;
|
|
|
|
return obj;
|
|
}, {});
|
|
}
|
|
|
|
function getPropertyFilterPrefix(key: string, prefixes: string[]) {
|
|
return prefixes.find(prefix => key.startsWith(`${prefix}_`));
|
|
}
|
|
|
|
function decodePropertyName(value: string) {
|
|
try {
|
|
return decodeURIComponent(value);
|
|
} catch {
|
|
return value;
|
|
}
|
|
}
|
|
|
|
function encodePropertyName(value: string) {
|
|
return encodeURIComponent(value).replaceAll('.', '%2E');
|
|
}
|
|
|
|
export function parsePropertyFilters(
|
|
query: Record<string, any>,
|
|
prefixes: string[] = ['pf'],
|
|
): PropertyFilter[] {
|
|
return Object.entries(query).flatMap(([key, val]) => {
|
|
const prefix = getPropertyFilterPrefix(key, prefixes);
|
|
|
|
if (!prefix) {
|
|
return [];
|
|
}
|
|
|
|
const stringValue = String(val);
|
|
const withoutPrefix = key.slice(prefix.length + 1);
|
|
const propertyName = withoutPrefix.replace(/\d+$/, ''); // strip trailing index digits
|
|
const prefixedDotMatch = stringValue.match(/^(\d+)\.([^.]+)\.(.*)$/);
|
|
const untypedDotMatch = stringValue.match(/^([^.]+)\.(.*)$/);
|
|
const explicitDataType = prefixedDotMatch ? Number(prefixedDotMatch[1]) : undefined;
|
|
const rawOperator = prefixedDotMatch ? prefixedDotMatch[2] : untypedDotMatch?.[1];
|
|
const operator = resolveOperator(rawOperator);
|
|
|
|
if (
|
|
!operator ||
|
|
(explicitDataType !== undefined && !isValidEventDataType(explicitDataType))
|
|
) {
|
|
return [];
|
|
}
|
|
|
|
const value = prefixedDotMatch ? prefixedDotMatch[3] : untypedDotMatch?.[2];
|
|
|
|
if (value === undefined) {
|
|
return [];
|
|
}
|
|
|
|
return [
|
|
{
|
|
propertyName,
|
|
dataType: explicitDataType ?? DATA_TYPE.string,
|
|
operator,
|
|
value,
|
|
},
|
|
];
|
|
});
|
|
}
|
|
|
|
export function parseEventPropertyFilters(query: Record<string, any>) {
|
|
return parsePropertyFilters(query, ['pf']);
|
|
}
|
|
|
|
export function parseUniversalEventPropertyFilters(
|
|
query: Record<string, any>,
|
|
): EventPropertyFilter[] {
|
|
return Object.entries(query).flatMap(([key, val]) => {
|
|
if (!EVENT_PROPERTY_FILTER_PARAM.test(key)) {
|
|
return [];
|
|
}
|
|
|
|
const stringValue = String(val);
|
|
const match = stringValue.match(/^(\d+)\.([^.]+)\.([^.]+)\.(.*)$/);
|
|
const explicitDataType = match ? Number(match[1]) : undefined;
|
|
const operator = resolveOperator(match?.[2]);
|
|
const propertyName = match?.[3] ? decodePropertyName(match[3]) : undefined;
|
|
const value = match?.[4];
|
|
|
|
if (
|
|
!propertyName ||
|
|
!operator ||
|
|
explicitDataType === undefined ||
|
|
!isValidEventDataType(explicitDataType) ||
|
|
value === undefined
|
|
) {
|
|
return [];
|
|
}
|
|
|
|
return [
|
|
{
|
|
propertyName,
|
|
dataType: explicitDataType,
|
|
operator,
|
|
value,
|
|
},
|
|
];
|
|
});
|
|
}
|
|
|
|
export function parseSessionPropertyFilters(query: Record<string, any>): SessionPropertyFilter[] {
|
|
return Object.entries(query).flatMap(([key, val]) => {
|
|
if (!SESSION_PROPERTY_FILTER_PARAM.test(key)) {
|
|
return [];
|
|
}
|
|
|
|
const stringValue = String(val);
|
|
const match = stringValue.match(/^(\d+)\.([^.]+)\.([^.]+)\.(.*)$/);
|
|
const explicitDataType = match ? Number(match[1]) : undefined;
|
|
const operator = resolveOperator(match?.[2]);
|
|
const propertyName = match?.[3] ? decodePropertyName(match[3]) : undefined;
|
|
const value = match?.[4];
|
|
|
|
if (
|
|
!propertyName ||
|
|
!operator ||
|
|
explicitDataType === undefined ||
|
|
!isValidEventDataType(explicitDataType) ||
|
|
value === undefined
|
|
) {
|
|
return [];
|
|
}
|
|
|
|
return [
|
|
{
|
|
propertyName,
|
|
dataType: explicitDataType,
|
|
operator,
|
|
value,
|
|
},
|
|
];
|
|
});
|
|
}
|
|
|
|
export function serializePropertyFilters(
|
|
filters: PropertyFilter[],
|
|
prefix = 'pf',
|
|
): Record<string, string> {
|
|
const counts: Record<string, number> = {};
|
|
return Object.fromEntries(
|
|
filters.map(f => {
|
|
const n = counts[f.propertyName] ?? 0;
|
|
counts[f.propertyName] = n + 1;
|
|
return [
|
|
`${prefix}_${f.propertyName}${n > 0 ? n : ''}`,
|
|
`${f.dataType}.${f.operator}.${f.value}`,
|
|
];
|
|
}),
|
|
);
|
|
}
|
|
|
|
export function serializeEventPropertyFilters(filters: PropertyFilter[]) {
|
|
return serializePropertyFilters(filters, 'pf');
|
|
}
|
|
|
|
export function serializeUniversalEventPropertyFilters(filters: EventPropertyFilter[]) {
|
|
return Object.fromEntries(
|
|
filters.map((filter, index) => [
|
|
`epf${index}`,
|
|
`${filter.dataType}.${filter.operator}.${encodePropertyName(filter.propertyName)}.${filter.value}`,
|
|
]),
|
|
);
|
|
}
|
|
|
|
export function serializeSessionPropertyFilters(filters: SessionPropertyFilter[]) {
|
|
return Object.fromEntries(
|
|
filters.map((filter, index) => [
|
|
`spf${index}`,
|
|
`${filter.dataType}.${filter.operator}.${encodePropertyName(filter.propertyName)}.${filter.value}`,
|
|
]),
|
|
);
|
|
}
|