Comparing version 0.0.10 to 0.0.11
452
main.js
@@ -1,225 +0,259 @@ | ||
String.prototype.replaceAll = function (from, to) { | ||
return this.split(from).join(to); | ||
}; | ||
String.prototype.capitalize = function () { | ||
return this.substr(0,1).toUpperCase() + this.substr(1).toLowerCase(); | ||
}; | ||
var tools = { | ||
/** | ||
* array utils, inspired to goog.array | ||
*/ | ||
array: { | ||
/** | ||
* array utils, inspired to goog.array | ||
* remove an element from array | ||
* @param {Array} array | ||
* @param {*} item | ||
*/ | ||
array: { | ||
/** | ||
* remove an element from array | ||
* @param {Array} array | ||
* @param {*} item | ||
*/ | ||
remove: function (array, item) { | ||
var _index = array.indexOf(item); | ||
if (_index != -1) | ||
array.splice(_index, 1); | ||
}, | ||
/** | ||
* remove an element from array at position | ||
* @param {Array} array | ||
* @param {number} index | ||
*/ | ||
removeAt: function (array, index) { | ||
return Array.prototype.splice.call(array, index, 1).length == 1; | ||
}, | ||
/** | ||
* get last element of array or null | ||
* @param {Array} array | ||
* @returns {*} last element of the array or null | ||
*/ | ||
last: function (array) { | ||
return array[array.length - 1] || null; | ||
}, | ||
/** | ||
* get first element of array or null | ||
* @param {Array} array | ||
* @returns {*} last element of the array or null | ||
*/ | ||
first: function (array) { | ||
return array[0]; | ||
}, | ||
/** | ||
* check if array contains an element | ||
* @param {Array} array | ||
* @param {*} item | ||
* @returns {Boolean} | ||
*/ | ||
contains: function (array, item) { | ||
return array.indexOf(item) != -1; | ||
}, | ||
/** | ||
* insert an item into array at index position | ||
* @param {Array} array | ||
* @param {number} index | ||
* @param {*} item | ||
*/ | ||
insert: function (array, index, item) { | ||
if(index > array.length) | ||
index = array.length; | ||
if (array[index]) | ||
array.splice(index, 0, item); | ||
else | ||
array[index] = item; | ||
}, | ||
/** | ||
* get random element from array | ||
* @param {Array} array | ||
* @param {*} not | ||
* @returns {*} element | ||
*/ | ||
randomElement: function (array, not) { | ||
if (!not) | ||
return array[tools.random.number(0, array.length - 1)]; | ||
else { | ||
var _item, i = 0; | ||
do { | ||
_item = randomElement(array); | ||
} while (not.indexOf(_item) != -1 && ++i < array.length); | ||
return _item; | ||
} | ||
}, | ||
/** | ||
* concat arrays | ||
* @param {...Array} arrays to chain | ||
* @returns {Array} chained arrays | ||
* @example tools.array.concat([0,1,2],[3,4,5]) > [0,1,2,3,4,5] | ||
*/ | ||
concat: function (args) { | ||
return Array.prototype.concat.apply(Array.prototype, arguments); | ||
}, | ||
/** | ||
* empty - need to not break references | ||
*/ | ||
empty: function (array) { | ||
while(array[0]) | ||
array.pop(); | ||
} | ||
remove: function (array, item) { | ||
var _index = array.indexOf(item) | ||
if (_index !== -1) array.splice(_index, 1) | ||
}, | ||
/** | ||
* random utils | ||
* remove an element from array at position | ||
* @param {Array} array | ||
* @param {number} index | ||
*/ | ||
random: { | ||
/** | ||
* get random int from 0 to val | ||
* @param {number} val max item | ||
* @returns {number} | ||
*/ | ||
rnd: function (val) { | ||
if (!val) | ||
return 0; | ||
return Math.floor(val * (Math.random() % 1)); | ||
}, | ||
/** | ||
* get random int from min to max | ||
* @param {number} min | ||
* @param {number} max | ||
* @returns {number} | ||
*/ | ||
number: function (min, max) { | ||
if (!max) | ||
return tools.random.rnd(min); | ||
removeAt: function (array, index) { | ||
return Array.prototype.splice.call(array, index, 1).length === 1 | ||
}, | ||
/** | ||
* get last element of array or null | ||
* @param {Array} array | ||
* @returns {*} last element of the array or null | ||
*/ | ||
last: function (array) { | ||
return array[array.length - 1] || null | ||
}, | ||
/** | ||
* get first element of array or null | ||
* @param {Array} array | ||
* @returns {*} last element of the array or null | ||
*/ | ||
first: function (array) { | ||
return array[0] | ||
}, | ||
/** | ||
* check if array contains an element | ||
* @param {Array} array | ||
* @param {*} item | ||
* @returns {Boolean} | ||
*/ | ||
contains: function (array, item) { | ||
return array.indexOf(item) !== -1 | ||
}, | ||
/** | ||
* insert an item into array at index position | ||
* @param {Array} array | ||
* @param {number} index | ||
* @param {*} item | ||
*/ | ||
insert: function (array, index, item) { | ||
if (index > array.length) { | ||
index = array.length | ||
} | ||
min = Math.floor(min); | ||
max = Math.floor(max); | ||
return min + tools.random.rnd(1 + max - min); | ||
}, | ||
/** | ||
* get random string | ||
* @param {number} [length=8] | ||
* @param {Array} [set=qwertyuiopasdfghjklzxcvbnm] | ||
* @returns {String} | ||
*/ | ||
string: function (length, set) { | ||
if(!length) | ||
lenght = 8; | ||
if(!set) | ||
set = 'qwertyuiopasdfghjklzxcvbnm'; | ||
var _str = ''; | ||
for(var i = 0; i < length; i++) | ||
_str += tools.array.randomElement(set); | ||
return _str; | ||
} | ||
if (array[index]) { | ||
array.splice(index, 0, item) | ||
} else { | ||
array[index] = item | ||
} | ||
}, | ||
object: { | ||
/** | ||
* merge obj2 into obj1 | ||
* @param {object} obj1 | ||
* @param {object} obj2 | ||
*/ | ||
merge: function(obj1, obj2) { | ||
for(var i in obj2) { | ||
if(typeof obj2[i] == 'object') { | ||
!obj1[i] && (obj1[i] = {}); | ||
tools.object.merge(obj1[i], obj2[i]); | ||
} else | ||
obj1[i] = obj2[i]; | ||
} | ||
/** | ||
* get random element from array | ||
* @param {Array} array | ||
* @param {*} not | ||
* @returns {*} element | ||
*/ | ||
randomElement: function (array, not) { | ||
if (!not) { | ||
return array[tools.random.number(0, array.length - 1)] | ||
} else { | ||
var _item | ||
var i = 0 | ||
do { | ||
_item = tools.array.randomElement(array) | ||
} while (not.indexOf(_item) !== -1 && ++i < array.length) | ||
return _item | ||
} | ||
}, | ||
/** | ||
* concat arrays | ||
* @param {...Array} arrays to chain | ||
* @returns {Array} chained arrays | ||
* @example tools.array.concat([0,1,2],[3,4,5]) > [0,1,2,3,4,5] | ||
*/ | ||
concat: function (args) { | ||
return Array.prototype.concat.apply(Array.prototype, arguments) | ||
}, | ||
/** | ||
* empty - need to not break references | ||
*/ | ||
empty: function (array) { | ||
while (array[0]) array.pop() | ||
} | ||
}, | ||
/** | ||
* random utils | ||
*/ | ||
random: { | ||
/** | ||
* get random int from 0 to val | ||
* @param {number} val max item | ||
* @returns {number} | ||
*/ | ||
rnd: function (val) { | ||
if (!val) return 0 | ||
return Math.floor(val * (Math.random() % 1)) | ||
}, | ||
/** | ||
* get random int from min to max | ||
* @param {number} min | ||
* @param {number} max | ||
* @returns {number} | ||
*/ | ||
number: function (min, max) { | ||
if (!max) return tools.random.rnd(min) | ||
min = Math.floor(min) | ||
max = Math.floor(max) | ||
return min + tools.random.rnd(1 + max - min) | ||
}, | ||
/** | ||
* get random string | ||
* @param {number} [length=8] | ||
* @param {Array} [set=qwertyuiopasdfghjklzxcvbnm] | ||
* @returns {String} | ||
*/ | ||
string: function (length, set) { | ||
if (!length) length = 8 | ||
if (!set) set = 'qwertyuiopasdfghjklzxcvbnm' | ||
var _str = '' | ||
for (var i = 0; i < length; i++) { | ||
_str += tools.array.randomElement(set) | ||
} | ||
return _str | ||
} | ||
}, | ||
object: { | ||
/** | ||
* merge obj2 into obj1 | ||
* @param {object} obj1 | ||
* @param {object} obj2 | ||
*/ | ||
merge: function (obj1, obj2) { | ||
for (var i in obj2) { | ||
if (typeof obj2[i] === 'object') { | ||
!obj1[i] && (obj1[i] = {}) | ||
tools.object.merge(obj1[i], obj2[i]) | ||
} else { | ||
obj1[i] = obj2[i] | ||
} | ||
} | ||
}, | ||
/** | ||
* tasks (promises) async manage | ||
* @param {function} done callback when all tasks are completed | ||
* @see http://google.github.io/closure-library/api/source/closure/goog/object/object.js.src.html#l225 | ||
* @param {object} obj | ||
* @returns {Array} | ||
*/ | ||
tasks: function (done) { | ||
var __tasks = []; | ||
return { | ||
/** | ||
* schedule what to do | ||
* @param {string} id | ||
*/ | ||
todo: function (id) { | ||
__tasks.push(id); | ||
}, | ||
/** | ||
* declare it's done | ||
* @param {string} id | ||
*/ | ||
done: function (id) { | ||
tools.array.remove(__tasks, id); | ||
1 > __tasks.length && (done && done()); | ||
} | ||
}; | ||
getKeys: function (obj) { | ||
var _keys = [] | ||
for (var key in obj) { | ||
_keys.push(key) | ||
} | ||
return _keys | ||
}, | ||
string: { | ||
/** | ||
* replace placeholders inside graph brackets {} with obj dictionary | ||
* ~ES6 template string, but safer | ||
* @param {string} str | ||
* @param {type} obj | ||
* @param {bool} [remove=false] remove missing placeholders from obj | ||
* @returns {unresolved} | ||
*/ | ||
template: function (str, obj, remove) { | ||
return str.replace(/\{([\w]+)\}/g, function (str, key) { | ||
return obj[key] ? obj[key] : (remove ? '' : str); | ||
}); | ||
}, | ||
/** | ||
* trim string | ||
* @see http://google.github.io/closure-library/api/namespace_goog_string.html | ||
* @param {string} str | ||
* @param {?string[]} cuts | ||
* @returns {string} | ||
*/ | ||
trim: function(str, cuts) { | ||
if(!cuts) | ||
return str.replace(/^[\s\xa0]+|[\s\xa0]+$/g, ''); | ||
else { | ||
var _cuts = cuts.join(); | ||
return str.replace(new RegExp('^[' + _cuts + ']+|[' + _cuts + ']+$', 'gm'), ''); | ||
} | ||
/** | ||
* @param {object} obj | ||
* @returns {object} | ||
*/ | ||
sortKeys: function (obj) { | ||
var _keys = tools.object.getKeys(obj) | ||
_keys.sort() | ||
var _obj = {} | ||
for(var i = 0; i < _keys.length; i++) | ||
_obj[_keys[i]] = obj[_keys[i]] | ||
return _obj | ||
} | ||
}, | ||
/** | ||
* async parallel task manager | ||
* @constructor | ||
* @param {function} done callback when all tasks are completed | ||
*/ | ||
Tasks: function (done) { | ||
var __tasks = [] | ||
return { | ||
/** | ||
* schedule what to do | ||
* @param {string} id | ||
*/ | ||
todo: function (id) { | ||
__tasks.push(id) | ||
}, | ||
/** | ||
* declare it's done | ||
* @param {string} id | ||
*/ | ||
done: function (id) { | ||
tools.array.remove(__tasks, id) | ||
if(__tasks.length < 1) { | ||
done && done() | ||
} | ||
} | ||
} | ||
}; | ||
}, | ||
string: { | ||
/** | ||
* replace placeholders inside graph brackets {} with obj dictionary | ||
* ~ES6 template string, but safer | ||
* @param {string} str | ||
* @param {type} obj | ||
* @param {bool} [remove=false] remove missing placeholders from obj | ||
* @returns {unresolved} | ||
*/ | ||
template: function (str, obj, remove) { | ||
return str.replace(/\{([\w]+)\}/g, function (str, key) { | ||
return obj[key] ? obj[key] : (remove ? '' : str) | ||
}) | ||
}, | ||
/** | ||
* trim string | ||
* @see http://google.github.io/closure-library/api/namespace_goog_string.html | ||
* @param {string} str | ||
* @param {?string[]} cuts | ||
* @returns {string} | ||
*/ | ||
trim: function (str, cuts) { | ||
if (!cuts) { | ||
return str.replace(/^[\s\xa0]+|[\s\xa0]+$/g, '') | ||
} else { | ||
var _cuts = cuts.join() | ||
return str.replace(new RegExp('^[' + _cuts + ']+|[' + _cuts + ']+$', 'gm'), '') | ||
} | ||
}, | ||
replaceAll: function (str, from, to) { | ||
return str.split(from).join(to) | ||
}, | ||
capitalize: function (str) { | ||
return str.substr(0, 1).toUpperCase() + str.substr(1).toLowerCase() | ||
} | ||
} | ||
} | ||
if (typeof module != 'undefined' && module.exports) | ||
module.exports = tools; | ||
// compatibilty < 0.0.10 - to remove | ||
tools.tasks = tools.Tasks | ||
String.prototype.replaceAll = function (from, to) { | ||
return tools.string.replaceAll(this, from, to) | ||
} | ||
String.prototype.capitalize = function () { | ||
return tools.string.capitalize(this) | ||
} | ||
if (typeof module !== 'undefined' && module.exports) { | ||
module.exports = tools | ||
} |
{ | ||
"name": "a-toolbox", | ||
"version": "0.0.10", | ||
"description": "lightweight tools", | ||
"keywords": ["tools", "lib", "misc", "toolbox", "array", "task", "random"], | ||
"url": "http://github.com/simone-sanfratello/a-toolbox", | ||
"author": "Simone Sanfratello <simone.sanfra@gmail.com>", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/simone-sanfratello/a-toolbox.git" | ||
}, | ||
"engines": { | ||
"node": ">= 0.10.x" | ||
}, | ||
"scripts": { | ||
"test": "istanbul cover test.js -x test.js" | ||
}, | ||
"tonicExampleFilename": "test.js", | ||
"files": ["main.js", "test.js"], | ||
"main": "main" | ||
} | ||
"name": "a-toolbox", | ||
"version": "0.0.11", | ||
"description": "lightweight tools", | ||
"keywords": [ | ||
"tools", | ||
"lib", | ||
"misc", | ||
"toolbox", | ||
"array", | ||
"task", | ||
"random" | ||
], | ||
"url": "http://github.com/simone-sanfratello/a-toolbox", | ||
"author": "Simone Sanfratello <simone.sanfra@gmail.com>", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/simone-sanfratello/a-toolbox.git" | ||
}, | ||
"engines": { | ||
"node": ">= 0.10.x" | ||
}, | ||
"scripts": { | ||
"test": "istanbul cover test.js -x test.js" | ||
}, | ||
"tonicExampleFilename": "test.js", | ||
"files": [ | ||
"main.js", | ||
"test.js" | ||
], | ||
"main": "main" | ||
} |
@@ -5,2 +5,3 @@ # a-toolbox | ||
[![JS Standard Style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/) | ||
[![Code Climate](https://codeclimate.com/github/simone-sanfratello/a-toolbox/badges/gpa.svg)](https://codeclimate.com/github/simone-sanfratello/a-toolbox) | ||
@@ -7,0 +8,0 @@ [![Test Coverage](https://codeclimate.com/github/simone-sanfratello/a-toolbox/badges/coverage.svg)](https://codeclimate.com/github/simone-sanfratello/a-toolbox/coverage) |
166
test.js
@@ -1,126 +0,124 @@ | ||
//var tools = require('./main'); | ||
var tools = require('a-toolbox'); | ||
// var tools = require('./main') | ||
var tools = require('a-toolbox') | ||
// add replaceAll in String prototype | ||
console.log("no replace all in js native code that replace all the replace".replaceAll(' ', '_')); | ||
console.log('no replace all in js native code that replace all the replace'.replaceAll(' ', '_')) | ||
//> no_replace_all_in_js_native_code_that_replace_all_the_replace | ||
// > no_replace_all_in_js_native_code_that_replace_all_the_replace | ||
var _array = ['very', 'annoying', 'remove', 'elements', 'in', 'js', 'arrays']; | ||
tools.array.remove(_array, 'very'); | ||
tools.array.remove(_array, 'annoying'); | ||
console.log(_array); | ||
var _array = ['very', 'annoying', 'remove', 'elements', 'in', 'js', 'arrays'] | ||
tools.array.remove(_array, 'very') | ||
tools.array.remove(_array, 'annoying') | ||
console.log(_array) | ||
//>[ 'remove', 'elements', 'in', 'js', 'arrays' ] | ||
// >[ 'remove', 'elements', 'in', 'js', 'arrays' ] | ||
tools.array.removeAt(_array, 4); | ||
console.log(_array); | ||
tools.array.removeAt(_array, 4) | ||
console.log(_array) | ||
//>[ 'remove', 'elements', 'in', 'arrays' ] | ||
// >[ 'remove', 'elements', 'in', 'arrays' ] | ||
console.log('last element is', tools.array.last(_array)); | ||
console.log('last element is', tools.array.last(_array)) | ||
//>last element is js | ||
// >last element is js | ||
console.log('first element is', tools.array.first(_array)); | ||
console.log('first element is', tools.array.first(_array)) | ||
//>first element is remove | ||
// >first element is remove | ||
console.log('contains js?', tools.array.contains(_array, 'js')); | ||
console.log('contains js?', tools.array.contains(_array, 'js')) | ||
//>contains js? true | ||
// >contains js? true | ||
console.log('contains ruby?', tools.array.contains(_array, 'ruby')); | ||
console.log('contains ruby?', tools.array.contains(_array, 'ruby')) | ||
//>contains ruby? false | ||
// >contains ruby? false | ||
tools.array.insert(_array, 0, 'something'); | ||
console.log('inserted something', _array); | ||
tools.array.insert(_array, 0, 'something') | ||
console.log('inserted something', _array) | ||
//>inserted something [ 'something', 'remove', 'elements', 'in', 'js' ] | ||
// >inserted something [ 'something', 'remove', 'elements', 'in', 'js' ] | ||
console.log('get random element:', tools.array.randomElement(_array)); | ||
console.log('get random element:', tools.array.randomElement(_array)) | ||
//>get random element element: in | ||
// >get random element element: in | ||
console.log('concat more arrays', tools.array.concat(_array, [0,1,2,3], ['a','b','c'])); | ||
console.log('concat more arrays', tools.array.concat(_array, [0, 1, 2, 3], ['a', 'b', 'c'])) | ||
//>concat more arrays [ 'something', | ||
//> 'remove', | ||
//> 'elements', | ||
//> 'in', | ||
//> 'js', | ||
//> 0, | ||
//> 1, | ||
//> 2, | ||
//> 3, | ||
//> 'a', | ||
//> 'b', | ||
//> 'c' ] | ||
// >concat more arrays [ 'something', | ||
// > 'remove', | ||
// > 'elements', | ||
// > 'in', | ||
// > 'js', | ||
// > 0, | ||
// > 1, | ||
// > 2, | ||
// > 3, | ||
// > 'a', | ||
// > 'b', | ||
// > 'c' ] | ||
tools.array.empty(_array); | ||
console.log('empty it', _array); | ||
tools.array.empty(_array) | ||
console.log('empty it', _array) | ||
//>empty it [] | ||
// >empty it [] | ||
var _tasks = new tools.tasks(function () { | ||
console.log('well done'); | ||
}); | ||
var _tasks = new tools.Tasks(function () { | ||
console.log('well done') | ||
}) | ||
var _asyncOperationTimeout = [ 500, 1000, 200, 1500, 100]; | ||
var _asyncOperationTimeout = [ 500, 1000, 200, 1500, 100] | ||
var i | ||
for(var i in _asyncOperationTimeout) { | ||
_tasks.todo('task#' + i); | ||
for (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]); | ||
for (i in _asyncOperationTimeout) { | ||
setTimeout(function (i) { | ||
return function () { | ||
console.log('done task #', i) | ||
_tasks.done('task#' + i) | ||
} | ||
}(i), _asyncOperationTimeout[i]) | ||
} | ||
//>done task # 4 | ||
//>done task # 2 | ||
//>done task # 0 | ||
//>done task # 1 | ||
//>done task # 3 | ||
//>well done | ||
// >done task # 4 | ||
// >done task # 2 | ||
// >done task # 0 | ||
// >done task # 1 | ||
// >done task # 3 | ||
// >well done | ||
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); | ||
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) | ||
//>to merge { a: 1, b: 2 } | ||
//>merged { a: 4, b: 2, c: { d: 8, e: 9 } } | ||
// >to merge { a: 1, b: 2 } | ||
// >merged { a: 4, b: 2, c: { d: 8, e: 9 } } | ||
console.log('random number from 1 to 100:', tools.random.number(1, 100)); | ||
console.log('random number from 1 to 100:', tools.random.number(1, 100)) | ||
//>random number from 1 to 100: 14 | ||
// >random number from 1 to 100: 14 | ||
console.log('random string of 8 chars, default set:', tools.random.string(8)); | ||
console.log('random string of 8 chars, default set:', tools.random.string(8)) | ||
//>random string of 8 chars, default set: dcglhcvr | ||
// >random string of 8 chars, default set: dcglhcvr | ||
var _hex = '0123456789abcdef'; | ||
console.log('random string of 16 chars, custom set (hex)', _hex, ':', tools.random.string(16, _hex)); | ||
var _hex = '0123456789abcdef' | ||
console.log('random string of 16 chars, custom set (hex)', _hex, ':', tools.random.string(16, _hex)) | ||
//>random string of 16 chars, custom set (hex) 0123456789abcdef : b4a61c1af5360fd4 | ||
// >random string of 16 chars, custom set (hex) 0123456789abcdef : b4a61c1af5360fd4 | ||
var data = { | ||
name: 'Alice', | ||
year: 2014, | ||
color: 'yellow' | ||
}; | ||
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)); | ||
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)) | ||
var str = '({cut these silly brackets please)}'; | ||
console.log('trim:', tools.string.trim(str, ['{','}','(',')'])); | ||
str = '({cut these silly brackets please)}' | ||
console.log('trim:', tools.string.trim(str, ['{', '}', '(', ')'])) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
340
271
17441
1