From 98bfb71d000741c84a63ced3a0f1955a8f7eeee1 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 20 May 2021 19:48:33 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20some=20Filters?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nest_frontend/utils/Filter.js | 62 +++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 nest_frontend/utils/Filter.js diff --git a/nest_frontend/utils/Filter.js b/nest_frontend/utils/Filter.js new file mode 100644 index 0000000..5551f2b --- /dev/null +++ b/nest_frontend/utils/Filter.js @@ -0,0 +1,62 @@ +export class Filter { + constructor() { + + } + + exec(tweet) { + return true + } +} + +export class ContainsFilter extends Filter { + word + + constructor(word) { + super() + this.word = word.toLowerCase().trim() + } + + exec(tweet) { + return tweet.content?.toLowerCase().includes(this.word) + } +} + +export class UserFilter extends Filter { + user + + constructor(user) { + super() + this.user = user.toLowerCase().trim().replace(/^@/, "") + } + + exec(tweet) { + return tweet.poster.toLowerCase() === this.user + } +} + +export class HasLocationFilter extends Filter { + hasLocation + + constructor(hasLocation) { + super() + this.hasLocation = hasLocation + } + + exec(tweet) { + return (tweet["location"] !== null) === this.hasLocation + } +} + +export class HasPlaceFilter extends Filter { + hasPlace + + constructor(hasPlace) { + super() + this.hasPlace = hasPlace + } + + exec(tweet) { + return (tweet["place"] !== null) === this.hasPlace + } +} +