Comparing version 1.4.1 to 1.4.2
83
array.js
@@ -170,15 +170,82 @@ /** | ||
* @method tools.array.flat | ||
* @param {Array<*>} array | ||
* @param {Array<*>} array_ | ||
* @test.case [0,[1,2],[3]] > [0,1,2,3] | ||
*/ | ||
flat: function (array) { | ||
flat: function (array_) { | ||
let _flat = [] | ||
array.map((i) => { | ||
if (i instanceof Array) { | ||
_flat = _flat.concat(array.flat(i)) | ||
for (let i = 0; i < array_.length; i++) { | ||
const _e = array_[i] | ||
if (_e instanceof Array) { | ||
_flat = _flat.concat(array.flat(_e)) | ||
} else { | ||
_flat.push(_e) | ||
} | ||
} | ||
return _flat | ||
}, | ||
/** | ||
* insert an element in a sorted array, keeping sorted | ||
* @method tools.array.sortingInsert | ||
* @param {Array<*>} array_ | ||
* @param {*} item | ||
* @test.case [0,1,2,10,11,20], 15 > &[0,1,2,10,11,15,20] | ||
* @test.case [0,1,2,10,11,20], 25 > &[0,1,2,10,11,20,25] | ||
* @test.case [0,1,2,10,11,20], 0 > &[0,0,1,2,10,11,20] | ||
* @test.case [1,2,10,11,20], 0 > &[0,1,2,10,11,20] | ||
*/ | ||
sortingInsert: function (array_, item) { | ||
let _end = array_.length - 1 | ||
let _start = 0 | ||
let i, _element | ||
while (_start <= _end) { | ||
i = (_start + _end) / 2 | 0 | ||
_element = array_[i] | ||
if (_element < item) { | ||
_start = i + 1 | ||
} else if (_element > item) { | ||
_end = i - 1 | ||
} else { | ||
array.insert(array_, i, item) | ||
return | ||
} | ||
_flat.push(i) | ||
}) | ||
return _flat | ||
} | ||
if (_end < array_.length - 1) { | ||
array.insert(array_, _end + 1, item) | ||
} else if (_start > 0) { | ||
array.insert(array_, _start, item) | ||
} else { | ||
array_.push(item) | ||
} | ||
}, | ||
/** | ||
* like Array.indexOf but perform binary search (array should be sorted) | ||
* @method tools.array.binaryIndexOf | ||
* @param {Array<*>} array | ||
* @param {*} item | ||
* @return {number} index of element of -1 | ||
* @test.case [0,1,2,3], 0 > 0 | ||
* @test.case [0,1,2,3,19,20,100], 19 > 4 | ||
* @test.case [0,1,2,3,19,20,100,999], 11 > -1 | ||
*/ | ||
binaryIndexOf: function (array, item) { | ||
let _end = array.length - 1 | ||
let _start = 0 | ||
let i, _element | ||
while (_start <= _end) { | ||
i = (_start + _end) / 2 | 0 | ||
_element = array[i] | ||
if (_element < item) { | ||
_start = i + 1 | ||
} else if (_element > item) { | ||
_end = i - 1 | ||
} else { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} | ||
@@ -185,0 +252,0 @@ |
@@ -1250,3 +1250,3 @@ (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
},{"./array":16,"./hash":17,"./object":18,"./random":19,"./string":20,"./task":21,"./time":22,"./util":23}],16:[function(require,module,exports){ | ||
"use strict";var array={remove:function remove(array,item){var _index=array.indexOf(item);if(_index!==-1){array.splice(_index,1)}},removeAt:function removeAt(array,index){return Array.prototype.splice.call(array,index,1).length===1},last:function last(array){return array[array.length-1]},at:function at(array,p){if(p<0){p=array.length+p}return array[p]},first:function first(array){return array[0]},contains:function contains(array,item){return array.indexOf(item)!==-1},insert:function insert(array,index,item){if(index>array.length){index=array.length}if(array[index]){array.splice(index,0,item)}else{array[index]=item}},concat:function concat(args){return Array.prototype.concat.apply(Array.prototype,arguments)},empty:function empty(array){while(array[0]){array.pop()}},add:function add(array,item,unique){if(!!unique&&array.indexOf(item)!==-1){return}array.push(item)},flat:function flat(array){var _flat=[];array.map(function(i){if(i instanceof Array){_flat=_flat.concat(array.flat(i));return}_flat.push(i)});return _flat}};module.exports=array; | ||
"use strict";var array={remove:function remove(array,item){var _index=array.indexOf(item);if(_index!==-1){array.splice(_index,1)}},removeAt:function removeAt(array,index){return Array.prototype.splice.call(array,index,1).length===1},last:function last(array){return array[array.length-1]},at:function at(array,p){if(p<0){p=array.length+p}return array[p]},first:function first(array){return array[0]},contains:function contains(array,item){return array.indexOf(item)!==-1},insert:function insert(array,index,item){if(index>array.length){index=array.length}if(array[index]){array.splice(index,0,item)}else{array[index]=item}},concat:function concat(args){return Array.prototype.concat.apply(Array.prototype,arguments)},empty:function empty(array){while(array[0]){array.pop()}},add:function add(array,item,unique){if(!!unique&&array.indexOf(item)!==-1){return}array.push(item)},flat:function flat(array_){var _flat=[];for(var i=0;i<array_.length;i++){var _e=array_[i];if(_e instanceof Array){_flat=_flat.concat(array.flat(_e))}else{_flat.push(_e)}}return _flat},sortingInsert:function sortingInsert(array_,item){var _end=array_.length-1;var _start=0;var i=void 0,_element=void 0;while(_start<=_end){i=(_start+_end)/2|0;_element=array_[i];if(_element<item){_start=i+1}else if(_element>item){_end=i-1}else{array.insert(array_,i,item);return}}if(_end<array_.length-1){array.insert(array_,_end+1,item)}else if(_start>0){array.insert(array_,_start,item)}else{array_.push(item)}},binaryIndexOf:function binaryIndexOf(array,item){var _end=array.length-1;var _start=0;var i=void 0,_element=void 0;while(_start<=_end){i=(_start+_end)/2|0;_element=array[i];if(_element<item){_start=i+1}else if(_element>item){_end=i-1}else{return i}}return-1}};module.exports=array; | ||
@@ -1253,0 +1253,0 @@ },{}],17:[function(require,module,exports){ |
{ | ||
"name": "a-toolbox", | ||
"version": "1.4.1", | ||
"version": "1.4.2", | ||
"description": "javascript lightweight basic tools, isomorphic", | ||
"main": "index.js", | ||
"dependencies": { | ||
"hash.js": "^1.x" | ||
"hash.js": "^1.1.3" | ||
}, | ||
@@ -20,3 +20,3 @@ "devDependencies": { | ||
"gulp-clean": "^0.3.x", | ||
"gulp-sourcemaps": "^2.6.x", | ||
"gulp-sourcemaps": "^2.6.4", | ||
"ink-docstrap": "^1.x", | ||
@@ -26,3 +26,3 @@ "pre-commit": "^1.x", | ||
"tap": "^10.x", | ||
"tollo": "^0.0.3-dev", | ||
"tollo": "0.0.3-dev", | ||
"vinyl-source-stream": "^1.x" | ||
@@ -29,0 +29,0 @@ }, |
@@ -237,2 +237,30 @@ # a-toolbox | ||
#### array.sortingInsert(array_, item) | ||
- _array__ \<Array<*>\> | ||
- _item_ \<*\> | ||
insert an element in a sorted array, keeping sorted | ||
_Example_ | ||
````js | ||
let a = [0,1,2,10,11,20] | ||
tools.array.sortingInsert(a, 15) | ||
// > a = [0,1,2,10,11,15,20] | ||
```` | ||
#### array.binaryIndexOf(array, item) | ||
- _array_ \<Array<*>\> | ||
- _item_ \<*\> | ||
- _return:_ number index of element of -1 | ||
like Array.indexOf but perform binary search (array should be sorted) | ||
_Example_ | ||
````js | ||
tools.array.binaryIndexOf([0,1,2,3], 0) | ||
// > 0 | ||
```` | ||
--- | ||
@@ -239,0 +267,0 @@ |
@@ -170,15 +170,82 @@ /** | ||
* @method tools.array.flat | ||
* @param {Array<*>} array | ||
* @param {Array<*>} array_ | ||
* @test.case [0,[1,2],[3]] > [0,1,2,3] | ||
*/ | ||
flat: function (array) { | ||
flat: function (array_) { | ||
let _flat = [] | ||
array.map((i) => { | ||
if (i instanceof Array) { | ||
_flat = _flat.concat(array.flat(i)) | ||
for (let i = 0; i < array_.length; i++) { | ||
const _e = array_[i] | ||
if (_e instanceof Array) { | ||
_flat = _flat.concat(array.flat(_e)) | ||
} else { | ||
_flat.push(_e) | ||
} | ||
} | ||
return _flat | ||
}, | ||
/** | ||
* insert an element in a sorted array, keeping sorted | ||
* @method tools.array.sortingInsert | ||
* @param {Array<*>} array_ | ||
* @param {*} item | ||
* @test.case [0,1,2,10,11,20], 15 > &[0,1,2,10,11,15,20] | ||
* @test.case [0,1,2,10,11,20], 25 > &[0,1,2,10,11,20,25] | ||
* @test.case [0,1,2,10,11,20], 0 > &[0,0,1,2,10,11,20] | ||
* @test.case [1,2,10,11,20], 0 > &[0,1,2,10,11,20] | ||
*/ | ||
sortingInsert: function (array_, item) { | ||
let _end = array_.length - 1 | ||
let _start = 0 | ||
let i, _element | ||
while (_start <= _end) { | ||
i = (_start + _end) / 2 | 0 | ||
_element = array_[i] | ||
if (_element < item) { | ||
_start = i + 1 | ||
} else if (_element > item) { | ||
_end = i - 1 | ||
} else { | ||
array.insert(array_, i, item) | ||
return | ||
} | ||
_flat.push(i) | ||
}) | ||
return _flat | ||
} | ||
if (_end < array_.length - 1) { | ||
array.insert(array_, _end + 1, item) | ||
} else if (_start > 0) { | ||
array.insert(array_, _start, item) | ||
} else { | ||
array_.push(item) | ||
} | ||
}, | ||
/** | ||
* like Array.indexOf but perform binary search (array should be sorted) | ||
* @method tools.array.binaryIndexOf | ||
* @param {Array<*>} array | ||
* @param {*} item | ||
* @return {number} index of element of -1 | ||
* @test.case [0,1,2,3], 0 > 0 | ||
* @test.case [0,1,2,3,19,20,100], 19 > 4 | ||
* @test.case [0,1,2,3,19,20,100,999], 11 > -1 | ||
*/ | ||
binaryIndexOf: function (array, item) { | ||
let _end = array.length - 1 | ||
let _start = 0 | ||
let i, _element | ||
while (_start <= _end) { | ||
i = (_start + _end) / 2 | 0 | ||
_element = array[i] | ||
if (_element < item) { | ||
_start = i + 1 | ||
} else if (_element > item) { | ||
_end = i - 1 | ||
} else { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} | ||
@@ -185,0 +252,0 @@ |
@@ -11,4 +11,4 @@ const tester = require('tollo') | ||
{ | ||
input: [[\'js\',\'ruby\',\'python\'], \'ruby\'], | ||
output: [\'js\',\'python\'] | ||
input: [['js','ruby','python'], 'ruby'], | ||
output: ['js','python'] | ||
}, | ||
@@ -104,7 +104,7 @@ { | ||
{ | ||
input: [[0,1,\'0\',false], -2], | ||
output: \'0\' | ||
input: [[0,1,'0',false], -2], | ||
output: '0' | ||
}, | ||
{ | ||
input: [[undefined,\'0\',false], 0], | ||
input: [[undefined,'0',false], 0], | ||
output: undefined | ||
@@ -141,3 +141,3 @@ }, | ||
{ | ||
input: [[undefined,\'0\',false]], | ||
input: [[undefined,'0',false]], | ||
output: undefined | ||
@@ -174,16 +174,16 @@ }, | ||
{ | ||
input: [[\'john\', \'alice\', \'bob\'], 0, \'mary\'], | ||
output: [\'mary\', \'john\', \'alice\', \'bob\'] | ||
input: [['john', 'alice', 'bob'], 0, 'mary'], | ||
output: ['mary', 'john', 'alice', 'bob'] | ||
}, | ||
{ | ||
input: [[\'john\', \'alice\', \'bob\'], 1, \'mary\'], | ||
output: [\'john\', \'mary\', \'alice\', \'bob\'] | ||
input: [['john', 'alice', 'bob'], 1, 'mary'], | ||
output: ['john', 'mary', 'alice', 'bob'] | ||
}, | ||
{ | ||
input: [[\'john\', \'alice\', \'bob\'], -1, \'mary\'], | ||
output: [\'john\', \'alice\', \'bob\', \'mary\'] | ||
input: [['john', 'alice', 'bob'], -1, 'mary'], | ||
output: ['john', 'alice', 'bob', 'mary'] | ||
}, | ||
{ | ||
input: [[\'john\', \'alice\', \'bob\'], -2, \'mary\'], | ||
output: [\'john\', \'alice\', \'mary\', \'bob\'] | ||
input: [['john', 'alice', 'bob'], -2, 'mary'], | ||
output: ['john', 'alice', 'mary', 'bob'] | ||
} | ||
@@ -203,4 +203,4 @@ ], | ||
{ | ||
input: [[0, 1, 2, 3], [\'a\', \'b\', \'c\'], [{a: 2}]], | ||
output: [0, 1, 2, 3, \'a\', \'b\', \'c\', {a: 2}] | ||
input: [[0, 1, 2, 3], ['a', 'b', 'c'], [{a: 2}]], | ||
output: [0, 1, 2, 3, 'a', 'b', 'c', {a: 2}] | ||
} | ||
@@ -244,12 +244,52 @@ ], | ||
{ | ||
input: [0,[1,2],[3]], | ||
input: [[0,[1,2],[3]]], | ||
output: [0,1,2,3] | ||
} | ||
], | ||
assert: tester.assert.equal | ||
}, | ||
'array.sortingInsert': { | ||
describe: '', | ||
mode: tester.mode.SYNC, | ||
act: instance.sortingInsert, | ||
cases: [ | ||
{ | ||
input: [[0,1,2,10,11,20], 15], | ||
output: [0,1,2,10,11,15,20] | ||
}, | ||
{ | ||
input: [0,[1,2],[3,[4,5,[6,7]]]], | ||
output: [0,1,2,3,4,5,6,7] | ||
input: [[0,1,2,10,11,20], 25], | ||
output: [0,1,2,10,11,20,25] | ||
}, | ||
{ | ||
input: [[0,1,2,10,11,20], 0], | ||
output: [0,0,1,2,10,11,20] | ||
}, | ||
{ | ||
input: [[1,2,10,11,20], 0], | ||
output: [0,1,2,10,11,20] | ||
} | ||
], | ||
assert: tester.assert.mutation | ||
}, | ||
'array.binaryIndexOf': { | ||
describe: '', | ||
mode: tester.mode.SYNC, | ||
act: instance.binaryIndexOf, | ||
cases: [ | ||
{ | ||
input: [[0,1,2,3], 0], | ||
output: 0 | ||
}, | ||
{ | ||
input: [[0,1,2,3,19,20,100], 19], | ||
output: 4 | ||
}, | ||
{ | ||
input: [[0,1,2,3,19,20,100,999], 11], | ||
output: -1 | ||
} | ||
], | ||
assert: tester.assert.equal | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
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
307128
4191
835
Updatedhash.js@^1.1.3