Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Array.prototype.sum = function () {
- var total = 0;
- for (var i = 0; i < this.length; i++) {
- total += this[i];
- }
- return total;
- };
- Array.prototype.first = function () {
- return this[0];
- };
- Array.prototype.last = function () {
- return this[this.length - 1];
- };
- Array.prototype.average = function () {
- return this.sum() / this.length;
- };
- Array.prototype.range = function () {
- var self = this.sort();
- return {
- min: self[0],
- max: self[this.length - 1],
- };
- };
- Array.prototype.isempty = function () {
- return this.length == 0;
- };
- Array.prototype.max = function () {
- return Math.max(...this);
- };
- Array.prototype.min = function () {
- return Math.min(...this);
- };
- Array.prototype.notin = Array.prototype.missingfrom = Array.prototype.whichvaluesarenotin = Array.prototype.filternotin = function (
- arr
- ) {
- //filters from arr1 that is found in arr2
- const s = new Set(arr);
- return this.filter((x) => !s.has(x));
- };
- Array.prototype.removethatareingreedy = Array.prototype.removeallthatareingreedy = function (
- arr
- ) {
- arr.map((x) => this.removeall(x));
- return this;
- };
- Array.prototype.missingthatsin = Array.prototype.whichvaluesaremissingfromthisbutarein = function (
- arr
- ) {
- const s = new Set(this);
- return arr.filter((x) => !s.has(x));
- };
- Array.prototype.sub = Array.prototype.rep = function (
- val,
- substitution = ""
- ) {
- let idx = this.indexOf(val);
- this.splice(idx, 1, substitution);
- return this;
- };
- Array.prototype.remove = function (val) {
- let idx = this.indexOf(val);
- this.splice(idx, 1);
- return this;
- };
- Array.prototype.removeall = Array.prototype.greedyremoveall = Array.prototype.removeallwhere = Array.prototype.removeevery = function (
- val
- ) {
- let idx = this.indexOf(val);
- if (idx !== -1) {
- this.splice(idx, 1);
- this.removeall(val);
- }
- return this;
- };
- Array.prototype.nongreedyfilter = Array.prototype.nonegreedyfilteroutthatsin = Array.prototype.filter1by1iterablyfrom = Array.prototype.filtereachonebyone = Array.prototype.removefromarray = function (
- arr
- ) {
- let tmp = [...this];
- arr.map((x) => {
- if (x.isin(tmp)) {
- tmp.sub(x, "#REMOVED");
- }
- });
- return tmp.filter((x) => x != "#REMOVED");
- };
- Array.prototype.allindexeswherevaluesarein = Array.prototype.removefromlistofindexes = Array.prototype.removebyindexesnongreedy = Array.prototype.removebyindexfromarray = function (
- arr
- ) {
- arr.map((x) => this.sub(x, "#DELETED"));
- this.removeall("#DELETED");
- return this;
- };
- Array.prototype.nongreedyremoveiterablyfrom = Array.prototype.remove1by1iterablyfrom = Array.prototype.removeiterativelyfromarr = Array.prototype.removefromarray = function (
- arr
- ) {
- arr.map((x) => {
- if (x.isin(this)) {
- this.sub(x, "#REMOVED");
- }
- });
- this.removeall("#REMOVED");
- return this;
- };
- Array.prototype.suball = function (val) {
- let idx = this.indexOf(val);
- if (idx !== -1) {
- this.splice(idx, 1, "");
- this.sub(val);
- }
- return this;
- };
- Array.prototype.samevalues = Array.prototype.samein = Array.prototype.filtersame = Array.prototype.hassame = Array.prototype.thatsin = Array.prototype.hasthesamein = function (
- arr
- ) {
- //filters from arr1 that are found in arr2
- const s = new Set(arr);
- return this.filter((x) => s.has(x));
- };
- Array.prototype.missingeither = Array.prototype.missingboth = Array.prototype.missingfromboth = Array.prototype.alsomissingfrom = Array.prototype.missinginboth = Array.prototype.missingalsoin = function (
- arr
- ) {
- //removes from arr1 that is found in arr2
- const s = new Set(arr);
- return this.filter((x) => !s.has(x));
- };
- Array.prototype.unique = function () {
- return [...new Set(this)];
- };
- Array.prototype.srt = function (direction = null) {
- if (direction) {
- return this.sort((a, b) => b - a);
- }
- return this.sort((a, b) => a - b);
- };
- Array.prototype.uniquewith = Array.prototype.diff = Array.prototype.diffwith = Array.prototype.notoccuringineither = Array.prototype.difference = function (
- arr
- ) {
- return [
- ...new Set([
- ...this.filter((v) => !arr.includes(v)),
- ...arr.filter((v) => !this.includes(v)),
- ]),
- ];
- };
- Array.prototype.addmissing = Array.prototype.addunique = Array.prototype.addmissingfrom = function (
- arr
- ) {
- const s = new Set(this);
- this.push(...arr.filter((x) => !s.has(x)));
- return this;
- };
- Array.prototype.duplicates = Array.prototype.dupe = Array.prototype.findduplicates = function () {
- let duplicates = this.reduce(function (acc, el, i, arr) {
- if (arr.indexOf(el) !== i && acc.indexOf(el) < 0) acc.push(el);
- return acc;
- }, []);
- return duplicates;
- };
- Array.prototype.pos = Array.prototype.idx = Array.prototype.position = function (
- v
- ) {
- return Array.prototype.indexOf.call(this, v);
- };
- Array.prototype.has = Array.prototype.contains = function (v) {
- return Array.prototype.indexOf.call(this, v) !== -1;
- };
- Array.prototype.srt = function (direction = null) {
- if (direction) {
- return this.sort((a, b) => b - a);
- }
- return this.sort((a, b) => a - b);
- };
- Number.prototype.isin = function (arr) {
- return Array.prototype.indexOf.call(arr, this.valueOf()) !== -1;
- };
- Number.prototype.removeallingreedy = function (arr) {
- arr.removeall(this.valueOf());
- return arr;
- };
- Number.prototype.alllocationsin = Number.prototype.alloccurrenceindexesin = Number.prototype.allindexesinside = Number.prototype.allindexesin = function (
- arr
- ) {
- return arr
- .map((e, i) => (e === this.valueOf() ? i : ""))
- .filter((x) => x !== "");
- };
- String.prototype.isin = String.prototype.has = function (v) {
- return v.includes(this.valueOf());
- };
- String.prototype.locationof = function (v) {
- return v.indexOf(this.valueOf());
- };
- String.prototype.alllocationsin = String.prototype.alloccurrenceindexesin = String.prototype.allindexesinside = String.prototype.allindexesin = function (
- arr
- ) {
- return arr.map((e, i) => (e === value ? i : "")).filter(this.valueOf());
- };
- String.prototype.num = function () {
- return parseInt(this.valueOf());
- };
- Number.prototype.abs = function () {
- return Math.abs(this.valueOf());
- };
- Number.prototype.str = function () {
- return this.toString();
- };
- String.prototype.rep = function (what, wit) {
- let what2 = new RegExp(what, "g");
- return this.replace(what2, wit);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement