Comparing version 0.0.14 to 0.0.16
267
main.js
@@ -0,3 +1,47 @@ | ||
// standardjs | ||
/* global Blob File FormData $ */ | ||
var tools = { | ||
/** low level function */ | ||
core: { | ||
/** | ||
* check if v is setted, means it's not null or undefined | ||
* @param {*} v | ||
*/ | ||
isSet: function (v) { | ||
return (typeof v !== 'undefined' || v !== null) | ||
} | ||
}, | ||
/** | ||
* @constructor | ||
* @param {Object} params | ||
* @param {String[]|string} params.segments enabled segments or '*' for all | ||
* @param {String[]|string} params.segments enabled level or '*' for all; levels are: info, warning, error | ||
*/ | ||
Log: function (params) { | ||
var __segments = params.segments | ||
var __levels = params.levels | ||
// @todo const __mode = params.mode console, file, email ... | ||
var verbose = function (segment, level) { | ||
if ((__segments === '*' || tools.array.contains(__segments, segment)) && | ||
(__levels === '*' || tools.array.contains(__levels, level))) { | ||
var _args = Array.prototype.slice.call(arguments) | ||
if (level === 'error') { | ||
console.error.apply(console, _args) | ||
} else if (level === 'warning') { | ||
console.warn.apply(console, _args) | ||
} else { | ||
console.log.apply(console, _args) | ||
} | ||
} | ||
} | ||
return { | ||
verbose: verbose | ||
} | ||
}, | ||
/** | ||
* array utils, inspired to goog.array | ||
@@ -13,4 +57,5 @@ */ | ||
var _index = array.indexOf(item) | ||
if (_index !== -1) | ||
if (_index !== -1) { | ||
array.splice(_index, 1) | ||
} | ||
}, | ||
@@ -28,3 +73,3 @@ /** | ||
* @param {Array} array | ||
* @returns {*} last element of the array or null | ||
* @return {*} last element of the array or null | ||
*/ | ||
@@ -37,3 +82,3 @@ last: function (array) { | ||
* @param {Array} array | ||
* @returns {*} last element of the array or null | ||
* @return {*} last element of the array or null | ||
*/ | ||
@@ -47,3 +92,3 @@ first: function (array) { | ||
* @param {*} item | ||
* @returns {Boolean} | ||
* @return {Boolean} | ||
*/ | ||
@@ -74,3 +119,3 @@ contains: function (array, item) { | ||
* @param {*} not | ||
* @returns {*} element | ||
* @return {*} element | ||
*/ | ||
@@ -92,3 +137,3 @@ randomElement: function (array, not) { | ||
* @param {...Array} arrays to chain | ||
* @returns {Array} chained arrays | ||
* @return {Array} chained arrays | ||
* @example tools.array.concat([0,1,2],[3,4,5]) > [0,1,2,3,4,5] | ||
@@ -103,7 +148,8 @@ */ | ||
empty: function (array) { | ||
while (array[0]) | ||
while (array[0]) { | ||
array.pop() | ||
} | ||
}, | ||
add: function(array, item, unique) { | ||
if(unique && tools.array.contains(array, item)) { | ||
add: function (array, item, unique) { | ||
if (unique && tools.array.contains(array, item)) { | ||
return | ||
@@ -116,2 +162,3 @@ } else { | ||
}, | ||
/** | ||
@@ -124,7 +171,8 @@ * random utils | ||
* @param {number} val max item | ||
* @returns {number} | ||
* @return {number} | ||
*/ | ||
rnd: function (val) { | ||
if (!val) | ||
if (!val) { | ||
return 0 | ||
} | ||
return Math.floor(val * (Math.random() % 1)) | ||
@@ -136,7 +184,8 @@ }, | ||
* @param {number} max | ||
* @returns {number} | ||
* @return {number} | ||
*/ | ||
number: function (min, max) { | ||
if (!max) | ||
if (!max) { | ||
return tools.random.rnd(min) | ||
} | ||
min = Math.floor(min) | ||
@@ -150,9 +199,11 @@ max = Math.floor(max) | ||
* @param {Array} [set=qwertyuiopasdfghjklzxcvbnm] | ||
* @returns {String} | ||
* @return {String} | ||
*/ | ||
string: function (length, set) { | ||
if (!length) | ||
if (!length) { | ||
length = 8 | ||
if (!set) | ||
} | ||
if (!set) { | ||
set = 'qwertyuiopasdfghjklzxcvbnm' | ||
} | ||
var _str = '' | ||
@@ -164,13 +215,20 @@ for (var i = 0; i < length; i++) { | ||
}, | ||
/** | ||
* get random hex string | ||
* @param {number} [length=8] | ||
* @return {String} | ||
*/ | ||
hex: function (length) { | ||
if (!length) | ||
if (!length) { | ||
length = 8 | ||
} | ||
return tools.random.string(length, '0123456789abcdef') | ||
} | ||
}, | ||
object: { | ||
/** | ||
* merge obj2 into obj1 | ||
* @param {object} obj1 | ||
* @param {object} obj2 | ||
* @param {Object} obj1 | ||
* @param {Object} obj2 | ||
*/ | ||
@@ -191,7 +249,8 @@ merge: function (obj1, obj2) { | ||
* @param {Object|Array} obj The array or the object to clone | ||
* @returns {Object|Array} | ||
* @return {Object|Array} | ||
*/ | ||
clone: function (obj) { | ||
if (obj === null || obj === undefined) | ||
if (obj === null || obj === undefined) { | ||
return obj | ||
} | ||
var _type = (obj instanceof Array) ? _type = 'array' : typeof obj | ||
@@ -204,4 +263,5 @@ if (_type === 'object' || _type === 'array') { | ||
} else { | ||
if (obj.clone) | ||
if (obj.clone) { | ||
return obj.clone() | ||
} | ||
/** | ||
@@ -211,4 +271,5 @@ * @type {Array|Object} | ||
var _clone = _type === 'array' ? [] : {} | ||
for (var key in obj) | ||
for (var key in obj) { | ||
_clone[key] = tools.object.clone(obj[key]) | ||
} | ||
return _clone | ||
@@ -221,4 +282,4 @@ } | ||
* @see http://google.github.io/closure-library/api/source/closure/goog/object/object.js.src.html#l225 | ||
* @param {object} obj | ||
* @returns {Array} | ||
* @param {Object} obj | ||
* @return {Array} | ||
*/ | ||
@@ -233,4 +294,4 @@ getKeys: function (obj) { | ||
/** | ||
* @param {object} obj | ||
* @returns {object} | ||
* @param {Object} obj | ||
* @return {Object} | ||
*/ | ||
@@ -241,7 +302,9 @@ sortKeys: function (obj) { | ||
var _obj = {} | ||
for (var i = 0; i < _keys.length; i++) | ||
for (var i = 0; i < _keys.length; i++) { | ||
_obj[_keys[i]] = obj[_keys[i]] | ||
} | ||
return _obj | ||
} | ||
}, | ||
/** | ||
@@ -255,4 +318,5 @@ * async parallel task manager | ||
var __chronos = {} | ||
if (!options) | ||
if (!options) { | ||
options = {} | ||
} | ||
@@ -286,2 +350,3 @@ return { | ||
}, | ||
string: { | ||
@@ -294,3 +359,3 @@ /** | ||
* @param {bool} [remove=false] remove missing placeholders from obj | ||
* @returns {unresolved} | ||
* @return {unresolved} | ||
*/ | ||
@@ -307,3 +372,3 @@ template: function (str, obj, remove) { | ||
* @param {?string[]} cuts | ||
* @returns {string} | ||
* @return {string} | ||
*/ | ||
@@ -325,6 +390,8 @@ trim: function (str, cuts) { | ||
}, | ||
time: { | ||
Chrono: function (tag) { | ||
if (!tag) | ||
if (!tag) { | ||
tag = 'chrono' | ||
} | ||
@@ -337,3 +404,3 @@ var __tick | ||
* @function | ||
* @returns {string} | ||
* @return {string} | ||
*/ | ||
@@ -359,12 +426,13 @@ return { | ||
}, | ||
console: { | ||
/** | ||
* | ||
* @param {object} options | ||
* @param {Object} options | ||
* @param {number} [options.tick=10] millisec | ||
* @param {object} [options.spinner=['. ', '.. ', '... ', '.... ', '.....']] | ||
* @param {Object} [options.spinner=['. ', '.. ', '... ', '.... ', '.....']] | ||
*/ | ||
Wait: function (options) { | ||
if (!options) | ||
if (!options) { | ||
options = {} | ||
} | ||
@@ -378,9 +446,11 @@ var __wait = null | ||
var start = function () { | ||
if (__wait) | ||
if (__wait) { | ||
return | ||
} | ||
__timer = 0 | ||
__spinner = 0 | ||
__wait = setInterval(function () { | ||
if (!__wait) | ||
if (!__wait) { | ||
return | ||
} | ||
process.stdout.write(__spin[__spinner % __spin.length] + ' ' + (__timer / 1000).toFixed(2) + ' sec \r') | ||
@@ -402,4 +472,123 @@ __timer += __tick | ||
} | ||
}, | ||
ajax: { | ||
_cache: {}, | ||
/** | ||
* send ajax request using $.ajax | ||
* @param {Object} args | ||
* @param {Object} args.options $.ajax options -> @see http://api.jquery.com/jquery.ajax/ | ||
* default: { type: 'POST', dataType: 'json', cache: false, xhrFields: { withCredentials: true } }} | ||
* @param {Object} args.data | ||
* @param {string} args.url | ||
* @param {boolean} args.cache | ||
* @param {boolean} args.upload | ||
* @param {boolean} args.xhr return the xhr object, for call "abort" (especially for upload) | ||
* .. timeout | ||
* @param {function()} args.callback | ||
*/ | ||
request: function (args) { | ||
if (args.cache && tools.ajax._cache[args.url]) { | ||
args.callback && args.callback(tools.ajax._cache[args.url]) | ||
return | ||
} | ||
if (!args.options) { | ||
args.options = {} | ||
} | ||
if (!args.data) { | ||
args.data = {} | ||
} else { | ||
for (var i in args.data) { | ||
if (args.data[i] instanceof Date) { | ||
args.data[i] = args.data[i].toISOString() | ||
} else if (!tools.core.isSet(args.data[i])) { | ||
delete args.data[i] | ||
} | ||
} | ||
} | ||
var defaults = { | ||
type: 'POST', | ||
dataType: 'json', | ||
cache: false, | ||
xhrFields: { withCredentials: true }, | ||
success: function (response, textStatus, XMLHttpRequest) { | ||
if (tools.ajax.error(response)) { | ||
args.callback && args.callback(response, textStatus) | ||
} else { | ||
args.callback && args.callback(response, textStatus, XMLHttpRequest) | ||
if (args.cache && !tools.ajax._cache[args.url]) { | ||
tools.ajax._cache[args.url] = response | ||
} | ||
} | ||
}, | ||
error: function (xhr, textStatus, errorThrown) { | ||
if (xhr.status !== 0 || (!args.upload && (textStatus === 'abort' || textStatus === 'timeout'))) { | ||
args.callback(xhr, textStatus, errorThrown) | ||
} | ||
} | ||
} | ||
var _options = tools.object.clone(args.options) | ||
if (args.upload) { | ||
_options = tools.ajax._upload(_options) | ||
} | ||
$.ajax($.extend(defaults, _options)) | ||
if (args.xhr) { | ||
return args.xhr | ||
} | ||
}, | ||
_upload: function (options) { | ||
var _formData = new FormData() | ||
for (var _var in options.data) { | ||
var _val = options.data[_var] | ||
if (_val instanceof Blob || _val instanceof File) { | ||
_formData.append(_var, _val, _val._filename || _var) | ||
} else if (_val instanceof Array) { | ||
for (var i in _val) { | ||
_formData.append(_var + '[' + i + ']', _val[i]) | ||
} | ||
} else if (_val instanceof Object) { | ||
_formData.append(_var, JSON.stringify(_val)) | ||
} else { | ||
_formData.append(_var, _val) | ||
} | ||
} | ||
options.data = _formData | ||
options.contentType = false | ||
options.processData = false | ||
options.xhr = function () { | ||
var _xhr = $.ajaxSettings.xhr() | ||
_xhr.upload && _xhr.upload.addEventListener('progress', options.progress, false) | ||
return _xhr | ||
} | ||
return options | ||
}, | ||
error: function (response) { | ||
if (!response) return true | ||
if (response.error) { | ||
return true | ||
} | ||
return false | ||
} | ||
} | ||
} | ||
// enums | ||
tools.Log.level = { | ||
ERROR: 'ERROR', | ||
WARNING: 'WARNING', | ||
INFO: 'INFO' | ||
} | ||
// compatibilty < 0.0.10 - to remove | ||
@@ -406,0 +595,0 @@ tools.tasks = tools.Tasks |
{ | ||
"name": "a-toolbox", | ||
"version": "0.0.14", | ||
"version": "0.0.16", | ||
"description": "lightweight tools", | ||
@@ -25,3 +25,4 @@ "keywords": [ | ||
"scripts": { | ||
"test": "istanbul cover test.js -x test.js" | ||
"test": "istanbul cover test.js -x test.js", | ||
"doc": "jsdoc -c jsdoc.json -R README.md" | ||
}, | ||
@@ -33,3 +34,8 @@ "tonicExampleFilename": "test.js", | ||
], | ||
"main": "main" | ||
"main": "main", | ||
"devDependencies": { | ||
"standard": "x", | ||
"istanbul": "x", | ||
"ink-docstrap": "x" | ||
} | ||
} |
@@ -11,9 +11,13 @@ # a-toolbox | ||
[![NPM](https://nodei.co/npm-dl/a-toolbox.png)](https://nodei.co/npm/a-toolbox/) | ||
Javascript lightweight tools | ||
"This is my rifle. There are many others like it, but this one is mine." | ||
> "This is my rifle. There are many others like it, but this one is mine." | ||
## Npm Installation | ||
$ npm install a-toolbox | ||
```` | ||
npm install a-toolbox | ||
```` | ||
@@ -20,0 +24,0 @@ ## Tools on Node.js |
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
26136
630
288
3