How to make a parser and a filter?

I’ve waited to post this on Monday morning because… well, even if you don’t like coffee, more than any morning, the start of the week is certainly for “Coffee and Contemplation”.

(I prefer tea, but unfortunately, “Tea and Contemplation” doesn’t rol…

I’ve waited to post this on Monday morning because… well, even if you don’t like coffee, more than any morning, the start of the week is certainly for “Coffee and Contemplation”.

(I prefer tea, but unfortunately, “Tea and Contemplation” doesn’t roll of the tongue.)

Now that you’re primed, what am I talking about?



Parser

Parser takes a input (generally text) and transforms it in a data structure that can be used in the following steps.



Filter

Filter is a… filter… that filter things. More enter and less get out.

Anyway…

What can you do if you combine then?



A search engine!

Of course there’s a lot more behind the biggest ones you can think, but in essence it’s that it.

From the query you create a structure that the filter consumes, and at the end of the pipeline you have the results you want.



Have you ever tried making one?

The easiest thing you can make, and here I’ll be using Javascript is something like:

const sourceArray = [`your array with data`];
const queryString = 'probably from a input';

const filteredDataToReturn = sourceArray.filter((element) => {
    return JSON.stringify(element).includes(queryString);
});

I did say the easiest right?

The “more right” way to do it would be:

Object.values(element).includes(queryString)

Which is still really simple.

But far from any search engine



Enters EasyFilter!

EasyFilter print from NPM

Yes, as you might be realizing now… this is a shamelessly self promotion post… but please, I will teach you how to make your own “EasyFilter”!

https://www.npmjs.com/package/@noriller/easy-filter

If you wanted to get even close to a “real” search engine you have to put in the work, and when you’re busy doing other things, you just don’t have the time to give your users what they expect a search input to be: “Google”.

More than just random words, you use quotes and even tag:value pairs… my inspirations were:

  • Google / Gmail
  • Stackoverflow
  • Github

Seriously… check their advanced search… there’s so much you can filter!

And with EasyFilter you can simply:

const queryString = `search for something "this between quotes" and then here:"you search for this"`
const filter = EasyFilter(sourceArray)
const filteredResult = filter.search(queryString)

And it will filter just like you expect! It works for single values, quoted values and even values nested inside keys. AND MORE! (Check the documentation!)

You don’t need to settle for the easy, you can have it be easy for you and powerful for the users.



How do I make one?

For now… check the docs or the source code. 😋

https://github.com/Noriller/easy-filter

https://github.com/Noriller/easy-filter-parser

And next week, we start at earnest.

Also… EasyFilter ended with as just a warming

Let’s just say you can wait for the… Sequel.

(If you read the docs, you will understand.)

buy me a coffee

Cover Photo by Nathan Dumlao on Unsplash


Print Share Comment Cite Upload Translate
APA
Bruno Noriller | Sciencx (2024-03-28T20:23:04+00:00) » How to make a parser and a filter?. Retrieved from https://www.scien.cx/2021/10/11/how-to-make-a-parser-and-a-filter/.
MLA
" » How to make a parser and a filter?." Bruno Noriller | Sciencx - Monday October 11, 2021, https://www.scien.cx/2021/10/11/how-to-make-a-parser-and-a-filter/
HARVARD
Bruno Noriller | Sciencx Monday October 11, 2021 » How to make a parser and a filter?., viewed 2024-03-28T20:23:04+00:00,<https://www.scien.cx/2021/10/11/how-to-make-a-parser-and-a-filter/>
VANCOUVER
Bruno Noriller | Sciencx - » How to make a parser and a filter?. [Internet]. [Accessed 2024-03-28T20:23:04+00:00]. Available from: https://www.scien.cx/2021/10/11/how-to-make-a-parser-and-a-filter/
CHICAGO
" » How to make a parser and a filter?." Bruno Noriller | Sciencx - Accessed 2024-03-28T20:23:04+00:00. https://www.scien.cx/2021/10/11/how-to-make-a-parser-and-a-filter/
IEEE
" » How to make a parser and a filter?." Bruno Noriller | Sciencx [Online]. Available: https://www.scien.cx/2021/10/11/how-to-make-a-parser-and-a-filter/. [Accessed: 2024-03-28T20:23:04+00:00]
rf:citation
» How to make a parser and a filter? | Bruno Noriller | Sciencx | https://www.scien.cx/2021/10/11/how-to-make-a-parser-and-a-filter/ | 2024-03-28T20:23:04+00:00
https://github.com/addpipe/simple-recorderjs-demo