Let's create a simple library to validate emails and IPv4 addresses. The name of this library will be xanthe.
️Xanthe means “golden” or “yellow” in Greek. Xanthe is an exotic epithet of Demeter, goddess of the harvest and agriculture 🇬🇷🧚♀
1{2 "name": "xanthe",3 "version": "0.0.1",4 "author": {5 "email": "contact@elitizon.com",6 "name": "elitizon"7 },8 "license": "MIT",9 "description": "A library to validate common formats such as emails, numbers, phone numbers, and IP addresses",10 "contributors": [11 "raphaelmansuy"12 ],13 "keywords": [14 "format",15 "validator",16 "email validation",17 "IP address validation"18 ],19 "dependencies": {},20 "devDependencies": {}21}
1yarn add -D typescript @types/node
@types/node
contains type definitions for Node.jstypescript
contains the typescript compiler
1mkdir src
1mkdir tests
Jest is a testing library developed by Facebook.
1yarn add -D jest ts-jest
1module.exports = {2 transform: {'^.+\\.ts?$': 'ts-jest'},3 testEnvironment: 'node',4 testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$',5 moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']6}
1"scripts": {2 "build": "tsc",3 "test": "yarn build && jest",4 "coverage": "jest --coverage"5 },
1{2 "compilerOptions": {3 "target": "es5" ,4 "module": "commonjs",5 "outDir": "./build",6 "strict": true,7 "esModuleInterop": true ,8 "skipLibCheck": true,9 "forceConsistentCasingInFileNames": true,10 "declaration": true11 },12 "exclude": ["node_modules", "build", "tests"]13}
The build command will generate the CommonJS files in the ./build/ directory
1/**2 * @param email to validate. No spaces or tab or is allowed at the start or at the end of the string3 * @returns true of false if the email is valid or not4 */5function isEmail(email: string) : boolean {6 // An email address is a word followed by an unique @ followed by a word then a . followed by a word with a length from 2 to 3 characters7 const regEx = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/8 const result = regEx.test(email)9 return result10}111213export {14 isEmail15}
1/**2 * @param ip to validate. (No spaces or tab or is allowed at the start or at the end of the string)3 * @returns true of false if the IP is valid or not4 */5function isIPv4(ip: string) : boolean {6 // An ip V4 address has the form of X.X.X.X7 // Where X is a number between 0 to 2558 const regEx = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/9 const result = regEx.test(ip)10 return result11}121314export {15 isIPv416}
1import { isEmail } from "./validators/email"2import { isIPv4 } from "./validators/ipv4"34export { isEmail, isIPv4 }
Our module is now nearly ready. Let's create the unit tests.
1import { isEmail } from "../src/index"23test("email luc@perdu.com valid", () => {4 expect(isEmail("luc@perdu.com")).toBe(true)5})67test("Empty string not valid", () => {8 expect(isEmail("")).toBe(false)9})1011test("No double @ in an email", () => {12 expect(isEmail("martin@toto@titi.com")).toBe(false)13})1415test("not trimed email to be false", () => {16 expect(isEmail(" luc@perdu.com ")).toBe(false)17})
1import { isIPv4 } from "../src"23test("192.168.0.1 is valid", () => {45 expect(isIPv4("192.168.0.1")).toBe(true)6})78test("1920.168.0.1 is not valid", () => {910 expect(isIPv4("1920.168.0.1")).toBe(false)11})1213test("192.1682.0.1 is not valid", () => {1415 expect(isIPv4("192.1682.0.1")).toBe(false)16})1718test("192.168.256.1 is not valid", () => {1920 expect(isIPv4("192.168.256.1")).toBe(false)21})2223test("192.168.255.500 is not valid", () => {2425 expect(isIPv4("192.168.255.500")).toBe(false)26})2728test("192.168.255.255 is valid", () => {29 expect(isIPv4("192.168.255.255")).toBe(true)30})3132test("192.168.X.255 is valid", () => {33 expect(isIPv4("192.168.X.255")).toBe(false)34})
1yarn build
1yarn test
1yarn coverage
A coverage has been generated with all the information about the tests coverage
1node_modules2build3coverage
1git init
1git *
1git commit -m "First commit"
An empty Github project must be created before publishing.
The file package.json need to be updated as follow:
1"repository": {2 "url": "https://github.com/myorganisation/xanthe.git",3 "type": ""4 },
myorganisation represents your Github account
We can know set the Github project as the remote representation of the local project, and push the local master branch to the remote server (origin).
1git remote add origin`https://github.com/myorganisation/xanthe.git2git branch -M master3git push -u origin master
An account must be created before publishing a package in the npm registry.
1npm login
Enter your username, password, and email address registered on npmjs.org
Add a new file .npmignore to exclude some files from the publication
1README.md2TUTORIAL.md3jest.config.json4tests/
1yarn publish
A few questions will be asked such as the new version number 0.0.1
And voila our component is published and visible 🥳
We deliver high quality blog posts written by professionals monthly. And we promise no spam.