-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswoop.min.js
1 lines (1 loc) · 4.68 KB
/
swoop.min.js
1
const SWOOP={ARR:{union:function(t,e){return[...new Set([...t,...e])]},intersection:function(t,e){return t.filter((t=>e.includes(t)))},difference:function(t,e){const n=t.filter((t=>!e.includes(t))),r=e.filter((e=>!t.includes(e)));return n.concat(r)},sum:function(t){let e=0;for(var n=0;n<t.length;n++)e+=t[n];return e},mean:function(t){const e=t.length;return SWOOP.ARR.sum(t)/e},standard_deviation:function(t){const e=SWOOP.ARR.mean(t),n=t.length-1,r=t.map((t=>(t-e)**2)).reduce(((t,e)=>t+e),0)/n;return Math.sqrt(r)},variance:function(t){const e=SWOOP.ARR.mean(t),n=t.length-1;return t.map((t=>(t-e)**2)).reduce(((t,e)=>t+e),0)/n},median:function(t){t.sort(((t,e)=>t-e));const e=Math.floor(t.length/2);return t.length%2==0?t[e-1]+t[e]:t[e]},min:function(t){let e=t[0];for(var n=0;n<t.length;n++)t[n]<e&&(e=t[n]);return e},max:function(t){let e=t[0];for(var n=0;n<t.length;n++)e<t[n]&&(e=t[n]);return e},countUnique:function(t){return t.filter(((t,e,n)=>n.indexOf(t)===e)).length},countDuplicates:function(t){return t.filter(((e,n,r)=>t.indexOf(e)!==n)).length},merge:function(t,e){return[...t,...e]},freq:function(t){const e={};for(const n of t)e[n]=(e[n]||0)+1;return e}},DOM:{select:function(t){const e=document.querySelector(t);if(!e)throw new Error(`Element not found: ${t}`);return e},selectid:function(t){const e=document.getElementById(t);if(!e)throw new Error(`Element not found: ${t}`);return e},selectall:function(t){const e=document.querySelectorAll(t);if(!e.length)throw new Error(`Element not found: ${t}`);return e},addListener:function(t,e,n){return t.addEventListener(e,n)},removeListener:function(t,e,n){return t.removeEventListener(e,n)}},FUNC:{throttle:function(t,e){let n;return function(){const r=this,o=arguments;n||(n=setTimeout((()=>{n=null,t.apply(r,o)}),e))}},debounce:function(t,e){let n;return function(){const r=this,o=arguments;clearTimeout(n),n=setTimeout((()=>{t.apply(r,o)}),e)}},memoize:function(t){const e=new Map;return function(...n){const r=JSON.stringify(n);if(e.has(r))return e.get(r);const o=t(...n);return e.set(r,o),o}},curry:function(t){return function e(...n){return n.length>=t.length?t(...n):function(...t){return e(...n.concat(t))}}},once:function(t){return function(...e){return t(...e)}}},STR:{strip:function(t){return t.replace(/\s/g,"")},contains:function(t,e,n=0){return-1!==t.indexOf(e,n)},isBlank:function(t){return""===SWOOP.STR.strip(t)},isNotBlank:function(t){return""!==SWOOP.STR.strip(t)},isAlphabet:function(t){return/^\D+$/.test(t)},isNumeric:function(t){return/^\d+$/.test(t)},echo:function(t,e){for(var n=0;n<e;n++)console.log(t.toString())},capitalise:function(t){return replace(/^./,(t=>t.toUpperCase()))}},MATHS:{sin:t=>Math.sin(t),cos:t=>Math.cos(t),tan:t=>Math.tan(t),distance:(t,e,n,r)=>Math.sqrt((e-t)**2+(r+n)**2),midpoint:(t,e,n,r)=>[(t+e)/2,(n+r)/2],lerp:(t,e,n)=>t+(t-e)*n,clamp:(t,e,n)=>Math.min(Math.max(t,e),n),signum:t=>0===t?0:t>=0?1:-1,sqrt:t=>Math.sqrt(t),round:t=>Math.round(t),ceil:t=>Math.ceil(t),absolute:t=>Math.abs(t),gcd:(t,e)=>0===e?t:SWOOP.MATHS.gcd(e,t%e),lcm:(t,e)=>SWOOP.MATHS.absolute(t*e)/SWOOP.MATHS.gcd(t,e),hypot:(t,e)=>Math.sqrt(t*t+e*e),randnum:(t,e)=>Math.floor(Math.random()*(e-t+1)+t)}};class Swoophysix{constructor(t=9.81){this.gravity=t}convertFtoC(t){return 5*(t-32)/9}convertCtoF(t){return 9*t/5+32}convertCtoK(t){return t+273.15}convertKtoC(t){return t-273.15}convertFtoK(t){return 5*(t-32)/9+273.15}convertKtoF(t){return 9*(t-273.15)/5+32}convertMtoFT(t){return 3.28084*t}convertFTtoM(t){return t*(1/3.28084)}convertINtoCM(t){return 2.54*t}convertCMtoIN(t){return t/2.54}convertMtoIN(t){return t/.0254}convertINtoM(t){return.0254*t}convertCMtoM(t){return t/100}convertMtoCM(t){return 100*t}force(t,e){return t*e}pressure(t,e){return t/e}energy(t,e){return.5*t*e**2}momentum(t,e){return t*e}liquid_pressure(t,e,n){return t*e*this.gravity*n}friction(t,e){return t*e}acceleration(t,e){return t*e}potential_energy(t,e){return t*this.gravity*e}torque(t,e){return t*e}}class SwoopTest{constructor(){this.tests=[]}addTest(t,e){this.tests.push({name:t,fn:e})}runTests(){this.tests.forEach((t=>{try{t.fn(),console.log(`passed ${t.name} ✓`)}catch(e){console.log(`Failed:${t.name}-${e.message}`)}}))}assertEqual(t,e){if(t!==e)throw new Error(`expected ${e} but got ${t}`)}assertNotEqual(t,e,n){if(t===e)throw new Error(`expected ${e} but got ${t}`)}assertGreaterThan(t,e,n){if(t<=e)throw new Error(`expected ${e} but got ${t}`)}assertLessThan(t,e,n){if(t>=e)throw new Error(n)}}class SwoopCache{constructor(t=6e4){this.cache={},this.TTL=t}setCacheData(t,e){this.cache[t]={value:e,expires:Date.now()+this.TTL}}getCachedData(t){const e=this.cache[t];return!e||e.expires<Date.now()?(delete this.cache[t],null):e.value}deleteCachedData(t){delete this.cache[t]}clearCachedData(){this.cache={}}}