Home Java ImEx.jswill decorate your code

ImEx.jswill decorate your code

by admin

Have you written large projects using JS? Did you have a lot of objects in object objects (e.g. LIB.Module.object.param)? I had this a lot and it caused quite a big inconvenience and bad code look. Similarly, I often had to use anonymous functions, they don’t look particularly neat.

ImEx.js

Wanted some very simple and neat solution to the problem. It just so happens that the project I’m working on has a module in node.js that pulls together all the .js files into one build.js. And here it occurred to me, why not add some preprocessor that modifies the input code, thus allowing to do non-standard functions (lightweight CoffeeScript or TypeScript). This is how the little preprocessor ImEx.js came about

Target

Avoid long constructions

Suppose we have three entities World, World.Animal, World.animals.Dog, let’s imagine what it would look like in pure JS:

// World.jsvar World = {animals: {}};

// Animal.jsWorld.Animal = function ( name, legNum ) {this.name = name;this.legNum = legNum;};

// Dog.jsWorld.animals.Dog = function () {this.barking = false;};World.animals.Dog.prototype = Object.create( World.Animal );World.animals.Dog.prototype.bark = function () {this.barking = true;};World.animals.Dog.prototype.stopBark = function () {this.barking = false;};

If you use ImEx.js the code will look like this :

// World.jsnamespace Global;export World = {animals: {}};

// Animal.jsnamespace World;export Animal = function ( name, legNum ) {this.name = name;this.legNum = legNum;};

// Dog.jsnamespace World.animals;import World.Animal;export Dog = function () {this.barking = false;};Dog.prototype = Object.create( Animal );Dog.prototype.bark = function () {this.barking = true;};Dog.prototype.stopBark = function () {this.barking = false;};

Three directives ‘namespace’, ‘import’, ‘export’ have been added. It’s so simple that I don’t think it needs any explanation.

Make modularity more explicit

This is achieved by using the import, export directives.

Completely avoid anonymous functions, but have their benefits

If you get into the implementation, every module is wrapped in an anonymous function and all variables which are not prefixed with import or export don’t become global. This is exactly what we want to achieve.

To finish

I think something like this already exists and if someone could point a finger for comparison I would be grateful.
The code is open here
You can easily install : npm install imex -g
A more complete installation and startup guide with examples is at readme

You may also like