Hello World š
In this article, we are going to build, test, and publish an npm package to check whether a string is an email address or not. So if you want to know how we will do it, put on your seat belt, and letās go š
Prerequisites :
- Have Nodejs installed on your machine (If not, you can download itĀ here).
- Have an npm account, if not, you can create one at npmjs.com.
- And a little bit of JavaScript knowledge.
š ļø Build
First, we need a directory to hold our code, so create one and open it with your terminal, then initialize a new Nodejs project by running the command :
npm init -y
this command will generate aĀ package.jsonĀ file with default values, which we will edit later.
Next, inside our directory, letās create a new directory and name itĀ src,Ā then insideĀ src,Ā letās create anĀ index.jsĀ file that will contain our code.
Now that our root directory has the following file structure:
.
āāsrc
ā ā index.js
ā package.json
letās get inside the src/index.js file and export a function that takes a string as an argument and checks whether itās an email address. And to do so, we will be usingĀ Regular Expressions(regex)
Ā and test if the given string matches the regex pattern of an email address:
// ./src/index.js
const isEmailAddress = (string) => {
const regex =
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return regex.test(String(string).toLowerCase());
};
module.exports = isEmailAddress;
Great! Now that we have created our package, letās move on to the next step.
šØ Test
To test our package, we will be using Jest as our testing framework. and to do so letās first install Jest as a development dependency using the command :
npm install ā-save-dev jest
then create a new directory inside our root directory and name itĀ test, and inside it create anĀ index.test.jsĀ file which will contain our tests.
Now the file structure of our root directory will be the following :
.
āāsrc
ā ā index.js
āātest
ā ā index.test.js
ā package.json
inside test/index.test.js, letās import the function we exported from src/index.js and write our tests :
// ./test/index.test.js
const isEmail = require("../src");
describe("isEmail", () => {
it("should return false if the email is not valid", () => {
expect(isEmail("123")).toBe(false);
expect(isEmail("email")).toBe(false);
expect(isEmail("example.com")).toBe(false);
expect(isEmail("email@example")).toBe(false);
});
it("should return true if email is valid", () => {
expect(isEmail("email@example.com")).toBe(true);
expect(isEmail("email123@example.com")).toBe(true);
expect(isEmail("email-12@example.com")).toBe(true);
expect(isEmail("email+az@example.com")).toBe(true);
});
});
To run our tests, letās go to the package.json file and edit the test script to use jest:
- "test": "echo \\"Error: no test specified\\" && exit 1ā
+ "test": "jest"
then run the following command:
npm run test
and you should see our tests passed š
š Publish
To publish our package, we will need to make a few changes to the package.json file and we will start with the package name, I will simply name it is-email-address, but before you name your package, you first need to make sure that your package name is unique and doesn't exist in the npm registry otherwise you will get an error when you try to publish it.
I will add a little description as well and set the entry point of our package to src/index.js and I will add some keywords to help people discover our package as it's listed in npm search
(You can learn more about the specifics of npm's package.json handling from here).
Now the package.json file looks like this:
{
"name": "is-email-address",
"version": "1.0.0",
"description": "check whether a string is an email address",
"main": "src/index.js",
"scripts": {
"test": "jest"
},
"files": [
"src"
],
"keywords": [
"email",
"is-email-address",
"is-email"
],
"author": "Omar Baabouj <baabouj.dev@gmail.com>",
"license": "ISC",
"devDependencies": {
"jest": "^28.0.3"
}
}
Back to the terminal, letās first login to npm by running the command:
npm login
youāll be asked to enter your username, password, and email. Once you add them, you are successfully signed in!
Now to the last step, letās run the command to publish our package:
npm publish
And VoilĆ ! You just published an npm package š
If you have any thoughts or have noticed any mistakes please message me @baabouj_ on Twitter.
You can find the source code of this article in this repo, or check it out on npm
Top comments (0)