From dab43d0aa009781701eb1d15709eed1c4860538d Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Wed, 9 Sep 2020 22:02:15 +0300 Subject: [PATCH] added [].first() and [].last() ... Signed-off-by: Alex A. Naanou --- Viewer/lib/util.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/Viewer/lib/util.js b/Viewer/lib/util.js index 2ebc202c..d226d549 100755 --- a/Viewer/lib/util.js +++ b/Viewer/lib/util.js @@ -61,6 +61,48 @@ Array.prototype.includes return this.indexOf(value) >= 0 }) +// first/last element access short-hands... +// +// .first() +// -> elem +// +// .first(value) +// -> array +// +// +Array.prototype.first + || (Array.prototype.first = function(value){ + return arguments.length > 0 ? + ((this[0] = value), this) + : this[0]}) +Array.prototype.last + || (Array.prototype.last = function(value){ + return arguments.length > 0 ? + ((this[this.length - 1 || 0] = value), this) + : this[this.length - 1]}) + + +/*/ XXX not yet sure should these be funcs or props... +'first' in Array.prototype + || Object.defineProperty(Array.prototype, 'first', { + enumerable: false, + get : function () { + return this[0] }, + set : function(value){ + this[0] = value + return this }, }) + +'last' in Array.prototype + || Object.defineProperty(Array.prototype, 'last', { + enumerable: false, + get : function () { + return this[this.length - 1] }, + set : function(value){ + this[this.length - 1 || 0] = value + return this }, }) +//*/ + + // Compact a sparse array... // // NOTE: this will not compact in-place.