A string validator

We are going to develop a npm module which is used to validate string.

First of all, build the folder structure of a package:

  1. node-validator
  2. |- lib/
  3. |- test/
  4. |-package.json
  5. |- index.js
  6. |- README.md

As the project is quite simple, it’s fine to put all the codes in index.js.

However, we can place all the implement codes in the lib folder for extending the project later and regard the index.js file as an entrance of the moudle.

  1. module.exports = require('./lib');

Creating a index.js file in the lib in order to write the core of our moudle:

  1. module.exports =function(){
  2. console.log('Hello npm!');
  3. }

The program above is the npm version of the well-known “Hello World”. There’s no problem to use node index.js to have a test of it.

In the module system of CommonJS, module.exports can output a function or an object. So we can easily write codes like this:

  1. module.exports ={
  2. isEmail:function(){},
  3. isAllEnglish:function(){}
  4. };

After that, we can continue programming the module using regular expressions:

  1. module.exports ={
  2. isEmail:function(str){
  3. return/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(str);
  4. },
  5. isAllEnglish:function(str){
  6. return/^[a-zA-Z]+$/.test(str);
  7. }
  8. }

Up to now, the first alpha version of node-validator is ready for use.

Using npm link command could place the current module to the system’s npm module folders. In the other words, if we modify the codes in our project, we could feel that when using the module.

Assuming we have changed the name to validator-test in the package.json file, then the code below can work as expected:

  1. var validator = require('validator-test');
  2. validator.isEmail('foo@bar.net');// true

There’s no doubt that we can add more functions to the object showing above. And we can also split different functions into different files and combine them into one module using the module system of Node.js.