Skip to content

Commit

Permalink
[backend] add string operators for stix filtering (#6799)
Browse files Browse the repository at this point in the history
  • Loading branch information
Archidoit committed May 13, 2024
1 parent dfb3ba6 commit 623c831
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ export const testGenericFilter = <T extends string | number | boolean>({ mode, o
// we need to find all of them or none of them
return (operator === 'eq' && adaptedFilterValues.every((v) => stixCandidates.includes(v)))
|| (operator === 'not_eq' && adaptedFilterValues.every((v) => !stixCandidates.includes(v)))
|| (operator === 'contains' && adaptedFilterValues.every((v) => stixCandidates.some((c) => typeof c === 'string' && typeof v === 'string' && c.includes(v))))
|| (operator === 'not_contains' && adaptedFilterValues.every((v) => !stixCandidates.some((c) => typeof c === 'string' && typeof v === 'string' && c.includes(v))))
|| (operator === 'starts_with' && adaptedFilterValues.every((v) => stixCandidates.some((c) => typeof c === 'string' && typeof v === 'string' && c.startsWith(v))))
|| (operator === 'not_starts_with' && adaptedFilterValues.every((v) => !stixCandidates.some((c) => typeof c === 'string' && typeof v === 'string' && c.startsWith(v))))
|| (operator === 'ends_with' && adaptedFilterValues.every((v) => stixCandidates.some((c) => typeof c === 'string' && typeof v === 'string' && c.endsWith(v))))
|| (operator === 'not_ends_with' && adaptedFilterValues.every((v) => !stixCandidates.some((c) => typeof c === 'string' && typeof v === 'string' && c.endsWith(v))))
|| (operator === 'search' && adaptedFilterValues.every((v) => stixCandidates.some((c) => typeof c === 'string' && typeof v === 'string'
&& (v.split(' ').some((word) => c.includes(word)))))) // a stix candidate should contains at least one of the filter values words

// In real cases, there is only 1 filter value with the next operators (not much sense otherwise)
|| (operator === 'lt' && adaptedFilterValues.every((v) => stixCandidates.some((c) => c < v)))
Expand All @@ -60,6 +68,14 @@ export const testGenericFilter = <T extends string | number | boolean>({ mode, o
// we need to find one of them or at least one is not found
return (operator === 'eq' && adaptedFilterValues.some((v) => stixCandidates.includes(v)))
|| (operator === 'not_eq' && adaptedFilterValues.some((v) => !stixCandidates.includes(v)))
|| (operator === 'contains' && adaptedFilterValues.some((v) => stixCandidates.some((c) => typeof c === 'string' && typeof v === 'string' && c.includes(v))))
|| (operator === 'not_contains' && adaptedFilterValues.some((v) => !stixCandidates.some((c) => typeof c === 'string' && typeof v === 'string' && c.includes(v))))
|| (operator === 'starts_with' && adaptedFilterValues.some((v) => stixCandidates.some((c) => typeof c === 'string' && typeof v === 'string' && c.startsWith(v))))
|| (operator === 'not_starts_with' && adaptedFilterValues.some((v) => !stixCandidates.some((c) => typeof c === 'string' && typeof v === 'string' && c.startsWith(v))))
|| (operator === 'ends_with' && adaptedFilterValues.some((v) => stixCandidates.some((c) => typeof c === 'string' && typeof v === 'string' && c.endsWith(v))))
|| (operator === 'not_ends_with' && adaptedFilterValues.some((v) => !stixCandidates.some((c) => typeof c === 'string' && typeof v === 'string' && c.endsWith(v))))
|| (operator === 'search' && adaptedFilterValues.some((v) => stixCandidates.some((c) => typeof c === 'string' && typeof v === 'string'
&& (v.split(' ').some((word) => c.includes(word)))))) // a stix candidate should contains at least one of the filter values words

// In real cases, there is only 1 filter value with the next operators (not much sense otherwise)
|| (operator === 'lt' && adaptedFilterValues.some((v) => stixCandidates.some((c) => c < v)))
Expand Down

0 comments on commit 623c831

Please sign in to comment.