Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nice-chefs-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"maath": patch
---

Added generics to src/random functions to properly infer TypedArray
42 changes: 21 additions & 21 deletions packages/maath/src/random/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ const defaultSphere = {
// random on surface of sphere
// - https://twitter.com/fermatslibrary/status/1430932503578226688
// - https://mathworld.wolfram.com/SpherePointPicking.html
export function onSphere(
buffer: TypedArray,
export function onSphere<T extends TypedArray>(
buffer: T,
sphere?: Sphere,
rng: Generator = defaultGen
) {
): T {
const { radius, center } = {
...defaultSphere,
...sphere,
Expand All @@ -169,11 +169,11 @@ export function onSphere(
}

// from "Another Method" https://datagenetics.com/blog/january32020/index.html
export function inSphere(
buffer: TypedArray,
export function inSphere<T extends TypedArray>(
buffer: T,
sphere?: Sphere,
rng: Generator = defaultGen
) {
): T {
const { radius, center } = {
...defaultSphere,
...sphere,
Expand Down Expand Up @@ -213,11 +213,11 @@ const defaultCircle = {
};

// random circle https://stackoverflow.com/a/50746409
export function inCircle(
buffer: TypedArray,
export function inCircle<T extends TypedArray>(
buffer: T,
circle?: Circle,
rng: Generator = defaultGen
): TypedArray {
): T {
const { radius, center } = {
...defaultCircle,
...circle,
Expand All @@ -234,11 +234,11 @@ export function inCircle(
return buffer;
}

export function onCircle(
buffer: TypedArray,
export function onCircle<T extends TypedArray>(
buffer: T,
circle?: Circle,
rng: Generator = defaultGen
) {
): T {
const { radius, center } = {
...defaultCircle,
...circle,
Expand Down Expand Up @@ -287,22 +287,22 @@ export function inRect<T extends TypedArray>(
return buffer;
}

export function onRect(
buffer: TypedArray,
export function onRect<T extends TypedArray>(
buffer: T,
rect?: Rect,
rng: Generator = defaultGen
) {
): T {
return buffer;
}

/***
* [3D] Box
*/
export function inBox(
buffer: TypedArray,
export function inBox<T extends TypedArray>(
buffer: T,
box?: Box,
rng: Generator = defaultGen
) {
): T {
const { sides, center } = {
...defaultBox,
...box,
Expand Down Expand Up @@ -331,11 +331,11 @@ const defaultBox = {
center: [0, 0, 0],
};

export function onBox(
buffer: TypedArray,
export function onBox<T extends TypedArray>(
buffer: T,
box?: Box,
rng: Generator = defaultGen
) {
): T {
const { sides, center } = {
...defaultBox,
...box,
Expand Down