Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@daeinc/math

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@daeinc/math - npm Package Compare versions

Comparing version 0.5.0 to 0.6.0

39

dist/index.d.ts

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

export declare const PI: number;
export declare const TWO_PI: number;
declare const PI: number;
declare const TWO_PI: number;
/**

@@ -10,7 +10,7 @@ * clamp values between min and max (=constrain)

*/
export declare const clamp: (val: number, min: number, max: number) => number;
declare const clamp: (val: number, min: number, max: number) => number;
/**
* alias for clamp()
*/
export declare const constrain: (val: number, min: number, max: number) => number;
declare const constrain: (val: number, min: number, max: number) => number;
/**

@@ -24,3 +24,3 @@ *

*/
export declare const dist: (x1: number, y1: number, x2: number, y2: number) => number;
declare const dist: (x1: number, y1: number, x2: number, y2: number) => number;
/**

@@ -34,3 +34,3 @@ * returns squared distance

*/
export declare const distSq: (x1: number, y1: number, x2: number, y2: number) => number;
declare const distSq: (x1: number, y1: number, x2: number, y2: number) => number;
/**

@@ -48,3 +48,3 @@ *

*/
export declare const floorF: (n: number, digit: number) => number;
declare const floorF: (n: number, digit: number) => number;
/**

@@ -59,3 +59,3 @@ *

*/
export declare const map: (val: number, s: number, e: number, ns: number, ne: number) => number;
declare const map: (val: number, s: number, e: number, ns: number, ne: number) => number;
/**

@@ -68,7 +68,7 @@ * linear interpolation (=lerp)

*/
export declare const mix: (a: number, b: number, t: number) => number;
declare const mix: (a: number, b: number, t: number) => number;
/**
* alias for mix()
*/
export declare const lerp: (a: number, b: number, t: number) => number;
declare const lerp: (a: number, b: number, t: number) => number;
/**

@@ -80,3 +80,3 @@ * NOTE: it may not be accurate with non-integer numbers.

*/
export declare const mod: (n: number, max: number) => number;
declare const mod: (n: number, max: number) => number;
/**

@@ -89,3 +89,3 @@ * modulo(%) for float numbers up to precision digit.

*/
export declare const modF: (n: number, max: number, precision?: number) => number;
declare const modF: (n: number, max: number, precision?: number) => number;
/**

@@ -98,3 +98,3 @@ * inclusive modulo. modIncl(1, 3) will include 3.

*/
export declare const modIncl: (n: number, max: number) => number;
declare const modIncl: (n: number, max: number) => number;
/**

@@ -106,3 +106,3 @@ * converts polar coordinate to cartesian. to update center, result.map((v,i)=> v + center[i])

*/
export declare const polarToCartesian: (radius: number, angle: number) => number[];
declare const polarToCartesian: (radius: number, angle: number) => number[];
/**

@@ -114,3 +114,3 @@ * reflect a scalar value along axis. good for creating reflected version.

*/
export declare const reflect: (num: number, axis: number) => number;
declare const reflect: (num: number, axis: number) => number;
/**

@@ -122,3 +122,3 @@ * good for drawing shapes to include the maximum value (round up)

*/
export declare const roundF: (n: number, digit: number) => number;
declare const roundF: (n: number, digit: number) => number;
/**

@@ -131,3 +131,3 @@ * snap value to increment

*/
export declare const snapBy: (n: number, inc: number) => number;
declare const snapBy: (n: number, inc: number) => number;
/**

@@ -139,3 +139,4 @@ * snap to a value in array. whatever is closest to.

*/
export declare const snapToArray: (n: number, snapArr: number[]) => number;
//# sourceMappingURL=index.d.ts.map
declare const snapToArray: (n: number, snapArr: number[]) => number;
export { PI, TWO_PI, clamp, constrain, dist, distSq, floorF, lerp, map, mix, mod, modF, modIncl, polarToCartesian, reflect, roundF, snapBy, snapToArray };

@@ -1,179 +0,67 @@

