Part II: How to create a node module
Let’s learn how to create a small and simple node module with some ES2015 features and more. This is part 2 where we code all parts to make it possible to make the pizza. If you missed part 1 where I cover an introduction and how to set up the project, check here:
Choosing pizzas sizes and toppings
To get started we will need pizzas sizes and toppings available. So let's create them. It's a simple json file where it contains the data we want.
To make really simple I just added 2 toppings on my /helpers/toppings.json
file. You can add any topping you want.
And for sizes I added only 3 on /helpers/sizes.json
.
Checking if sizes and toppings are available
Now that we have some data, our next step is to check if the sizes and toppings the customer is asking are available.
Checking sizes
Let's check the sizes first on /lib/is-size.js
.
What are we doing here? On line 3 we a requiring the sizes.json
file that contains the sizes array.
Then, on line 5 we are exporting a function where it receives a parameter size
. So we can check if the sizes.json
array includes the parameter received and return a boolean value.
If don't know about includes, it basically determines whether an array includes a certain element or not and return a boolean value.
Checking toppings
On toppings, I used a different approach to show the differences between indexOf
and includes
. Although both of them returns the same result, indexOf
is a faster solution in this case.
I'm adding a link on the footnotes if you want to learn further the differences.
I did almost the same way I did with is-size.js
but this time using indexOf
. indexOf
will return the index of the string that we are searching for. If it does not find, it will return -1
. That's why we check if indexOf
of the parameter received is higher than -1. This function will also return a boolean value.
Filtering toppings
The customer may ask for a topping that it's not available, so we need to filter to not include those toppings.
On /lib/check-toppings.js
we receive an array
of toppings which is the toppings the customer wants. We filter the toppings and test if isTopping
.
isTopping
is the function we just created to check if the topping is available or not.
The filter method will create a new array with all elements that pass the test implemented.
Heating the pizza takes some time
We will fake that it takes some time to heat the pizza. In order to do that, let's create /lib/sleep.js
and /lib/heat-pizza.js
.
On sleep.js
we return a Promise that will resolve after some time.
On /lib/heat-pizza.js
we have to wait the sleep.js
Promise to be resolved.
We can wait for the Promise to be resolved in two different ways. With .then()
or with await
. We will await because we are cool!
The await
operator is used to wait for a Promise and it can only be used inside an async
function.
What's really happening on this function is that the function receives a timer
parameter and waits to the sleep
function to be resolved after (1000 * timer) ms
and then returns true
.
Finally, making the pizza! 🍕🎉
Now that we have all parts to make a pizza, let's make a pizza!
First thing first, we need to require all needed parts to this files. That's what I did from line 3 to 7.
Since we have to wait to heat the pizza, let's make it an async
function. This function accepts an object as a parameter that has size
and topping
.
I'm destructuring the object and using default parameters in case the customer doesn't choose a size.
Destructuring
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
Default parameters
Default function parameters allow formal parameters to be initialized with default values if no value or undefined
is passed.
It's the old var size = size || 'small'
.
Check if sizes and toppings are available
On line 12 to 20, we will use the is-size
and check-toppings
functions to check the sizes and toppings the customer wants. If it fails, we throw an error.
If there are no errors, we will heat the pizza. Life's is good! Since heatPizza
is waiting for a Promise, we have to wait to be resolved and then continue.
All good, pizza is hot! 🍕🔥
Return a message to the customer.
On line 24 we use a template literal
that is basically a string
that allows embedding expressions
. We just need to enclose it with a back-tick (``)
.