added Array.prototype.inplaceSortAs(..)...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-10-06 02:43:16 +03:00
parent 1306ec21b3
commit d569ad185c
3 changed files with 60 additions and 8 deletions

View File

@ -183,19 +183,48 @@ Array.prototype.setCmp = function(other){
.sort()) }
// Sort as the other array...
//
// This will sort the intersecting items in the head keeping the rest
// of the items in the same relative order...
//
// NOTE: if an item is in the array multiple times only the first index
// is used...
Array.prototype.sortAs = function(other){
// NOTE: the memory overhead here is better than the time overhead
// when using .indexOf(..)...
other = other.toMap()
var orig = this.toMap()
return this.sort(function(a, b){
var i = other.indexOf(a)
var j = other.indexOf(b)
return i < 0 && j < 0 ? 0
: i < 0 ? 1
: j < 0 ? -1
var i = other.get(a)
var j = other.get(b)
return i == null && j == null ?
orig.get(a) - orig.get(b)
: i == null ?
1
: j == null ?
-1
: i - j }) }
// Same as .sortAs(..) but will not change indexes of items not in other...
//
// XXX not sure if this is the best way to do this...
Array.prototype.inplaceSortAs = function(other){
// sort only the intersection...
var sorted = this
.filter(function(e){
return other.includes(e) })
.sortAs(other)
// zip the sorted items back into this...
this.forEach(function(e, i, l){
other.includes(e)
&& (l[i] = sorted.shift()) })
return this }
// Equivalent to .map(..) / .filter(..) / .reduce(..) / .forEach(..) that
// process the contents in chunks asynchronously...
// Equivalent to .map(..) / .filter(..) / .reduce(..) that process the
// contents in chunks asynchronously...
//
// .mapChunks(func)
// .mapChunks(chunk_size, func)

View File

@ -1,6 +1,6 @@
{
"name": "ig-types",
"version": "2.0.4",
"version": "2.0.6",
"description": "Generic JavaScript types and type extensions...",
"main": "main.js",
"scripts": {

23
test.js
View File

@ -29,6 +29,29 @@ var tests = test.Tests({
var cases = test.Cases({
// Array.js
// - flat
// - includes
// - first
// - last
// - compact
// - len
// - toKeys
// - toMap
// - unique
// - tailUnique
// - cmp
// - setCmp
// - sortAs
// - mapChunks
// - filterChunks
// - reduceChunks
Array: function(assert){
},
// Date.js
// containers.js
UniqueKeyMap: function(assert){
var a = assert(containers.UniqueKeyMap(), '')
var b = assert(containers.UniqueKeyMap([]), '')