added [].first() and [].last() ...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-09-09 22:02:15 +03:00
parent 3d67cc740a
commit dab43d0aa0

View File

@ -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.