a-toolbox
Javascript lightweight tools
"This is my rifle. There are many others like it, but this one is mine."
Npm Installation
$ npm install a-toolbox
Tools on Node.js
var tools = require('a-toolbox.js');
Tools on Browser
<script src="https://raw.githubusercontent.com/simone-sanfratello/a-toolbox/master/main.js"></script>
Tools
tasks (promises) async manage
var _tasks = new tools.tasks(function () {
console.log('well done');
});
var _asyncOperationTimeout = [ 500, 1000, 200, 1500, 100];
for(var i in _asyncOperationTimeout) {
_tasks.todo('task#' + i);
}
for(var i in _asyncOperationTimeout) {
setTimeout(function(i){
return function() {
console.log('done task #', i);
_tasks.done('task#' + i);
};
}(i), _asyncOperationTimeout[i]);
}
object merge
var _merge = {a: 1, b: 2};
console.log('to merge', _merge);
tools.object.merge(_merge, {a: 4, c: { d: 8, e: 9}});
console.log('merged', _merge);
random
console.log('random number from 1 to 100:', tools.random.number(1, 100));
console.log('random string of 8 chars, default set:', tools.random.string(8));
var _hex = '0123456789abcdef';
console.log('random string of 16 chars, custom set (hex)', _hex, ':', tools.random.string(16, _hex));
string template
Replace marker in template string with provided object data
var data = {
name: 'Alice',
year: 2014,
color: 'yellow'
};
var str = '<div>My name is {name} I was born in {year} and my favourite color is {color}</div>{nothing}';
console.log('template:', tools.string.template(str, data));
replaceAll in String prototype
console.log("no replace all in js native code that replace all the replace".replaceAll(' ', '_'));
array remove
var _array = ['very', 'annoying', 'remove', 'elements', 'in', 'js', 'arrays'];
tools.array.remove(_array, 'very');
tools.array.remove(_array, 'annoying');
console.log(_array);
array removeAt
tools.array.removeAt(_array, 4);
console.log(_array);
array first and last
console.log('last element is', tools.array.last(_array));
console.log('first element is', tools.array.first(_array));
array contains
console.log('contains js?', tools.array.contains(_array, 'js'));
console.log('contains ruby?', tools.array.contains(_array, 'ruby'));
array insert
tools.array.insert(_array, 0, 'something');
console.log('inserted something', _array);
array get random element
console.log('get random element:', tools.array.randomElement(_array));
array concat
console.log('concat more arrays', tools.array.concat(_array, [0,1,2,3], ['a','b','c']));
array empty
When you need to keep references
tools.array.empty(_array);
console.log('empty it', _array);
License
The MIT License (MIT)
Copyright (c) 2015 Simone Sanfratello
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.