Guest User

Untitled

a guest
Nov 18th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. const V = {
  2. hypo: (a, b) => Math.sqrt(a*a + b*b),
  3. circumference: r => r*2*Math.PI,
  4. area: r => r*r*Math.PI,
  5.  
  6. prime: n => {
  7. if ((n < 2 || n%2 === 0) && n !== 2) { return false; }
  8. for (let i = 3; i < n/2+1; i++) { if (n%i === 0) { return false; } }
  9. return true;
  10. },
  11.  
  12. isObject: val => (typeof val === 'object' || val === Object) && val !== null && !Array.isArray(val),
  13.  
  14. newArray: (length, func) => {
  15. newArr = new Array(length).fill(null);
  16. if (typeof func === 'function') { newArr = newArr.map((x, y, z) => func(x, y, z)); } else { newArr.fill(undefined); }
  17. return newArr;
  18. },
  19. range: (length, start = 0, n = 1) => new Array(length).fill(null).map(($, i, arr) => start+i*n),
  20. once: arr => Array.from(new Set(arr)),
  21. unique: arr => {
  22. if (!Array.isArray(arr)) { return null; }
  23. let chart = V.amountOf(arr);
  24. let newArr = [];
  25. for (let element of arr) { if (chart[element] === 1) { newArr.push(element); } }
  26. return newArr;
  27. },
  28. nonUnique: (arr, once = false) => {
  29. if (!Array.isArray(arr)) { return null; }
  30. let chart = V.amountOf(arr);
  31. let newArr = [];
  32. for (let element of arr) { if (chart[element] !== 1 && (once ? !newArr.includes(element) : true)) { newArr.push(element); } }
  33. return newArr;
  34. },
  35.  
  36. amountOf: val => {
  37. let chart = {};
  38. if (Array.isArray(val)) {
  39. for (let element of val) {
  40. chart[element] = chart[element] === undefined ? 1 : chart[element] + 1;
  41. }
  42. }
  43. return chart;
  44. },
  45.  
  46. repeat: (times, func) => { for (let i = 0; i < times; i++) { func(i); } },
  47.  
  48. grid: (w, h) => V.myGrid(w, h, (x, y) => new V.vector(x, y)),
  49. grid3D: (w, l, h) => V.myGrid3D(w, l, h, (x, y, z) => new V.vector(x, y, z)),
  50. myGrid: (w, h, func) => V.newArray(w, ($, x) => V.newArray(h, ($, y) => func(x, y))),
  51. myGrid3D: (w, l, h, func) => V.newArray(w, ($, x) => V.newArray(l, ($, y) => V.newArray(h, ($, z) => func(x, y, z)))),
  52.  
  53. randomInt: (min = 0, max = 1, exception = null) => {
  54. if (!Array.isArray(exception)) { exception = [exception]; }
  55. let n = Math.floor(Math.random()*(max-min+1))+min;
  56. return exception.includes(n) ? V.randomInt(min, max, exception) : n;
  57. },
  58. randomFloat: (min = 0, max = 1, exception = null) => {
  59. if (!Array.isArray(exception)) { exception = [exception]; }
  60. let n = Math.random()*(max-min)+min;
  61. return exception.includes(n) ? V.randomFloat(min, max, exception) : n;
  62. },
  63. randomBoolean: () => V.randomInt() === 1,
  64. randomString: (length = 10, props = {}) => {
  65. if (props.stringLower !== false) { props.stringLower = true; }
  66. if (props.stringUpper !== false) { props.stringUpper = true; }
  67. if (props.numbers !== false) { props.numbers = true; }
  68. if (props.symbols !== false) { props.symbols = true; }
  69. if (props.stringLower === false && props.stringUpper === false && props.numbers === false && props.symbols === false) { return ''; }
  70.  
  71. let string = '';
  72. let charas = {
  73. stringLower: 'qwertyuiopasdfghjklzxcvbnm',
  74. stringUpper: 'QWERTYUIOPASDFGHJKLZXCVBNM',
  75. numbers: '1234567890',
  76. symbols: '!@#$%^&*()`~-_+={}[];:\|/?<>,."'
  77. };
  78.  
  79. for (;;) {
  80. if (string.length >= length) { break; }
  81. let type = V.getRandom(charas);
  82. if (props[type]) { string += V.getRandom(charas[type]); }
  83. }
  84.  
  85. return string;
  86. },
  87. randomStringFrom: (arr, length = 10) => {
  88. let string = '';
  89. if (Array.isArray(arr) && arr.length !== 0) { V.repeat(length, () => string += V.getRandom(arr)); }
  90. return string;
  91. },
  92. getRandom: (foo, repeat = false) => {
  93. if (Array.isArray(foo)) { return foo[Math.floor(Math.random()*foo.length)]; } else
  94. if (V.isObject(foo)) { return Object.keys(foo)[Math.floor(Math.random()*Object.keys(foo).length)]; } else
  95. if (typeof foo === 'string') { return foo[Math.floor(Math.random()*foo.length)]; } else
  96. if (repeat) { return V.getRandom(foo + ''); }
  97. else { return null; }
  98. },
  99.  
  100. flatten: flatter => {
  101. let newArr = [];
  102. let pickOutArr = pickArr => {
  103. for (let element of pickArr) {
  104. if (Array.isArray(element)) { pickOutArr(element); } else
  105. if (V.isObject(element)) { newArr.concat(pickOutObj(element)); }
  106. else { newArr.push(element); }
  107. }
  108. };
  109. let pickOutObj = pickObj => {
  110. for (let prop in pickObj) {
  111. if (Array.isArray(pickObj[prop])) { pickOutArr(pickObj[prop]); } else
  112. if (V.isObject(pickObj[prop])) { pickOutObj(pickObj[prop]); }
  113. else { newArr.push(pickObj[prop]); }
  114. }
  115. };
  116. if (Array.isArray(flatter)) { pickOutArr(flatter); return newArr; } else
  117. if (V.isObject(flatter)) { pickOutObj(flatter); return newArr; }
  118. else { return new Array(1).fill(flatter); }
  119. },
  120.  
  121. vector: function(x, y, z = 0) {
  122. class Vector {
  123. constructor() {
  124. this.x = x;
  125. this.y = y;
  126. this.z = z;
  127. }
  128.  
  129. distFrom(obj) { return V.hypo(Math.abs(this.x-obj.x), Math.abs(this.y-obj.y)); }
  130. change(newX = this.x, newY = this.y, newZ = this.z) { this.x = newX === null ? this.x : newX; this.y = newY === null ? this.y : newY; this.z = newZ === null ? this.z : newZ; }
  131. formulate(formula) { if (typeof formula === 'function') { this.x = formula(this.x); this.y = formula(this.y); this.z = formula(this.z); } }
  132. modifyBy(obj, sign) {
  133. if (['+', '-', '/', '*', '**', '%'].some(element => element === sign)) {
  134. this.x = eval(this.x+sign+obj.x);
  135. this.y = eval(this.y+sign+obj.y);
  136. this.z = eval(this.z+sign+obj.z);
  137. } else { return null; }
  138. }
  139.  
  140. get dist() { return V.hypo(this.x, this.y); }
  141. }
  142.  
  143. return new Vector();
  144. }
  145. };
Add Comment
Please, Sign In to add comment