- [Instructor] Hi and welcome to the next video
in section four, compressing and decompressing JavaScript.
In the previous video we learned about finding
the frequency of words used in the given file.
In this video, we are going to write a JavaScript
compressor or obfuscation tool as well as a
decompressing tool.
JavaScript is widely used for designing websites.
While writing the JavaScript code, we use
several white spaces, commas and tabs for readability
and maintenance of the code.
This causes the file size to increase.
As the file size increases, so does the time
taken to load the page.
Hence, most of the professional websites use
compressed JavaScript files for fast loading.
This compression, also known as midified to JS is
mostly squeezing white space and new line characters.
Once JavaScript is compressed, it can be decompressed
by adding enough white space and new line characters
which make it readable.
This section is an attempt to produce similar
capabilities in the shell.
Let's consider the following sample JavaScript.
Following other tasks that we need to perform
following compressing JavaScript, remove new line
and tab characters, squeeze spaces, replace
commas that look like slash star, content star slash.
To decompress or to make the JavaScript more readable,
we can use the following tasks, replace colon
with colon slash n, replace open curly brace with open
curly brace slash n, closed curly brace with slash n.
Let's see how to do it.
Using the steps that we now have in mind, we can
use the following command chain.
The output is as follows.
We can write a decompression script for making
the obfuscated code readable as follows.
There is a limitation in the script that it even
gets rid of extra spaces where it is intended.
For example, if you have a line like the following.
The two spaces will be converted into one space.
Things like these are possible to fix by using the
pattern matching tools which we have discussed.
Also when dealing with mission critical JavaScript code.
It's advised to use well established tools to do this.
The compression command forms the following tasks.
Remove the slash n and slash the characters, use
this command.
Removes extra spaces, use this command.
Remove commas we can use this command.
Colon is used a set delimiter to avoid the
need of escaping slash, since we need to use
slash star and star slash instead.
Star is escaped as slash star, dot star is used
to match all the text between slash star and star slash.
Removes all the spaces proceeding and suffixing the.
The preceding said statement works as follows.
This in the said code is in the match part.
And this is the replacement part.
It is used to match any one character in the set
insert its spaces for readability.
These are group operators used to memorize the match
and back reference in the replacement part.
Brackets and are escaped and to give them special meaning
as a group operator.
Slash question mark precedes when follows the group
operators, it is to match the space character
that may precede or follow any of the characters
in the set.
In the replacement part, the match string,
that is the combination of colon, a space optional,
a character from the set and again an optional space,
is replaced with the character match.
It uses a back reference to the character match
memorized by using the group operator.
Back referenced characters refer to a group match
using the slash one symbol.
The decompression command works as follows.
Replaces semi colons with semi colon slash n.
Replaces open curly brace with open curly brace
slash n slash n, replaces closed curly brace
with slash n slash n closed curly brace.
Awesome, we have successfully learned how to compress
and decompress JavaScript.
In the next video, we will learn how to merge
multiple files as columns.