I have developed a simple and easy to use JavaScript library called, Omega
for practice. I would highly appreciate if you can point out all sorts of flaws in the code such as violations of best practices, conventions and even would highly prefer optimization tips or other better ways of achieving what I did.
Here's the source code:
Omega.js
:
/**
* Library: Omega.js - Ω.js - A utility library for JavaScript programs. :)
*
* Author: Hassan Althaf
* Website: http://hassanalthaf.com
* License: MIT
*/
(function (global) {
var Omega = function () {
return new Omega.init();
};
Omega.init = function () {
};
Omega.init.prototype = {
map: function (originalArray, mappingFunction) {
var newArray = [];
if (arguments.length < 2) {
throw 'This function expects two parameters. The original array and a mapping function.';
} else {
for (var index in originalArray) {
newArray.push(mappingFunction(originalArray[index]));
}
}
return newArray;
},
filter: function (originalArray, filteringFunction) {
var filteredArray = [];
if (arguments.length < 2) {
throw 'This function expects two parameters. The original array and a filtering function.';
} else {
for (var index in originalArray) {
if (filteringFunction(originalArray[index]) === true) {
filteredArray.push(originalArray[index]);
}
}
return filteredArray;
}
},
implode: function (array, divider) {
var implodedString = null;
if (arguments.length < 2) {
throw 'This function expects two parameters. The first to be an array, and second a string divider.';
} else {
for (index in array) {
implodedString = implodedString + array[index] + divider;
}
implodedString = implodedString.substring(0, implodedString.length - divider.length);
return implodedString;
}
},
explode: function (string, needle) {
if (arguments.length < 2) {
throw 'This function expects two parameters. A string and a needle.';
} else {
return string.split(needle);
}
},
mergeArrays: function (topArray, bottomArray) {
if (arguments.length < 2) {
throw 'This function expects two parameters. A top array and a bottom.';
} else {
for (index in bottomArray) {
topArray.push(bottomArray[index]);
}
return topArray;
}
},
eliminateDuplicates: function (array) {
if (arguments.length < 1) {
throw 'This function expects one parameter. An array where duplicates need to be eliminated.';
} else {
array = this.recursivelyEliminateDuplicates(array);
return array;
}
},
recursivelyEliminateDuplicates: function(array) {
for (mainLoopIndex in array) {
console.log(array);
var currentItem = array[mainLoopIndex];
for (index in array) {
if ((index !== mainLoopIndex) && (currentItem === array[index])) {
array.splice(index, 1);
this.recursivelyEliminateDuplicates(array);
}
}
}
return array;
}
};
window.Ω = window.Omega = Omega;
}(window));
index.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Omega Workspace</title>
</head>
<body>
<script type="text/javascript" src="Omega.js"></script>
<script type="text/javascript" src="app.js"></script>
</body>
</html>
app.js
:
var Ω = Ω(); // or Omega();
var numbers = [ 1, 2, 3 ];
var newNumbers = Ω.map( numbers, function(number) {
return (number * 2);
});
var filteredNumbers = Ω.filter( numbers, function(number) {
return (number % 2) === 0;
});
var implodeData = Ω.implode( numbers, '-' );
var explodeData = Ω.explode( '192.168.1.1', '.' );
var mergedArrays = Ω.mergeArrays( numbers, [ 4, 5, 6 ] );
var duplicatesEliminated = Ω.eliminateDuplicates( [1, 1, 1, 2, 2, 3, 4, 5, 9, 1, 3, 5, 9, 1, 5, 9, 8, 18, 20] );
console.log(newNumbers);
console.log(filteredNumbers);
console.log(implodeData);
console.log(explodeData);
console.log(mergedArrays);
console.log(duplicatesEliminated);
new Omega.init()
is horrifying and also why doing anew
if you never usethis
, it could be done way simpler, (let alone all those util functions can be done with native functions in one line) \$\endgroup\$this
and I did.init()
getting inspired byjQuery.
\$\endgroup\$this
is quite more justified/useful when you maintain a state (with variables attributes), well sorry for my opinion, and I also think there are much better things than jquery for inspirations \$\endgroup\$