forked from offlegacy/event-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcondition.ts
More file actions
33 lines (29 loc) · 1.08 KB
/
Copy pathcondition.ts
File metadata and controls
33 lines (29 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import type { Context, EventParams, EnabledCondition } from "../types";
/**
* Evaluates the enabled condition for event tracking
*
* @param enabled - The enabled condition (boolean or function)
* @param context - The current context
* @param params - The event parameters
* @returns boolean indicating whether tracking should be executed
*/
export function evaluateEnabledCondition<
TContext extends Context = Context,
TEventParams extends EventParams = EventParams,
>(enabled: EnabledCondition<TContext, TEventParams> | undefined, context: TContext, params: unknown): boolean {
// Default to true if enabled is not specified
if (enabled === undefined) return true;
// Handle boolean values
if (typeof enabled === "boolean") return enabled;
// Handle function values
if (typeof enabled === "function") {
try {
return enabled(context, params as TEventParams);
} catch (error) {
console.warn("Enabled condition evaluation failed:", error);
return false; // Fail safe - don't track on error
}
}
// Fallback to true for any other cases
return true;
}