Skip to content

Instantly share code, notes, and snippets.

@tim-minter
Last active April 19, 2022 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tim-minter/81cf77b18d6034e79384d5da0ac27fed to your computer and use it in GitHub Desktop.
Save tim-minter/81cf77b18d6034e79384d5da0ac27fed to your computer and use it in GitHub Desktop.
A simple passthrough filter with a text file filter list

If msg.entryToCheckFor exists in the text file then msg.dataToPassThrough is passed through to the output of the flow.

This was used as filter in a flow that got a list of users that had used a keyword in a post. The results contained users from the whole organisation but we were only interested in one team of people. The simplest way to filter the results so they only contained team members was to check the results against a list. A text list was the simplest method. We connected the results to this flow and if a user was in our team the result would be passed through else it would be discarded. Easy Peasey.

[{"id":"286bcc2.b1f9334","type":"comment","z":"5811904c.b5318","name":"Convert the string returned by the file node to an array and check if msg.entryToLookFor is in that array","info":"","x":1019.5,"y":448,"wires":[]},{"id":"42e3d289.191f24","type":"file in","z":"5811904c.b5318","name":"List Of Team Members","filename":"/Users/tim/Desktop/teammembers.txt","format":"utf8","x":706,"y":654,"wires":[["62714fd0.8e5cb","afebe3f4.ac2518"]]},{"id":"62714fd0.8e5cb","type":"function","z":"5811904c.b5318","name":"Check for Match","func":"var arrayOfTextEntries = msg.payload.split(\";\");\nvar entryToLookFor = msg.entryToLookFor;\nif (arrayOfTextEntries.indexOf(entryToLookFor)>=1){\n msg.found = true;\n msg.dataToPassThrough = msg.dataToPassThrough;\n} else {\n msg.found = false;\n msg.dataToPassThrough = null;\n}\nreturn msg;","outputs":1,"noerr":0,"x":699,"y":803,"wires":[["96fa4054.a60d8","9437e059.beb138"]]},{"id":"3095a18c.405446","type":"function","z":"5811904c.b5318","name":"Example input msg","func":"msg.entryToLookFor = \"Bob@minions.com\";\nmsg.dataToPassToOutput = \"This data will either be passed through to the output or not depending on whether there is a match with entryToCheckFor in the txt file\";\nreturn msg;","outputs":1,"noerr":0,"x":418,"y":653,"wires":[["42e3d289.191f24"]]},{"id":"96fa4054.a60d8","type":"debug","z":"5811904c.b5318","name":"","active":true,"console":"false","complete":"found","x":981,"y":804,"wires":[]},{"id":"afebe3f4.ac2518","type":"debug","z":"5811904c.b5318","name":"","active":true,"console":"false","complete":"false","x":982,"y":654,"wires":[]},{"id":"cd7c9508.e8dc","type":"comment","z":"5811904c.b5318","name":"A simple passthrough filter with a text file filter list","info":"","x":271.5,"y":545,"wires":[]},{"id":"175c753d.9157bb","type":"comment","z":"5811904c.b5318","name":"Output the contents of the file for testing purposes","info":"So that we can check we have found the file and what it contains","x":1099.5,"y":609,"wires":[]},{"id":"3c1a0ad3.fd3cee","type":"comment","z":"5811904c.b5318","name":"Have we found the string in the file?","info":"","x":1053.5,"y":768,"wires":[]},{"id":"1c6c86f2.414f71","type":"comment","z":"5811904c.b5318","name":"Full path to your text file","info":"eg /Users/tim/Desktop/teammembers.txt\n\nThis flow is written to accept a text file \nwith entries separated by semi-colons eg.\n\nDave@minions.com;Stuart@minions.com;Bob@minions.com;Jerry@minions.com;Carl@minions.com;Kevin@minions.com;Tim@minions.com;Mark@minions.com;Phil@minions.com;John@minions.com;Josh@minions.com;Steve@minions.com;Donny@minions.com;Ken@minions.com;Mike@minions.com;Paul@minions.com;Lance@minions.com;Larry@minions.com;Jorge@minions.com;Tom@minions.com;Norbert@minions.com;Chris@minions.com;Darwin@minions.com;\n\nThe separator is defined and can be changed in the split function within the Check for Match function","x":716.5,"y":611,"wires":[]},{"id":"3671cbaf.214834","type":"comment","z":"5811904c.b5318","name":"Convert the string returned by the file node to an array and check if msg.entryToLookFor is in that array","info":"","x":961.5,"y":847,"wires":[]},{"id":"9437e059.beb138","type":"debug","z":"5811904c.b5318","name":"","active":true,"console":"false","complete":"dataToPassToOutput","x":1025.5,"y":890,"wires":[]},{"id":"48e2a6d1.b039b","type":"comment","z":"5811904c.b5318","name":"The message will only pass through to here if msg.found = true","info":"","x":1146.5,"y":935,"wires":[]},{"id":"8fb56a45.995978","type":"inject","z":"5811904c.b5318","name":"Simulate Input","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":185.5,"y":653,"wires":[["3095a18c.405446"]]},{"id":"4d3c15b5.db69cc","type":"comment","z":"5811904c.b5318","name":"Flow Summary","info":"If msg.entryToCheckFor exists in the \ntext file then msg.dataToPassThrough is \npassed through to the output of the flow ","x":406.5,"y":699,"wires":[]}]
@TFWol
Copy link

TFWol commented Apr 19, 2022

Hello, just letting you know (arrayOfTextEntries.indexOf(entryToLookFor)>=1 should be (arrayOfTextEntries.indexOf(entryToLookFor)>-1.

The current code would result in a false negative and miss the first match at index 0 in the array.
Example:

List to match against:

user1 <--- This one would never get a hit since it's index 0 and the code says 'match Index 1 or higher'
user2
user3

REF: https://www.w3schools.com/jsref/jsref_indexof_array.asp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment