Tuesday, February 22, 2022

Format Dates In JavaScript

Working with a date and time in JS was awful in the past. I dont know about you, but I used MomentJS or date-nfs library. 

Now it is possible to start using the Internationalization API for formating a date and time. Support is good in all browsers.

Let see an example:

If you want to use the browser’s locale, you must pass an empty Array in the constructor. The second argument is options. For more details see the documentation.

There is a Intl.RelativeTimeFormat() class for relative manipulation with a date and time. The Intl.RelativeTimeFormat is similar to the Intl.DateTimeFormat.

Let see an example:

Bonus :)

Friday, February 18, 2022

Streams - Transform

Transform streams are a special kind of Duplex stream. Transform streams apply some kind of transformation to each chunk of data that they receive from the Writable side, and then make the transformed data available on the Readable side.

For implementing a new Transform stream, we have to fill _transform() and optionally _flush() methods.

Let see example:

Thursday, February 17, 2022

Streams - Readable, Writable

Streams are one of the most important components of Node.js 

For example, the fs module has createReadStream() for reading from a file and createWriteStream() for writing to a file, the HTTP request and response objects are essentially streams, the zlib module allows compress and decompress data using a streaming interface and, crypto module exposes streaming primitives like createCipheriv and createDecipheriv.

Every stream in Node.js is an implementation of one of the four base abstract classes. Writable, Readable, Duplex, Transform. Each stream class is also an instance of EventEmitter.

Streams support two operating modes:

  • Binary mode - buffers or strings
  • Object mode: objects

Readable streams

A Readable stream represents a source of data. There are two approaches to receive the data from a Readable stream: non-flowing (or paused) and flowing. 

Paused mode

In paused mode, you need to call read() on the stream instance repeatedly until every chunk of data has been read.

The highWaterMark property, passed as an option to fs.createReadStream, determines how much data buffers inside the stream.

All Readable streams begin in paused mode but can be switched to flowing mode in one of the following ways: 

  • Adding a 'data' event handler.
  • Calling the stream.resume() method.
  • Calling the stream.pipe() method to send the data to a Writable.

Flowing mode

You can call readable.pause() and readable.resume()

Readable streams from iterables

You can create Readable stream instances from arrays or other iterable objects using the Readable.from()

Writable streams

A Writable stream represents a data destination. The example bellow shows an elegant way to create a gziped copy of a file.

In the next post, I am going to look at Transform streams.

Monday, February 14, 2022

Compare locale string

Hi guys

this is a simple post, but I feel it useful. 

I wrote a comparison function for the Czech language in many times. A friend of mine show me a Intl.Collator object today. I acknowledge that I have overlooked it in the past many times.

Lets look at example:

Functions chaining with reduce

Functions chain is very strong pattern in functional programing.

Let see a very simple example:

The evaluation of a functions chain takes place from right to left.

The last step is to create a universal chain function with any number of arguments. I use the reduceRight function from the array with this.

Happy coding.