Socket
Socket
Sign inDemoInstall

@basementuniverse/commonjs

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@basementuniverse/commonjs - npm Package Compare versions

Comparing version 1.2.6 to 1.2.7

27

common.d.ts

@@ -82,3 +82,3 @@ /// <reference types="typescript" />

/**
* Re-map a number i from range a1->a2 to b1->b2
* Re-map a number i from range a1...a2 to b1...b2
* @param {number} i The number to re-map

@@ -171,3 +171,26 @@ * @param {number} a1

*/
dot(a: Array<number>, b: Array<number>): number
dot(a: Array<number>, b: Array<number>): number,
/**
* Get the factorial of a number
* @param {number} a
* @return {number} a!
*/
factorial(a: number): number,
/**
* Get the number of permutations of r elements from a set of n elements
* @param {number} n
* @param {number} r
* @return {number} nPr
*/
permutation(n: number, r: number): number,
/**
* Get the number of combinations of r elements from a set of n elements
* @param {number} n
* @param {number} r
* @return {number} nCr
*/
combination(n: number, r: number): number
}

@@ -174,0 +197,0 @@

/**
* @overview A library of useful functions
* @author Gordon Larrigan
* @version 1.2.6
* @version 1.2.7
*/

@@ -65,3 +65,3 @@

/**
* Re-map a number i from range a1->a2 to b1->b2
* Re-map a number i from range a1...a2 to b1...b2
* @param {number} i The number to re-map

@@ -182,2 +182,31 @@ * @param {number} a1

/**
* Get the factorial of a number
* @param {number} a
* @return {number} a!
*/
Math.factorial = a => {
let result = 1;
for (let i = 2; i <= a; i++) {
result *= i;
}
return result;
};
/**
* Get the number of permutations of r elements from a set of n elements
* @param {number} n
* @param {number} r
* @return {number} nPr
*/
Math.permutation = (n, r) => Math.factorial(n) / Math.factorial(n - r);
/**
* Get the number of combinations of r elements from a set of n elements
* @param {number} n
* @param {number} r
* @return {number} nCr
*/
Math.combination = (n, r) => Math.factorial(n) / (Math.factorial(r) * Math.factorial(n - r));
/** @class Array */

@@ -184,0 +213,0 @@

2

common.min.js

@@ -1,1 +0,1 @@

