Comparing version 0.1.10 to 0.1.12
35
index.js
@@ -233,3 +233,3 @@ 'use strict' | ||
} | ||
var _type = (obj instanceof Array) ? _type = 'array' : typeof obj | ||
var _type = (obj instanceof Array) ? 'array' : typeof obj | ||
if (_type === 'object' || _type === 'array') { | ||
@@ -288,3 +288,3 @@ if (obj instanceof Date) { | ||
inherits: function (destination, source) { | ||
// util.inherits(destination, source) | ||
// util.inherits(destination, source) | ||
Object.getOwnPropertyNames(source).forEach((property) => { | ||
@@ -340,2 +340,31 @@ destination[property] = source[property] | ||
/** | ||
* restore flat object | ||
* @param {object} obj | ||
* @returns {object} | ||
* @example { 'a.a1': 1, 'a.a2': 2, 'b': 3 } >> { a: { a1: 1, a2: 2 }, b: 3 } | ||
*/ | ||
raise: function (obj) { | ||
const _raise = {} | ||
const _f = function (flat, raise) { | ||
for (const path in flat) { | ||
const _keys = path.split('.') | ||
let _cursor = raise | ||
_keys.forEach((key, i) => { | ||
if (i < _keys.length - 1) { | ||
if (!_cursor[key]) { | ||
_cursor[key] = {} | ||
} | ||
_cursor = _cursor[key] | ||
} else { | ||
_cursor[key] = flat[path] | ||
} | ||
}) | ||
} | ||
} | ||
_f(obj, _raise) | ||
return _raise | ||
}, | ||
/** | ||
* get value in object using a flat key | ||
@@ -499,3 +528,3 @@ * @todo check params | ||
time: function () { | ||
__time | ||
return __time | ||
} | ||
@@ -502,0 +531,0 @@ } |
{ | ||
"name": "a-toolbox", | ||
"version": "0.1.10", | ||
"version": "0.1.12", | ||
"description": "javascript lightweight basic tools", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
131
test/tdd.js
@@ -1,128 +0,11 @@ | ||
// var tools = require('./main') | ||
var tools = require('a-toolbox') | ||
'use strict' | ||
const tools = require('../index') | ||
// add replaceAll in String prototype | ||
console.log(tools.string.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) | ||
// >[ 'remove', 'elements', 'in', 'js', 'arrays' ] | ||
tools.array.removeAt(_array, 4) | ||
console.log(_array) | ||
// >[ 'remove', 'elements', 'in', 'arrays' ] | ||
console.log('last element is', tools.array.last(_array)) | ||
// >last element is js | ||
console.log('first element is', tools.array.first(_array)) | ||
// >first element is remove | ||
console.log('contains js?', tools.array.contains(_array, 'js')) | ||
// >contains js? true | ||
console.log('contains ruby?', tools.array.contains(_array, 'ruby')) | ||
// >contains ruby? false | ||
tools.array.insert(_array, 0, 'something') | ||
console.log('inserted something', _array) | ||
// >inserted something [ 'something', 'remove', 'elements', 'in', 'js' ] | ||
console.log('get random element:', tools.array.randomElement(_array)) | ||
// >get random element element: in | ||
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' ] | ||
tools.array.empty(_array) | ||
console.log('empty it', _array) | ||
// >empty it [] | ||
var _tasks = new tools.Tasks(function () { | ||
console.log('well done') | ||
}) | ||
var _asyncOperationTimeout = [ 500, 1000, 200, 1500, 100] | ||
var i | ||
for (i in _asyncOperationTimeout) { | ||
_tasks.todo('task#' + i) | ||
const samples = { | ||
data: { a: { a1: 1, a2: 2 }, b: 3 } | ||
} | ||
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 | ||
let _flat = tools.object.flat(samples.data) | ||
let _raise = tools.object.raise(_flat) | ||
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 } } | ||
console.log('random number from 1 to 100:', tools.random.number(1, 100)) | ||
// >random number from 1 to 100: 14 | ||
console.log('random string of 8 chars, default set:', tools.random.string(8)) | ||
// >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)) | ||
// >random hash of 64 chars | ||
console.log('random hash of 64 chars', tools.random.hash()) | ||
// >random string of 16 chars, custom set (hex) 0123456789abcdef : b4a61c1af5360fd4 | ||
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)) | ||
str = '({cut these silly brackets please)}' | ||
console.log('trim:', tools.string.trim(str, ['{', '}', '(', ')'])) | ||
console.log(_flat, _raise) |
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
28628
8
783