added Array.prototype.flat(..) plyfill...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2018-11-14 15:18:06 +03:00
parent 16b5e1d2a2
commit 84f737e554

View File

@ -34,6 +34,18 @@ Object.defineProperty(Object.prototype, 'run', {
}) })
// Array.prototype.flat polyfill...
//
// NOTE: .flat(..) is not yet supported in IE/Edge...
Array.prototype.flat
|| (Array.prototype.flat = function(depth){
depth = typeof(depth) == typeof(123) ? depth : 1
return this.reduce(function(res, e){
return res.concat(e instanceof Array && depth > 0 ?
e.flat(depth-1)
: [e]) }, []) })
// Extended map... // Extended map...
// //
// .emap(func) // .emap(func)
@ -63,12 +75,7 @@ Object.defineProperty(Object.prototype, 'run', {
// ;[1, 2, 3] // ;[1, 2, 3]
// .emap(function(e){ return [[e]] }) // .emap(function(e){ return [[e]] })
// // -> [[1], [2], [3]] // // -> [[1], [2], [3]]
Array.prototype.emap = function(func){ Array.prototype.emap = function(func){ return this.map(func).flat() }
return this
.map(func)
.reduce(function(res, e){
return res
.concat(e instanceof Array ? e : [e] ) }, []) }
// Compact a sparse array... // Compact a sparse array...
@ -78,6 +85,28 @@ Array.prototype.compact = function(){
return this.filter(function(){ return true }) } return this.filter(function(){ return true }) }
// like .length but for sparse arrays will return the element count...
// XXX make this a prop...
/*
Array.prototype.len = function(){
//return this.compact().length
return Object.keys(this).length
}
*/
Object.defineProperty(Array.prototype, 'len', {
get : function () {
return Object.keys(this).length
},
set : function(val){},
// NOTE: this is hear to enable running this module multiple times
// without any side-effects...
configurable: true,
});
// Convert an array to object... // Convert an array to object...
// //
// Format: // Format:
@ -225,28 +254,6 @@ Object.flatCopy = function(obj){
} }
// like .length but for sparse arrays will return the element count...
// XXX make this a prop...
/*
Array.prototype.len = function(){
//return this.compact().length
return Object.keys(this).length
}
*/
Object.defineProperty(Array.prototype, 'len', {
get : function () {
return Object.keys(this).length
},
set : function(val){},
// NOTE: this is hear to enable running this module multiple times
// without any side-effects...
configurable: true,
});
// Quote a string and convert to RegExp to match self literally. // Quote a string and convert to RegExp to match self literally.
var quoteRegExp = var quoteRegExp =
RegExp.quoteRegExp = RegExp.quoteRegExp =