I am trying to create a custom pipe in Angular 5 that filters an array of events based on a given sequence. For instance, if my data is:
["submit", "click", "go_back", "click",...]
I want to filter this data based on up to three inputs. If input 1's value is "click", then I should only return events with all click occurrences. Additionally, if my input sequence is ["click", "submit", "go_back"], the output should only contain occurrences of that specific sequence.
["click","submit","go_back","click","submit","go_back","click",...]
I need to implement this functionality using array functions within an Angular pipe component. The sequence inputs are optional and limited to three. Currently, I have attempted to achieve this filtering logic with the following code:
transform(events: any[], event1?: string, event2?: string, event3?: string): any {
if (!events) { return []; }
if (event1) {
if (event2) {
if (event3) {
return events.filter((event, i) => {
// event.event represents the name of the event
return (event.event === event3 && events[i-1].event === event2 && events[i-2].event === event1);
});
} else {
console.log("here");
return events.filter((event, i) => {
return event.event === event2 && events[i-1].event === event1;
});
} else {
return events.filter((event, i) => event.event === event1);
}
} else {
return events;
}
}
Unfortunately, the current implementation or any other method I have tried so far results in displaying only the first event. In some cases, it shows each occurrence of the initial event types but still matching the specified sequence (e.g., "click" appearing n times for n matching sequences).