DEV Community

Cover image for ToolJS: Exporting Module Functions

ToolJS: Exporting Module Functions

redeakaa profile image Okoro Redemption ・2 min read

Export ToolJS Modules.

We looked at the ToolJS.config method the last time. Today we'll checkout the ToolJS.export method which you might have noticed the above method in the previous posts.

Though available in all working environment, this method was created mainly for users who reference the library through its CDN or locally. It gives the users the ability to export or bind the modules they want to any variable of their choice for flexibility.

In popular libraries like Lodash and JQuery when being used in browser or through their CDN, the methods are bound to "_" and "$" respectively. This feature also helps to reduce pollution of the window object with so many namespace.

ToolJS, as of version 1.0.1 and as the time of this post comes with six(6) in-built modules, namely "DOM", "Obj", "Str", "Num", "Calc" and "Utils", each with a handful of methods.

To export any of this modules, pass the module name to the ToolJS.export method as a string, and for multiple exports, pass it an array of strings.

// The export function accepts either a single string or an array of strings representing a registered module
var $ = ToolJS.export("Str");
// the above code exports all methods of the "Str" module to the variable "$" and can now be used as shown below

var myString = $.joinBy("_", "Hello", "World");
// => Hello_World
Enter fullscreen mode Exit fullscreen mode

Say you want to export both the "Str" and "Num" modules into a variable, just pass an array to the method

// The code below will export all the methods in both the "Str" and "Num" module to the variable "_"
var _ = ToolJS.export(["Str", "Num"]);

// the code below checks if a number is odd
// => false

// the code below slugifies a given string
var post_title = _.slugify("How to use ToolJS library");
// => "how-to-use-toolJS-library"
Enter fullscreen mode Exit fullscreen mode

The ToolJS.export method exports all modules in the library by default if no parameter was passed to it.

// this exports all the methods in all the modules of the library
var All = ToolJS.export();
Enter fullscreen mode Exit fullscreen mode

See the Pen ToolJS: The Export Function by Redemption Okoro
(@akaawereniya) on CodePen.


The ToolJS.export method exports a registered module's methods to a variable for use. This helps to avoid polluting the window object.

Discussion (0)

Editor guide