From 84f737e554b240ab9c9378284104b26dca878337 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Wed, 14 Nov 2018 15:18:06 +0300 Subject: [PATCH] added Array.prototype.flat(..) plyfill... Signed-off-by: Alex A. Naanou --- ui (gen4)/lib/util.js | 63 ++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/ui (gen4)/lib/util.js b/ui (gen4)/lib/util.js index c6fc47dd..5cc29d8e 100755 --- a/ui (gen4)/lib/util.js +++ b/ui (gen4)/lib/util.js @@ -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... // // .emap(func) @@ -63,12 +75,7 @@ Object.defineProperty(Object.prototype, 'run', { // ;[1, 2, 3] // .emap(function(e){ return [[e]] }) // // -> [[1], [2], [3]] -Array.prototype.emap = function(func){ - return this - .map(func) - .reduce(function(res, e){ - return res - .concat(e instanceof Array ? e : [e] ) }, []) } +Array.prototype.emap = function(func){ return this.map(func).flat() } // Compact a sparse array... @@ -78,6 +85,28 @@ Array.prototype.compact = function(){ 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... // // 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. var quoteRegExp = RegExp.quoteRegExp =