Advertisement
pharmokan

zray

Apr 17th, 2020
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. class Zray extends Array {
  2. constructor(...args) {
  3. super(...args);
  4. }
  5. }
  6. Zray.prototype.sum = function () {
  7. var total = 0;
  8. for (var i = 0; i < this.length; i++) {
  9. total += this[i];
  10. }
  11. return total;
  12. };
  13.  
  14. Zray.prototype.first = function () {
  15. return this[0];
  16. };
  17. Zray.prototype.last = function () {
  18. return this[this.length - 1];
  19. };
  20.  
  21. Zray.prototype.average = function () {
  22. return this.sum() / this.length;
  23. };
  24.  
  25. Zray.prototype.range = function () {
  26. var self = this.sort();
  27. return {
  28. min: self[0],
  29. max: self[this.length - 1],
  30. };
  31. };
  32.  
  33. Zray.prototype.isempty = function () {
  34. return this.length == 0;
  35. };
  36.  
  37. Zray.prototype.max = function () {
  38. return Math.max(...this);
  39. };
  40.  
  41. Zray.prototype.min = function () {
  42. return Math.min(...this);
  43. };
  44.  
  45. Zray.prototype.notin = Zray.prototype.missingfrom = Zray.prototype.whichvaluesarenotin = Zray.prototype.filternotin = function (
  46. arr
  47. ) {
  48. //filters from arr1 that is found in arr2
  49. const s = new Set(arr);
  50. return this.filter((x) => !s.has(x));
  51. };
  52.  
  53. Zray.prototype.removethatareingreedy = Zray.prototype.removeallthatareingreedy = function (
  54. arr
  55. ) {
  56. arr.map((x) => this.removeall(x));
  57. return this;
  58. };
  59.  
  60. Zray.prototype.missingthatsin = Zray.prototype.whichvaluesaremissingfromthisbutarein = function (
  61. arr
  62. ) {
  63. const s = new Set(this);
  64. return arr.filter((x) => !s.has(x));
  65. };
  66.  
  67. Zray.prototype.sub = Zray.prototype.rep = function (
  68. val,
  69. substitution = ""
  70. ) {
  71. let idx = this.indexOf(val);
  72. this.splice(idx, 1, substitution);
  73. return this;
  74. };
  75.  
  76. Zray.prototype.remove = function (val) {
  77. let idx = this.indexOf(val);
  78. this.splice(idx, 1);
  79. return this;
  80. };
  81.  
  82. Zray.prototype.removeall = Zray.prototype.greedyremoveall = Zray.prototype.removeallwhere = Zray.prototype.removeevery = function (
  83. val
  84. ) {
  85. let idx = this.indexOf(val);
  86. if (idx !== -1) {
  87. this.splice(idx, 1);
  88. this.removeall(val);
  89. }
  90. return this;
  91. };
  92. Zray.prototype.nongreedyfilter = Zray.prototype.nonegreedyfilteroutthatsin = Zray.prototype.filter1by1iterablyfrom = Zray.prototype.filtereachonebyone = Zray.prototype.removefromarray = function (
  93. arr
  94. ) {
  95. let tmp = [...this];
  96. arr.map((x) => {
  97. if (x.isin(tmp)) {
  98. tmp.sub(x, "#REMOVED");
  99. }
  100. });
  101. return tmp.filter((x) => x != "#REMOVED");
  102. };
  103.  
  104. Zray.prototype.allindexeswherevaluesarein = Zray.prototype.removefromlistofindexes = Zray.prototype.removebyindexesnongreedy = Zray.prototype.removebyindexfromarray = function (
  105. arr
  106. ) {
  107. arr.map((x) => this.splice(x,1, "#DELETED"));
  108. this.removeall("#DELETED");
  109. return this;
  110. };
  111.  
  112. Zray.prototype.nongreedyremoveiterablyfrom = Zray.prototype.remove1by1iterablyfrom = Zray.prototype.removeiterativelyfromarr = Zray.prototype.removefromarray = function (
  113. arr
  114. ) {
  115. arr.map((x) => {
  116. if (x.isin(this)) {
  117. this.sub(x, "#REMOVED");
  118. }
  119. });
  120. this.removeall("#REMOVED");
  121. return this;
  122. };
  123. Zray.prototype.suball = function (val) {
  124. let idx = this.indexOf(val);
  125. if (idx !== -1) {
  126. this.splice(idx, 1, "");
  127. this.sub(val);
  128. }
  129. return this;
  130. };
  131.  
  132. Zray.prototype.samevalues = Zray.prototype.samein = Zray.prototype.filtersame = Zray.prototype.hassame = Zray.prototype.thatsin = Zray.prototype.hasthesamein = function (
  133. arr
  134. ) {
  135. //filters from arr1 that are found in arr2
  136. const s = new Set(arr);
  137. return this.filter((x) => s.has(x));
  138. };
  139.  
  140. Zray.prototype.missingeither = Zray.prototype.missingboth = Zray.prototype.missingfromboth = Zray.prototype.alsomissingfrom = Zray.prototype.missinginboth = Zray.prototype.missingalsoin = function (
  141. arr
  142. ) {
  143. //removes from arr1 that is found in arr2
  144. const s = new Set(arr);
  145. return this.filter((x) => !s.has(x));
  146. };
  147.  
  148. Zray.prototype.unique = function () {
  149. return [...new Set(this)];
  150. };
  151.  
  152. Zray.prototype.srt = function (direction = null) {
  153. if (direction) {
  154. return this.sort((a, b) => b - a);
  155. }
  156.  
  157. return this.sort((a, b) => a - b);
  158. };
  159.  
  160. Zray.prototype.uniquewith = Zray.prototype.diff = Zray.prototype.diffwith = Zray.prototype.notoccuringineither = Zray.prototype.difference = function (
  161. arr
  162. ) {
  163. return [
  164. ...new Set([
  165. ...this.filter((v) => !arr.includes(v)),
  166. ...arr.filter((v) => !this.includes(v)),
  167. ]),
  168. ];
  169. };
  170.  
  171. Zray.prototype.addmissing = Zray.prototype.addunique = Zray.prototype.addmissingfrom = function (
  172. arr
  173. ) {
  174. const s = new Set(this);
  175. this.push(...arr.filter((x) => !s.has(x)));
  176. return this;
  177. };
  178.  
  179. Zray.prototype.duplicates = Zray.prototype.dupe = Zray.prototype.findduplicates = function () {
  180. let duplicates = this.reduce(function (acc, el, i, arr) {
  181. if (arr.indexOf(el) !== i && acc.indexOf(el) < 0) acc.push(el);
  182. return acc;
  183. }, []);
  184. return duplicates;
  185. };
  186.  
  187. Zray.prototype.pos = Zray.prototype.idx = Zray.prototype.position = function (
  188. v
  189. ) {
  190. return Zray.prototype.indexOf.call(this, v);
  191. };
  192.  
  193. Zray.prototype.has = Zray.prototype.contains = function (v) {
  194. return Zray.prototype.indexOf.call(this, v) !== -1;
  195. };
  196.  
  197. Zray.prototype.srt = function (direction = null) {
  198. if (direction) {
  199. return this.sort((a, b) => b - a);
  200. }
  201. return this.sort((a, b) => a - b);
  202. };
  203.  
  204. Zray.prototype.countnumberofoccurencesof = function (v) {
  205. return this.filter(x => x == v).length;
  206. }
  207.  
  208.  
  209. Zray.prototype.notin = Zray.prototype.missingfrom = Zray.prototype.whichvaluesarenotin = Zray.prototype.filternotin = function(arr) {
  210. //filters from arr1 that is found in arr2
  211. const s = new Set(arr);
  212. return this.filter((x) => !s.has(x));
  213. }
  214.  
  215. var sub = new Zray(1, 2, 3);
  216. sub.notin([9,5,2,3]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement