JSFiddle - React, Tailwind, and code Playground
by Scott Kaye
JavaScript
console.clear();
Object.defineProperty(Number.prototype, "down", {
get: function () {
var _ = +this,
a = [_];
while (_--) a.push(_);
return a;
}
});
Object.defineProperty(Number.prototype, "up", {
get: function () {
var _ = +this,
a = [_];
while (_--) a.unshift(_);
return a;
}
});
Object.defineProperty(Number.prototype, "to", {
value: function (to) {
var _ = +this;
var arr = [_];
if (to > _) while (_++ < to) arr.push(_);
else while (_-- > to) arr.push(_);
return arr;
}
});
Object.defineProperty(Array.prototype, "last", {
get: function () {
return this[this.length - 1];
}
});
Object.defineProperty(Number.prototype, "repeat", {
value: function (_) {
var a = [+this];
while (--_) a.push(+this);
return a;
}
});
Object.defineProperty(Array.prototype, "repeat", {
value: function (_) {
var o = this.slice();
while (--_) this.push.apply(this, o);
return this;
}
});
Object.defineProperty(Array.prototype, "asc", {
get: function () {
return this.sort();
}
});
Object.defineProperty(Array.prototype, "desc", {
get: function () {
return this.sort().reverse();
}
});
Object.defineProperty(Array.prototype, "asort", {
value: function (_) {
return this.sort(function (a, b) {
return a > b ? _ : a < b ? -1 * _ : 0
});
}
});
var s1 = [1, 5, 3, 6].asc; //1, 3, 5, 6
var s2 = [1, 5, 3, 6].desc; //6, 5, 3, 1
var s3 = [1, 5, 3, 6].asort(-1); //6, 5, 3, 1
var s4 = [1, 5, 3, 6].asort(1); //1, 3, 5, 6
var d = (5).down; //5, 4, 3, 2, 1, 0
var u = (5).up; //0, 1, 2, 3, 4, 5
var t = (5).to(10); //5, 6, 7, 8, 9, 10
var l = [1, 2, 3, 5].last; //5
var ne = (5).repeat(3); //5, 5, 5
var e = [5, 2].repeat(3); //5, 2, 5, 2, 5, 2
console.log();