mirror of
https://github.com/imezx/Warp.git
synced 2025-04-25 15:40:02 +00:00
26 lines
886 B
TypeScript
26 lines
886 B
TypeScript
|
import type { InsightsAdditionalEventParams } from "../types";
|
||
|
|
||
|
export type WithAdditionalParams<TEventType> =
|
||
|
| InsightsAdditionalEventParams
|
||
|
| TEventType;
|
||
|
|
||
|
export function extractAdditionalParams<TEventType extends { index: string }>(
|
||
|
params: Array<InsightsAdditionalEventParams | TEventType>
|
||
|
): { events: TEventType[]; additionalParams?: InsightsAdditionalEventParams } {
|
||
|
return params.reduce(
|
||
|
({ events, additionalParams }, param) => {
|
||
|
// Real events all have `index` as a mandatory parameter, which we
|
||
|
// can rely on to distinguish them from additional parameters
|
||
|
if ("index" in param) {
|
||
|
return { additionalParams, events: [...events, param] };
|
||
|
}
|
||
|
|
||
|
return { events, additionalParams: param };
|
||
|
},
|
||
|
{
|
||
|
events: [] as TEventType[],
|
||
|
additionalParams: undefined as InsightsAdditionalEventParams | undefined
|
||
|
}
|
||
|
);
|
||
|
}
|