JavaScript library compatible with RequireJs, jQuery and Node.js
Last night, I was updating is_email JavaScript library so that it supports jQuery, RequireJS and Node.js. I found information for each library but on different blogs. I didn’t find complete set information at one place. As each of my blog says, I would be putting it here in the hope that it would be accessible all at one place.
I would explain with demo example. First wrap whole library in a function, so that even though my library has thousand functions, only the one I needed would go in global scope. In below example I’m creating a library function which checks if variable is undefined.
[javascript]
(function () {
function is_undefined(variable) {
return typeof(variable) === undefined;
}
})();
[/javascript]
Now I have wrote my function, now I would want it to be enabled or compatible with RequireJS. Check if define is a function and it is a AMD loader then define our function. That’s it. Checkout below example.
[javascript]
(function () {
function is_undefined(variable) {
return typeof(variable) === undefined;
}
// Enable for Require.js.
if (typeof(define) === ‘function’ && define.amd) {
define(function() { return is_undefined; });
}
})();
[/javascript]
You want that your library can also be used as Node module. There are few more steps to create Node module. Checkout tutorial for how Node.js module is created. In below I would just show to expose your lib/function to Node as module.
[javascript]
(function () {
function is_undefined(variable) {
return typeof(variable) === undefined;
}
// Enable for Node.js or CommonJS.
if (typeof(module) !== ‘undefined’ && module.exports && typeof(exports) !== ‘undefined’) {
exports.is_undefined= is_undefined;
}
})();
[/javascript]
Check if “module” is defined and “exports” is defined, if yes then we assume it is Node whose using/calling a JS file or JS funcion.
Last, how to enable support for jQuery. There are many ways to do expose/add your function to jQuery. I would demo two methods.
[javascript]
(function () {
function is_undefined(variable) {
return typeof(variable) === undefined;
}
// Enable is_email as jQuery plugin.
if (typeof(jQuery) !== ‘undefined’) {
// If you want to use it in a style “$.is_undefined(x);”.
jQuery.is_undefined = is_undefined;
// If you want to use it in a style “$(‘selector’).is_undefined(x);”.
jQuery.fn.is_undefined = is_undefined;
// You would get “selector” inside is_undefined using “this” object.
}
})();
[/javascript]
If you are looking for complete tutorial for jQuery plugin, then you are on wrong blog as I’m just demoing how to enable jQuery support. Please go to for writing your own jquery plugin.
We can combine all these above 3 examples into one and enable support /compatibility for RequireJS, jQuery and Node. One of the working example is is_email library which validates your email against relevant RFCs.
If I’m missing some information or you have any suggestions, doubts or questions do write your comments/thoughts on the blog..