Comparing version 0.0.1 to 0.0.2
109
main.js
String.prototype.replaceAll = function(from, to) { | ||
String.prototype.replaceAll = function (from, to) { | ||
return this.split(from).join(to); | ||
}; | ||
var atoolbox = { | ||
var tools = { | ||
/** | ||
@@ -12,2 +11,7 @@ * array utils, inspired to goog.array | ||
array: { | ||
/** | ||
* remove an element from array | ||
* @param {Array} array | ||
* @param {*} value | ||
*/ | ||
remove: function (array, value) { | ||
@@ -17,6 +21,89 @@ var _index = array.indexOf(value); | ||
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; | ||
}, | ||
/** | ||
* check if array contains an element | ||
* @param {Array} array | ||
* @param {*} value | ||
* @returns {Boolean} | ||
*/ | ||
contains: function (array, value) { | ||
return array.indexOf(value) != -1; | ||
}, | ||
/** | ||
* insert an item into array at index position | ||
* @param {Array} array | ||
* @param {number} index | ||
* @param {*} item | ||
*/ | ||
insert: function (array, index, item) { | ||
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.math.random(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); | ||
} | ||
}, | ||
/** | ||
* math (?) utils | ||
*/ | ||
math: { | ||
/** | ||
* return random int from 0 to val | ||
* @param {number} val max value | ||
* @returns {number} | ||
*/ | ||
rnd: function (val) { | ||
if(!val) | ||
return 0; | ||
return Math.floor(val * (Math.random() % 1)); | ||
}, | ||
/** | ||
* return random int from min to max | ||
* @param {number} min | ||
* @param {number} max | ||
* @returns {number} | ||
*/ | ||
random: function (min, max) { | ||
if (!max) | ||
return tools.math.rnd(min); | ||
min = Math.floor(min); | ||
max = Math.floor(max); | ||
return min + tools.math.rnd(1 + max - min); | ||
} | ||
}, | ||
/** | ||
* tasks (promises) async manage | ||
@@ -28,7 +115,15 @@ * @param {function} done callback when all tasks are completed | ||
return { | ||
/** | ||
* schedule what to do | ||
* @param {string} id | ||
*/ | ||
todo: function (id) { | ||
__tasks.push(id); | ||
}, | ||
/** | ||
* declare it's done | ||
* @param {string} id | ||
*/ | ||
done: function (id) { | ||
atoolbox.array.remove(__tasks, id); | ||
tools.array.remove(__tasks, id); | ||
1 > __tasks.length && (done && done()); | ||
@@ -39,3 +134,3 @@ } | ||
}; | ||
module.exports = atoolbox; | ||
if (typeof window == 'undefined') | ||
module.exports = tools; |
{ | ||
"name": "a-toolbox", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "lightweight tools", | ||
"keywords": ["tools", "lib", "misc"], | ||
"keywords": ["tools", "lib", "misc", "toolbox", "array", "task", "random"], | ||
"url": "http://github.com/simone-sanfratello/a-toolbox", | ||
@@ -7,0 +7,0 @@ "author": "Simone Sanfratello <simone.sanfra@gmail.com>", |
@@ -15,3 +15,3 @@ # a-toolbox | ||
## Tools | ||
## Tools on Node.js | ||
@@ -24,25 +24,7 @@ ```js | ||
#### add replaceAll in String prototype | ||
## Tools on Browser | ||
```js | ||
<script src="https://raw.githubusercontent.com/simone-sanfratello/a-toolbox/master/main.js"></script> | ||
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 | ||
``` | ||
#### array function management, inspired to goog.array (to be continued) | ||
```js | ||
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' ] | ||
``` | ||
#### tasks (promises) async manage | ||
@@ -80,2 +62,25 @@ | ||
#### add replaceAll in String prototype | ||
```js | ||
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 | ||
``` | ||
#### array function management, inspired to goog.array (to be continued) | ||
```js | ||
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' ] | ||
``` | ||
## License | ||
@@ -82,0 +87,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7323
128
106