Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Zray extends Array {
- constructor(...args) {
- super(...args);
- }
- }
- Zray.prototype.sum = function () {
- var total = 0;
- for (var i = 0; i < this.length; i++) {
- total += this[i];
- }
- return total;
- };
- Zray.prototype.first = function () {
- return this[0];
- };
- Zray.prototype.last = function () {
- return this[this.length - 1];
- };
- Zray.prototype.average = function () {
- return this.sum() / this.length;
- };
- Zray.prototype.range = function () {
- var self = this.sort();
- return {
- min: self[0],
- max: self[this.length - 1],
- };
- };
- Zray.prototype.isempty = function () {
- return this.length == 0;
- };
- Zray.prototype.max = function () {
- return Math.max(...this);
- };
- Zray.prototype.min = function () {
- return Math.min(...this);
- };
- Zray.prototype.notin = Zray.prototype.missingfrom = Zray.prototype.whichvaluesarenotin = Zray.prototype.filternotin = function (
- arr
- ) {
- //filters from arr1 that is found in arr2
- const s = new Set(arr);
- return this.filter((x) => !s.has(x));
- };
- Zray.prototype.removethatareingreedy = Zray.prototype.removeallthatareingreedy = function (
- arr
- ) {
- arr.map((x) => this.removeall(x));
- return this;
- };
- Zray.prototype.missingthatsin = Zray.prototype.whichvaluesaremissingfromthisbutarein = function (
- arr
- ) {
- const s = new Set(this);
- return arr.filter((x) => !s.has(x));
- };
- Zray.prototype.sub = Zray.prototype.rep = function (
- val,
- substitution = ""
- ) {
- let idx = this.indexOf(val);
- this.splice(idx, 1, substitution);
- return this;
- };
- Zray.prototype.remove = function (val) {
- let idx = this.indexOf(val);
- this.splice(idx, 1);
- return this;
- };
- Zray.prototype.removeall = Zray.prototype.greedyremoveall = Zray.prototype.removeallwhere = Zray.prototype.removeevery = function (
- val
- ) {
- let idx = this.indexOf(val);
- if (idx !== -1) {
- this.splice(idx, 1);
- this.removeall(val);
- }
- return this;
- };
- Zray.prototype.nongreedyfilter = Zray.prototype.nonegreedyfilteroutthatsin = Zray.prototype.filter1by1iterablyfrom = Zray.prototype.filtereachonebyone = Zray.prototype.removefromarray = function (
- arr
- ) {
- let tmp = [...this];
- arr.map((x) => {
- if (x.isin(tmp)) {
- tmp.sub(x, "#REMOVED");
- }
- });
- return tmp.filter((x) => x != "#REMOVED");
- };
- Zray.prototype.allindexeswherevaluesarein = Zray.prototype.removefromlistofindexes = Zray.prototype.removebyindexesnongreedy = Zray.prototype.removebyindexfromarray = function (
- arr
- ) {
- arr.map((x) => this.splice(x,1, "#DELETED"));
- this.removeall("#DELETED");
- return this;
- };
- Zray.prototype.nongreedyremoveiterablyfrom = Zray.prototype.remove1by1iterablyfrom = Zray.prototype.removeiterativelyfromarr = Zray.prototype.removefromarray = function (
- arr
- ) {
- arr.map((x) => {
- if (x.isin(this)) {
- this.sub(x, "#REMOVED");
- }
- });
- this.removeall("#REMOVED");
- return this;
- };
- Zray.prototype.suball = function (val) {
- let idx = this.indexOf(val);
- if (idx !== -1) {
- this.splice(idx, 1, "");
- this.sub(val);
- }
- return this;
- };
- Zray.prototype.samevalues = Zray.prototype.samein = Zray.prototype.filtersame = Zray.prototype.hassame = Zray.prototype.thatsin = Zray.prototype.hasthesamein = function (
- arr
- ) {
- //filters from arr1 that are found in arr2
- const s = new Set(arr);
- return this.filter((x) => s.has(x));
- };
- Zray.prototype.missingeither = Zray.prototype.missingboth = Zray.prototype.missingfromboth = Zray.prototype.alsomissingfrom = Zray.prototype.missinginboth = Zray.prototype.missingalsoin = function (
- arr
- ) {
- //removes from arr1 that is found in arr2
- const s = new Set(arr);
- return this.filter((x) => !s.has(x));
- };
- Zray.prototype.unique = function () {
- return [...new Set(this)];
- };
- Zray.prototype.srt = function (direction = null) {
- if (direction) {
- return this.sort((a, b) => b - a);
- }
- return this.sort((a, b) => a - b);
- };
- Zray.prototype.uniquewith = Zray.prototype.diff = Zray.prototype.diffwith = Zray.prototype.notoccuringineither = Zray.prototype.difference = function (
- arr
- ) {
- return [
- ...new Set([
- ...this.filter((v) => !arr.includes(v)),
- ...arr.filter((v) => !this.includes(v)),
- ]),
- ];
- };
- Zray.prototype.addmissing = Zray.prototype.addunique = Zray.prototype.addmissingfrom = function (
- arr
- ) {
- const s = new Set(this);
- this.push(...arr.filter((x) => !s.has(x)));
- return this;
- };
- Zray.prototype.duplicates = Zray.prototype.dupe = Zray.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;
- };
- Zray.prototype.pos = Zray.prototype.idx = Zray.prototype.position = function (
- v
- ) {
- return Zray.prototype.indexOf.call(this, v);
- };
- Zray.prototype.has = Zray.prototype.contains = function (v) {
- return Zray.prototype.indexOf.call(this, v) !== -1;
- };
- Zray.prototype.srt = function (direction = null) {
- if (direction) {
- return this.sort((a, b) => b - a);
- }
- return this.sort((a, b) => a - b);
- };
- Zray.prototype.countnumberofoccurencesof = function (v) {
- return this.filter(x => x == v).length;
- }
- Zray.prototype.notin = Zray.prototype.missingfrom = Zray.prototype.whichvaluesarenotin = Zray.prototype.filternotin = function(arr) {
- //filters from arr1 that is found in arr2
- const s = new Set(arr);
- return this.filter((x) => !s.has(x));
- }
- var sub = new Zray(1, 2, 3);
- sub.notin([9,5,2,3]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement