Thursday, May 18, 2023

Function composition

Function composition is a mathematical operation that combines two or more functions in such a way that the output of one function becomes the input for the next function. The result is a new function that combines the behavior of both functions.

Implementation of general composition function

The corresponding execution order is from left to right. If you want to execute from right to left, then you can use the Array.prototype.reduceRight method.


Tuesday, April 25, 2023

Memoization function

I found an interesting function while working on my latest project. I call it a memoization function.

The function stores the results in memory and returns immediately if called repeatedly with the same arguments. Such a function is suitable for some longer and demanding calculations.

That is nothing new, but it's a nice piece of code. ;)

Look at this:

Tuesday, April 18, 2023

Dynamically loading font resources

Did you know, that fonts can be downloaded with Javascript using a new API, the CSS Font Loading API?

The API exposes 2 objects - FontFace and FontFaceSet. A FontFace represents a font. After loaded it, the font can be added to the list of available fonts (called FontFaceSet), and then can be used normally.

See example:

Tuesday, March 14, 2023

Extract the non-tagged contents of HTML in browser

Yes, you can use the RegularExpression of cource. But, there is cool idea, how to extract tagged content in browser:

JS String padding

Sometimes we want the string to have a specific length. We can use the padStart and padEnd methods.

Sunday, February 5, 2023

Removing Event Listeners with AbortController()

With the AbortControler you can cancel fetch request but remove Event Listeners too.

Let's look at an example:

You can use one signal to remove multiple listeners:

Friday, December 2, 2022

Promise with timeout

When you use await, it will wait until the Promise is either resolved or rejected. But sometimes API requests we make don’t get any response from the server.

You can use AbortController to abort one or more Web requests. The second way to solve this is to create a promise with a timeout.