Math.floatEquals=(t,e,a=Number.EPSILON)=>Math.abs(t-e)<a,Math.clamp=(t,e=0,a=1)=>t<e?e:t>a?a:t,Math.frac=t=>t>=0?t-Math.floor(t):t-Math.ceil(t),Math.lerp=(t,e,a)=>t+(e-t)*a,Math.unlerp=(t,e,a)=>(a-t)/(e-t),Math.blerp=(t,e,a,r,n,m)=>Math.lerp(Math.lerp(t,e,n),Math.lerp(a,r,n),m),Math.remap=(t,e,a,r,n)=>r+(t-e)*(n-r)/(a-e),Math.smoothstep=(t,e,a)=>Math.lerp(t,e,3*Math.pow(a,2)-2*Math.pow(a,3)),Math.radians=t=>Math.PI/180*t,Math.degrees=t=>180/Math.PI*t,Math.randomBetween=(t,e)=>Math.random()*(e-t)+t,Math.randomIntBetween=(t,e)=>Math.floor(Math.random()*(e-t+1))+t,Math.cltRandom=(t=.5,e=.5,a=2)=>{let r=0;for(let t=a;t--;)r+=Math.random();return t+(r-a/2)/(a/2)*e},Math.cltRandomInt=(t,e)=>Math.floor(t+Math.cltRandom(.5,.5,2)*(e+1-t)),Math.weightedRandom=t=>{let e=t.reduce(((t,e)=>t+e),0),a=0;const r=Math.random()*e;for(;e>r;)e-=t[a++];return a-1},Math.lerpArray=(t,e,a=Math.lerp)=>{const r=e*(t.length-1),n=Math.clamp(Math.trunc(r),0,t.length-1);return a(t[n]||0,t[n+1]||0,Math.frac(r))},Math.dot=(t,e)=>t.reduce(((t,a,r)=>t+a*e[r]),0),Array.times=(t,e)=>Array(e).fill(0).map(((e,a)=>t(a))),Array.range=t=>Array.times((t=>t),t),Object.defineProperty(Array.prototype,"at",{value:function(t){const e=this.length;return this[t<0?e-Math.abs(t+1)%e-1:t%e]}}),Object.defineProperty(Array.prototype,"chunk",{value:function(t){return Array.times((e=>this.slice(e*t,e*t+t)),Math.ceil(this.length/t))}}),Object.defineProperty(Array.prototype,"shuffle",{value:function(){return this.map((t=>[Math.random(),t])).sort(((t,e)=>t[0]-e[0])).map((t=>t[1]))}});const vec=(t,e)=>t||e?"object"==typeof t?{x:t.x||0,y:t.y||0}:null==e?{x:t,y:t}:{x:t,y:e}:{x:0,y:0};vec.components=t=>[t.x,t.y],vec.ux=()=>vec(1,0),vec.uy=()=>vec(0,1),vec.add=(t,e)=>({x:t.x+e.x,y:t.y+e.y}),vec.mul=(t,e)=>({x:t.x*e,y:t.y*e}),vec.sub=(t,e)=>({x:t.x-e.x,y:t.y-e.y}),vec.len=t=>Math.sqrt(t.x*t.x+t.y*t.y),vec.manhattan=t=>Math.abs(t.x)+Math.abs(t.y),vec.nor=t=>{let e=vec.len(t);return e?{x:t.x/e,y:t.y/e}:vec()},vec.dot=(t,e)=>t.x*e.x+t.y*e.y,vec.rot=(t,e)=>{let a=Math.sin(e),r=Math.cos(e);return{x:r*t.x-a*t.y,y:a*t.x+r*t.y}},vec.eq=(t,e)=>t.x===e.x&&t.y===e.y,vec.rad=t=>Math.atan2(t.y,t.x),vec.cpy=t=>vec(t),vec.map=(t,e)=>({x:e(t.x,"x"),y:e(t.y,"y")}),vec.str=(t,e=", ")=>`${t.x}${e}${t.y}`;const mat=(t=4,e=4,a=[])=>({m:t,n:e,entries:a.concat(Array(t*e).fill(0)).slice(0,t*e)});mat.identity=t=>mat(t,t,Array(t*t).fill(0).map(((e,a)=>+(Math.floor(a/t)===a%t)))),mat.get=(t,e,a)=>t.entries[a-1+(e-1)*t.n],mat.set=(t,e,a,r)=>{t.entries[a-1+(e-1)*t.n]=r},mat.row=(t,e)=>{const a=(e-1)*t.n;return t.entries.slice(a,a+t.n)},mat.col=(t,e)=>Array.times((a=>mat.get(t,a+1,e)),t.m),mat.add=(t,e)=>t.m===e.m&&t.n===e.n&&mat.map(t,((t,a)=>t+e.entries[a])),mat.sub=(t,e)=>t.m===e.m&&t.n===e.n&&mat.map(t,((t,a)=>t-e.entries[a])),mat.mul=(t,e)=>{if(t.n!==e.m)return!1;const a=mat(t.m,e.n);for(let r=1;r<=t.m;r++)for(let n=1;n<=e.n;n++)mat.set(a,r,n,Math.dot(mat.row(t,r),mat.col(e,n)));return a},mat.scale=(t,e)=>mat.map(t,(t=>t*e)),mat.trans=t=>mat(t.n,t.m,Array.times((e=>mat.col(t,e+1)),t.n).flat()),mat.minor=(t,e,a)=>{if(t.m!==t.n)return!1;const r=[];for(let n=1;n<=t.m;n++)if(n!==e)for(let e=1;e<=t.n;e++)e!==a&&r.push(mat.get(t,n,e));return mat(t.m-1,t.n-1,r)},mat.det=t=>{if(t.m!==t.n)return!1;if(1===t.m)return t.entries[0];if(2===t.m)return t.entries[0]*t.entries[3]-t.entries[1]*t.entries[2];let e=0,a=1;for(let r=1;r<=t.n;r++)e+=a*t.entries[r-1]*mat.det(mat.minor(t,1,r)),a*=-1;return e},mat.nor=t=>{if(t.m!==t.n)return!1;const e=mat.det(t);return mat.map(t,(t=>t*e))},mat.adj=t=>{const e=mat(t.m,t.n);for(let a=1;a<=t.m;a++)for(let r=1;r<=t.n;r++)mat.set(e,a,r,mat.det(mat.minor(t,a,r)));const a=mat.map(e,((t,e)=>t*(e%2?-1:1)));return mat.trans(a)},mat.inv=t=>{if(t.m!==t.n)return!1;const e=mat.det(t);return 0!==e&&mat.scale(mat.adj(t),1/e)},mat.eq=(t,e)=>t.m===e.m&&t.n===e.n&&mat.str(t)===mat.str(e),mat.cpy=t=>mat(t.m,t.n,[...t.entries]),mat.map=(t,e)=>mat(t.m,t.n,t.entries.map(e)),mat.str=(t,e=", ",a="\n")=>t.entries.chunk(t.n).map((t=>t.join(e))).join(a),"undefined"!=typeof module&&(module.exports={vec:vec,mat:mat});
Math.floatEquals=(t,a,e=Number.EPSILON)=>Math.abs(t-a)<e,Math.clamp=(t,a=0,e=1)=>t<a?a:t>e?e:t,Math.frac=t=>t>=0?t-Math.floor(t):t-Math.ceil(t),Math.lerp=(t,a,e)=>t+(a-t)*e,Math.unlerp=(t,a,e)=>(e-t)/(a-t),Math.blerp=(t,a,e,r,n,m)=>Math.lerp(Math.lerp(t,a,n),Math.lerp(e,r,n),m),Math.remap=(t,a,e,r,n)=>r+(t-a)*(n-r)/(e-a),Math.smoothstep=(t,a,e)=>Math.lerp(t,a,3*Math.pow(e,2)-2*Math.pow(e,3)),Math.radians=t=>Math.PI/180*t,Math.degrees=t=>180/Math.PI*t,Math.randomBetween=(t,a)=>Math.random()*(a-t)+t,Math.randomIntBetween=(t,a)=>Math.floor(Math.random()*(a-t+1))+t,Math.cltRandom=(t=.5,a=.5,e=2)=>{let r=0;for(let t=e;t--;)r+=Math.random();return t+(r-e/2)/(e/2)*a},Math.cltRandomInt=(t,a)=>Math.floor(t+Math.cltRandom(.5,.5,2)*(a+1-t)),Math.weightedRandom=t=>{let a=t.reduce(((t,a)=>t+a),0),e=0;const r=Math.random()*a;for(;a>r;)a-=t[e++];return e-1},Math.lerpArray=(t,a,e=Math.lerp)=>{const r=a*(t.length-1),n=Math.clamp(Math.trunc(r),0,t.length-1);return e(t[n]||0,t[n+1]||0,Math.frac(r))},Math.dot=(t,a)=>t.reduce(((t,e,r)=>t+e*a[r]),0),Math.factorial=t=>{let a=1;for(let e=2;e<=t;e++)a*=e;return a},Math.permutation=(t,a)=>Math.factorial(t)/Math.factorial(t-a),Math.combination=(t,a)=>Math.factorial(t)/(Math.factorial(a)*Math.factorial(t-a)),Array.times=(t,a)=>Array(a).fill(0).map(((a,e)=>t(e))),Array.range=t=>Array.times((t=>t),t),Object.defineProperty(Array.prototype,"at",{value:function(t){const a=this.length;return this[t<0?a-Math.abs(t+1)%a-1:t%a]}}),Object.defineProperty(Array.prototype,"chunk",{value:function(t){return Array.times((a=>this.slice(a*t,a*t+t)),Math.ceil(this.length/t))}}),Object.defineProperty(Array.prototype,"shuffle",{value:function(){return this.map((t=>[Math.random(),t])).sort(((t,a)=>t[0]-a[0])).map((t=>t[1]))}});const vec=(t,a)=>t||a?"object"==typeof t?{x:t.x||0,y:t.y||0}:null==a?{x:t,y:t}:{x:t,y:a}:{x:0,y:0};vec.components=t=>[t.x,t.y],vec.ux=()=>vec(1,0),vec.uy=()=>vec(0,1),vec.add=(t,a)=>({x:t.x+a.x,y:t.y+a.y}),vec.mul=(t,a)=>({x:t.x*a,y:t.y*a}),vec.sub=(t,a)=>({x:t.x-a.x,y:t.y-a.y}),vec.len=t=>Math.sqrt(t.x*t.x+t.y*t.y),vec.manhattan=t=>Math.abs(t.x)+Math.abs(t.y),vec.nor=t=>{let a=vec.len(t);return a?{x:t.x/a,y:t.y/a}:vec()},vec.dot=(t,a)=>t.x*a.x+t.y*a.y,vec.rot=(t,a)=>{let e=Math.sin(a),r=Math.cos(a);return{x:r*t.x-e*t.y,y:e*t.x+r*t.y}},vec.eq=(t,a)=>t.x===a.x&&t.y===a.y,vec.rad=t=>Math.atan2(t.y,t.x),vec.cpy=t=>vec(t),vec.map=(t,a)=>({x:a(t.x,"x"),y:a(t.y,"y")}),vec.str=(t,a=", ")=>`${t.x}${a}${t.y}`;const mat=(t=4,a=4,e=[])=>({m:t,n:a,entries:e.concat(Array(t*a).fill(0)).slice(0,t*a)});mat.identity=t=>mat(t,t,Array(t*t).fill(0).map(((a,e)=>+(Math.floor(e/t)===e%t)))),mat.get=(t,a,e)=>t.entries[e-1+(a-1)*t.n],mat.set=(t,a,e,r)=>{t.entries[e-1+(a-1)*t.n]=r},mat.row=(t,a)=>{const e=(a-1)*t.n;return t.entries.slice(e,e+t.n)},mat.col=(t,a)=>Array.times((e=>mat.get(t,e+1,a)),t.m),mat.add=(t,a)=>t.m===a.m&&t.n===a.n&&mat.map(t,((t,e)=>t+a.entries[e])),mat.sub=(t,a)=>t.m===a.m&&t.n===a.n&&mat.map(t,((t,e)=>t-a.entries[e])),mat.mul=(t,a)=>{if(t.n!==a.m)return!1;const e=mat(t.m,a.n);for(let r=1;r<=t.m;r++)for(let n=1;n<=a.n;n++)mat.set(e,r,n,Math.dot(mat.row(t,r),mat.col(a,n)));return e},mat.scale=(t,a)=>mat.map(t,(t=>t*a)),mat.trans=t=>mat(t.n,t.m,Array.times((a=>mat.col(t,a+1)),t.n).flat()),mat.minor=(t,a,e)=>{if(t.m!==t.n)return!1;const r=[];for(let n=1;n<=t.m;n++)if(n!==a)for(let a=1;a<=t.n;a++)a!==e&&r.push(mat.get(t,n,a));return mat(t.m-1,t.n-1,r)},mat.det=t=>{if(t.m!==t.n)return!1;if(1===t.m)return t.entries[0];if(2===t.m)return t.entries[0]*t.entries[3]-t.entries[1]*t.entries[2];let a=0,e=1;for(let r=1;r<=t.n;r++)a+=e*t.entries[r-1]*mat.det(mat.minor(t,1,r)),e*=-1;return a},mat.nor=t=>{if(t.m!==t.n)return!1;const a=mat.det(t);return mat.map(t,(t=>t*a))},mat.adj=t=>{const a=mat(t.m,t.n);for(let e=1;e<=t.m;e++)for(let r=1;r<=t.n;r++)mat.set(a,e,r,mat.det(mat.minor(t,e,r)));const e=mat.map(a,((t,a)=>t*(a%2?-1:1)));return mat.trans(e)},mat.inv=t=>{if(t.m!==t.n)return!1;const a=mat.det(t);return 0!==a&&mat.scale(mat.adj(t),1/a)},mat.eq=(t,a)=>t.m===a.m&&t.n===a.n&&mat.str(t)===mat.str(a),mat.cpy=t=>mat(t.m,t.n,[...t.entries]),mat.map=(t,a)=>mat(t.m,t.n,t.entries.map(a)),mat.str=(t,a=", ",e="\n")=>t.entries.chunk(t.n).map((t=>t.join(a))).join(e),"undefined"!=typeof module&&(module.exports={vec:vec,mat:mat});
{
"name": "@basementuniverse/commonjs",
"version": "1.2.6",
"version": "1.2.7",
"description": "A collection of useful functions",

@@ -5,0 +5,0 @@ "main": "common.js",

@@ -98,2 +98,5 @@ # common.js

* [.dot(a, b)](#Math.dot) ⇒ <code>number</code>
* [.factorial(a)](#Math.factorial) ⇒ <code>number</code>
* [.permutation(n, r)](#Math.permutation) ⇒ <code>number</code>
* [.combination(n, r)](#Math.combination) ⇒ <code>number</code>

@@ -188,3 +191,3 @@ <a name="Math.floatEquals"></a>

### Math.remap(i, a1, a2, b1, b2) ⇒ <code>number</code>
Re-map a number i from range a1->a2 to b1->b2
Re-map a number i from range a1...a2 to b1...b2

@@ -331,2 +334,40 @@ **Kind**: static method of [<code>Math</code>](#Math)

<a name="Math.factorial"></a>
### Math.factorial(a) ⇒ <code>number</code>
Get the factorial of a number
**Kind**: static method of [<code>Math</code>](#Math)
**Returns**: <code>number</code> - a!
| Param | Type |
| --- | --- |
| a | <code>number</code> |
<a name="Math.permutation"></a>
### Math.permutation(n, r) ⇒ <code>number</code>
Get the number of permutations of r elements from a set of n elements
**Kind**: static method of [<code>Math</code>](#Math)
**Returns**: <code>number</code> - nPr
| Param | Type |
| --- | --- |
| n | <code>number</code> |
| r | <code>number</code> |
<a name="Math.combination"></a>
### Math.combination(n, r) ⇒ <code>number</code>
Get the number of combinations of r elements from a set of n elements
**Kind**: static method of [<code>Math</code>](#Math)
**Returns**: <code>number</code> - nCr
| Param | Type |
| --- | --- |
| n | <code>number</code> |
| r | <code>number</code> |
<a name="Array"></a>

@@ -333,0 +374,0 @@

@@ -47,4 +47,4 @@ require('../common.js');

QUnit.test('Shuffle an array', assert => {
const a = Array.range(10).shuffle();
const a = Array.range(100).shuffle();
assert.notEqual(a[0], 0);
});

@@ -202,2 +202,6 @@ const { mat } = require('../common.js');

assert.equal(mat.det(a), -306);
// Determinant of a non-square matrix
const b = mat(2, 3, [1, 2, 3, 4, 5, 6]);
assert.equal(mat.det(b), false);
});

@@ -216,2 +220,6 @@

});
// Normalise a non-square matrix
const b = mat(2, 3, [1, 2, 3, 4, 5, 6]);
assert.equal(mat.nor(b), false);
});

@@ -237,2 +245,10 @@

}
// Invert a non-square matrix
const b = mat(2, 3, [1, 2, 3, 4, 5, 6]);
assert.equal(mat.inv(b), false);
// Invert a singular/degenerate matrix
const c = mat(2, 2, [3, 4, 6, 8]);
assert.equal(mat.inv(c), false);
});

@@ -239,0 +255,0 @@

@@ -5,3 +5,3 @@ require('../common.js');

QUnit.test('Appromixate value equality', assert => {
QUnit.test('Approximate value equality', assert => {
assert.equal(Math.floatEquals(1 / 3, 0.3333333333333333), true);

@@ -105,1 +105,20 @@ });

});
QUnit.test('Factorial of a number', assert => {
const factorials = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800];
for (let i = 0; i < factorials.length; i++) {
assert.equal(Math.factorial(i), factorials[i]);
}
});
QUnit.test('Combinations', assert => {
assert.equal(Math.combination(3, 1), 3);
assert.equal(Math.combination(3, 3), 1);
assert.equal(Math.combination(10, 3), 120);
});
QUnit.test('Permutations', assert => {
assert.equal(Math.permutation(3, 1), 3);
assert.equal(Math.permutation(3, 3), 6);
assert.equal(Math.permutation(10, 3), 720);
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc