Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const V = {
- hypo: (a, b) => Math.sqrt(a*a + b*b),
- circumference: r => r*2*Math.PI,
- area: r => r*r*Math.PI,
- prime: n => {
- if ((n < 2 || n%2 === 0) && n !== 2) { return false; }
- for (let i = 3; i < n/2+1; i++) { if (n%i === 0) { return false; } }
- return true;
- },
- isObject: val => (typeof val === 'object' || val === Object) && val !== null && !Array.isArray(val),
- newArray: (length, func) => {
- newArr = new Array(length).fill(null);
- if (typeof func === 'function') { newArr = newArr.map((x, y, z) => func(x, y, z)); } else { newArr.fill(undefined); }
- return newArr;
- },
- range: (length, start = 0, n = 1) => new Array(length).fill(null).map(($, i, arr) => start+i*n),
- once: arr => Array.from(new Set(arr)),
- unique: arr => {
- if (!Array.isArray(arr)) { return null; }
- let chart = V.amountOf(arr);
- let newArr = [];
- for (let element of arr) { if (chart[element] === 1) { newArr.push(element); } }
- return newArr;
- },
- nonUnique: (arr, once = false) => {
- if (!Array.isArray(arr)) { return null; }
- let chart = V.amountOf(arr);
- let newArr = [];
- for (let element of arr) { if (chart[element] !== 1 && (once ? !newArr.includes(element) : true)) { newArr.push(element); } }
- return newArr;
- },
- amountOf: val => {
- let chart = {};
- if (Array.isArray(val)) {
- for (let element of val) {
- chart[element] = chart[element] === undefined ? 1 : chart[element] + 1;
- }
- }
- return chart;
- },
- repeat: (times, func) => { for (let i = 0; i < times; i++) { func(i); } },
- grid: (w, h) => V.myGrid(w, h, (x, y) => new V.vector(x, y)),
- grid3D: (w, l, h) => V.myGrid3D(w, l, h, (x, y, z) => new V.vector(x, y, z)),
- myGrid: (w, h, func) => V.newArray(w, ($, x) => V.newArray(h, ($, y) => func(x, y))),
- myGrid3D: (w, l, h, func) => V.newArray(w, ($, x) => V.newArray(l, ($, y) => V.newArray(h, ($, z) => func(x, y, z)))),
- randomInt: (min = 0, max = 1, exception = null) => {
- if (!Array.isArray(exception)) { exception = [exception]; }
- let n = Math.floor(Math.random()*(max-min+1))+min;
- return exception.includes(n) ? V.randomInt(min, max, exception) : n;
- },
- randomFloat: (min = 0, max = 1, exception = null) => {
- if (!Array.isArray(exception)) { exception = [exception]; }
- let n = Math.random()*(max-min)+min;
- return exception.includes(n) ? V.randomFloat(min, max, exception) : n;
- },
- randomBoolean: () => V.randomInt() === 1,
- randomString: (length = 10, props = {}) => {
- if (props.stringLower !== false) { props.stringLower = true; }
- if (props.stringUpper !== false) { props.stringUpper = true; }
- if (props.numbers !== false) { props.numbers = true; }
- if (props.symbols !== false) { props.symbols = true; }
- if (props.stringLower === false && props.stringUpper === false && props.numbers === false && props.symbols === false) { return ''; }
- let string = '';
- let charas = {
- stringLower: 'qwertyuiopasdfghjklzxcvbnm',
- stringUpper: 'QWERTYUIOPASDFGHJKLZXCVBNM',
- numbers: '1234567890',
- symbols: '!@#$%^&*()`~-_+={}[];:\|/?<>,."'
- };
- for (;;) {
- if (string.length >= length) { break; }
- let type = V.getRandom(charas);
- if (props[type]) { string += V.getRandom(charas[type]); }
- }
- return string;
- },
- randomStringFrom: (arr, length = 10) => {
- let string = '';
- if (Array.isArray(arr) && arr.length !== 0) { V.repeat(length, () => string += V.getRandom(arr)); }
- return string;
- },
- getRandom: (foo, repeat = false) => {
- if (Array.isArray(foo)) { return foo[Math.floor(Math.random()*foo.length)]; } else
- if (V.isObject(foo)) { return Object.keys(foo)[Math.floor(Math.random()*Object.keys(foo).length)]; } else
- if (typeof foo === 'string') { return foo[Math.floor(Math.random()*foo.length)]; } else
- if (repeat) { return V.getRandom(foo + ''); }
- else { return null; }
- },
- flatten: flatter => {
- let newArr = [];
- let pickOutArr = pickArr => {
- for (let element of pickArr) {
- if (Array.isArray(element)) { pickOutArr(element); } else
- if (V.isObject(element)) { newArr.concat(pickOutObj(element)); }
- else { newArr.push(element); }
- }
- };
- let pickOutObj = pickObj => {
- for (let prop in pickObj) {
- if (Array.isArray(pickObj[prop])) { pickOutArr(pickObj[prop]); } else
- if (V.isObject(pickObj[prop])) { pickOutObj(pickObj[prop]); }
- else { newArr.push(pickObj[prop]); }
- }
- };
- if (Array.isArray(flatter)) { pickOutArr(flatter); return newArr; } else
- if (V.isObject(flatter)) { pickOutObj(flatter); return newArr; }
- else { return new Array(1).fill(flatter); }
- },
- vector: function(x, y, z = 0) {
- class Vector {
- constructor() {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- distFrom(obj) { return V.hypo(Math.abs(this.x-obj.x), Math.abs(this.y-obj.y)); }
- 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; }
- formulate(formula) { if (typeof formula === 'function') { this.x = formula(this.x); this.y = formula(this.y); this.z = formula(this.z); } }
- modifyBy(obj, sign) {
- if (['+', '-', '/', '*', '**', '%'].some(element => element === sign)) {
- this.x = eval(this.x+sign+obj.x);
- this.y = eval(this.y+sign+obj.y);
- this.z = eval(this.z+sign+obj.z);
- } else { return null; }
- }
- get dist() { return V.hypo(this.x, this.y); }
- }
- return new Vector();
- }
- };
Add Comment
Please, Sign In to add comment