// frequently used constants
export const PI = Math.PI;
export const TWO_PI = Math.PI * 2;
/**
* clamp values between min and max (=constrain)
* @param val
* @param min
* @param max
* @returns
*/
export const clamp = (val, min, max) => Math.min(Math.max(val, min), max);
/**
* alias for clamp()
*/
export const constrain = clamp;
/**
*
* @param x1
* @param y1
* @param x2
* @param y2
* @returns
*/
export const dist = (x1, y1, x2, y2) => {
return Math.sqrt(distSq(x1, y1, x2, y2));
// index.ts
var PI = Math.PI;
var TWO_PI = Math.PI * 2;
var clamp = (val, min, max) => Math.min(Math.max(val, min), max);
var constrain = clamp;
var dist = (x1, y1, x2, y2) => {
return Math.sqrt(distSq(x1, y1, x2, y2));
};
/**
* returns squared distance
* @param x1
* @param y1
* @param x2
* @param y2
* @returns
*/
export const distSq = (x1, y1, x2, y2) => {
return (x2 - x1) ** 2 + (y2 - y1) ** 2;
var distSq = (x1, y1, x2, y2) => {
return (x2 - x1) ** 2 + (y2 - y1) ** 2;
};
/**
*
* @param n number
* @param amp how far it can be away from input
* @returns
*/
// export const shakeNum = (n: number, amp: number): number =>
// n + map(Math.random(), 0, 1, -1, 1) * amp;
/**
* drop decimals after digit. good for array index (floor)
* @param n float number
* @param digit how many decimal digits to keep
* @returns
*/
export const floorF = (n, digit) => {
n = parseFloat(n.toFixed(digit));
const factor = Math.pow(10, digit);
return Math.floor(n * factor) / factor;
var floorF = (n, digit) => {
n = parseFloat(n.toFixed(digit));
const factor = Math.pow(10, digit);
return Math.floor(n * factor) / factor;
};
/**
*
* @param val
* @param s
* @param e
* @param ns
* @param ne
* @returns
*/
export const map = (val, s, e, ns, ne) => ns + ((val - s) / (e - s)) * (ne - ns);
/**
* linear interpolation (=lerp)
* @param a start value
* @param b stop value
* @param t amount 0..1
* @returns
*/
export const mix = (a, b, t) => a * (1 - t) + b * t;
/**
* alias for mix()
*/
export const lerp = mix;
/**
* NOTE: it may not be accurate with non-integer numbers.
* @param n
* @param max
* @returns
*/
export const mod = (n, max) => {
return ((n % max) + max) % max;
var map = (val, s, e, ns, ne) => ns + (val - s) / (e - s) * (ne - ns);
var mix = (a, b, t) => a * (1 - t) + b * t;
var lerp = mix;
var mod = (n, max) => {
return (n % max + max) % max;
};
/**
* modulo(%) for float numbers up to precision digit.
* @param n original number
* @param max modulo
* @param precision float precision digits. defaults to 6
* @returns
*/
export const modF = (n, max, precision = 6) => {
const mlt = Math.pow(10, precision); // multiplier to make it integer
const ni = Math.floor(n * mlt);
const maxi = Math.floor(max * mlt);
return (((ni % maxi) + maxi) % maxi) / mlt;
var modF = (n, max, precision = 6) => {
const mlt = Math.pow(10, precision);
const ni = Math.floor(n * mlt);
const maxi = Math.floor(max * mlt);
return (ni % maxi + maxi) % maxi / mlt;
};
// export const floorF = (n: number, digit: number): number => {
// n = parseFloat(n.toFixed(digit));
// const factor = Math.pow(10, digit);
// return Math.floor(n * factor) / factor;
// };
/**
* inclusive modulo. modIncl(1, 3) will include 3.
* can handle negative number and returns positive value
* @param n number to update
* @param max number to divide with
* @returns number from modulo op. within range 0..max (inclusive)
*/
export const modIncl = (n, max) => {
if (max < 0)
throw new Error("modIncl(): 2nd arg must be >= 0");
return n === max ? max : ((n % max) + max) % max;
var modIncl = (n, max) => {
if (max < 0)
throw new Error("modIncl(): 2nd arg must be >= 0");
return n === max ? max : (n % max + max) % max;
};
/**
* converts polar coordinate to cartesian. to update center, result.map((v,i)=> v + center[i])
* @param radius
* @param angle in radians
* @returns
*/
export const polarToCartesian = (radius, angle) => {
return [Math.cos(angle) * radius, Math.sin(angle) * radius];
var polarToCartesian = (radius, angle) => {
return [Math.cos(angle) * radius, Math.sin(angle) * radius];
};
/**
* reflect a scalar value along axis. good for creating reflected version.
* @param num number to flip
* @param axis value to reflect against
* @returns
*/
export const reflect = (num, axis) => {
return axis - (num - axis);
var reflect = (num, axis) => {
return axis - (num - axis);
};
/**
* good for drawing shapes to include the maximum value (round up)
* @param n float number
* @param digit how many float digits to keep
* @returns
*/
export const roundF = (n, digit) => {
const factor = Math.pow(10, digit);
return Math.round(n * factor) / factor;
var roundF = (n, digit) => {
const factor = Math.pow(10, digit);
return Math.round(n * factor) / factor;
};
/**
* snap value to increment
* @param n original number
* @param inc increment to snap to.
* @returns
*
*/
export const snapBy = (n, inc) => {
return Math.round(n / inc) * inc;
var snapBy = (n, inc) => {
return Math.round(n / inc) * inc;
};
/**
* snap to a value in array. whatever is closest to.
* @param n original number
* @param snapArr values to snap to
* @returns {number | undefined}
*/
export const snapToArray = (n, snapArr) => {
snapArr.sort((a, b) => a - b); // sort numbers in order
if (n <= snapArr[0])
return snapArr[0]; // if less than the smallest one
else if (n >= snapArr[snapArr.length - 1])
return snapArr[snapArr.length - 1]; // if greater than the largest num
else {
for (let i = 0; i < snapArr.length - 1; i++) {
const prev = snapArr[i];
const next = snapArr[i + 1];
if (n > prev && n < next) {
return Math.abs(n - next) <= Math.abs(n - prev) ? next : prev;
}
}
var snapToArray = (n, snapArr) => {
snapArr.sort((a, b) => a - b);
if (n <= snapArr[0])
return snapArr[0];
else if (n >= snapArr[snapArr.length - 1])
return snapArr[snapArr.length - 1];
else {
for (let i = 0; i < snapArr.length - 1; i++) {
const prev = snapArr[i];
const next = snapArr[i + 1];
if (n > prev && n < next) {
return Math.abs(n - next) <= Math.abs(n - prev) ? next : prev;
}
}
throw new Error("snapToArray(): did not meet any condition");
}
throw new Error("snapToArray(): did not meet any condition");
};
export { PI, TWO_PI, clamp, constrain, dist, distSq, floorF, lerp, map, mix, mod, modF, modIncl, polarToCartesian, reflect, roundF, snapBy, snapToArray };
//# sourceMappingURL=out.js.map
//# sourceMappingURL=index.js.map
{
"name": "@daeinc/math",
"version": "0.5.0",
"version": "0.6.0",
"description": "Math utilities",
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"main": "./dist/index.js",
"files": [

@@ -14,5 +15,5 @@ "/dist"

"scripts": {
"test": "jest --watch",
"test": "vitest",
"watch": "tsc --watch",
"build": "tsc --build --clean && tsc --build"
"build": "tsc --noemit && tsup"
},

@@ -22,4 +23,4 @@ "author": "Daeinc",

"devDependencies": {
"jest": "^29.1.2",
"ts-jest": "^29.0.3"
"tsup": "^6.6.3",
"vitest": "^0.28.5"
},

@@ -26,0 +27,0 @@ "repository": {

Sorry, the diff of this file is not supported yet

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