Advertisement
pharmokan

javascript prototype overhaul

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