Skip to content

Commit

Permalink
chore(lib+docs): better grouping of core functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rektdeckard committed Feb 21, 2024
1 parent 619514d commit 7a6522d
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 250 deletions.
326 changes: 163 additions & 163 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,22 @@ A collection of interesting helpers, data structures, and utility types for mess
- [K-dimensional Tree](#kdtree)
- [Bloom Filter](#bloomfilter)
- [Ring Buffer](#ringbuffer)
- [Transforms](#transforms)
- [clamp](#clamp)
- [lerp](#lerp)
- [gcf](#gcf)
- [lcm](#lcm)
- [trailingZeros](#trailingzeros)
- [Generators](#generators)
- [Random](#random)
- [Range](#range)
- [Probability](#probability)
- [Noise](#noise)
- [objectHash](#objecthash)
- [Analysis](#analysis)
- [Comparator](#comparator)
- [Statistics](#statistics)
- [Fourier](#fourier)
- [Utils](#Utilities)
- [clamp](#clamp)
- [lerp](#lerp)
- [gcf](#gcf)
- [lcm](#lcm)
- [trailingZeros](#trailingzeros)
- [objectHash](#objecthash)
- [Assertions](#assertions)
- [castInteger](#castinteger)
- [assertInteger](#assertinteger)
Expand Down Expand Up @@ -714,130 +715,6 @@ buff.dequeue(); // 24
buff.drain(); // [36, 48];
```

## Transforms

### clamp

Constrain a value to within a given range `[min, max]`.

<details>
<summary>Function Signature</summary>
<p>

```ts
function clamp(min: number, max: number, value: number): number;
```

</p>
</details>

```ts
import { clamp } from "kdim";

// Clamp values to [0, 255]
const a = 32;
const b = 300;

clamp(0, 255, a); // 32
clamp(0, 255, b); // 255
```

> **NOTE:** throws a RangeError when the range is invalid, E.G. `min > max`.
### lerp

Linear interpolation of a value in the range `[0, 1]` to a value in the range `[from, to]`.

<details>
<summary>Function Signature</summary>
<p>

```ts
function lerp(from: number, to: number, value: number): number;
```

</p>
</details>

```ts
import { lerp } from "kdim";

// Interpolate 0.4 in range [0,1] to range [1,99]
const value = 0.4;
const interpolated = lerp(1, 99, value); // 40.2
```

> **NOTE:** throws a RangeError when the value is outside of `[0, 1]`
### gcf

Find the Greatest Common Factor of two integers.

<details>
<summary>Function Signature</summary>
<p>

```ts
function gcf(a: number, b: number): number;
```

</p>
</details>

```ts
import { gcf } from "kdim";

gcf(45, 420); // 15
```

> **NOTE:** throws a RangeError when `a` or `b` are non-integral.
### lcm

Find the Least Common Multiple of two integers.

<details>
<summary>Function Signature</summary>
<p>

```ts
function lcm(a: number, b: number): number;
```

</p>
</details>

```ts
import { lcm } from "kdim";

lcm(6, 20); // 60
```

> **NOTE:** throws a RangeError when `a` or `b` are non-integral.
### trailingZeros

Compute the number of trailing zeros in a number's 32-bit representation, equivalent to its largest power-of-two divisor.

<details>
<summary>Function Signature</summary>
<p>

```ts
function trailingZeros(n: number): number;
```

</p>
</details>

```ts
import { trailingZeros } from "kdim";

trailingZeros(24); // 3
```

> **NOTE:** throws a RangeError when `n` is non-integral.
## Generators

### Random
Expand Down Expand Up @@ -1036,7 +913,6 @@ const complexes = Array.from(deferred); // Only now are values produced

Generate structured mathematical noise patterns like Perlin and Simplex, in both 2D and 3D spaces. Layer patterns for fractal noise fields. Efficiently fill `TypedArray` and `ImageData` buffers for use in graphics applications. These can be parametrized with [Seedable PRNGs](#seedable-prngs), or use the default (unseedable) PRNG based on `Math.random()`.


<details>
<summary>Abstract Class Signature</summary>
<p>
Expand Down Expand Up @@ -1202,37 +1078,6 @@ const d2 = new Probability(events, new Random.Seedable(42));
d2.sample().value; // "uncommon", definitely.
```

### objectHash

A hashing function for arbitrary objects and primitives that digests the value into a pseudo-unique base-16 hash string. Uses structural hashing, such that objects of identical structure will produce the same hash.

<details>
<summary>Function Signature</summary>
<p>

```ts
type ObjectHashAlgorithm = "SHA-1" | "SHA-256" | "SHA-384" | "SHA-512";

type ObjectHashOptions = {
algorithm?: ObjectHashAlgorithm;
};

async function objectHash<T>(
obj: T,
options?: ObjectHashOptions
): Promise<string>;
```

</p>
</details>

```ts
import { objectHash } from "kdim";

const hash = await objectHash({ foo: 7, bar: [] }, { algorithm: "SHA-1" });
// "1448bf86764e7ff7f9df0cb61b2d77c946ba854"
```

## Analysis

### Comparator
Expand Down Expand Up @@ -1475,6 +1320,161 @@ const d = Fourier.dft(sample);
// ]
```

## Transforms

### clamp

Constrain a value to within a given range `[min, max]`.

<details>
<summary>Function Signature</summary>
<p>

```ts
function clamp(min: number, max: number, value: number): number;
```

</p>
</details>

```ts
import { clamp } from "kdim";

// Clamp values to [0, 255]
const a = 32;
const b = 300;

clamp(0, 255, a); // 32
clamp(0, 255, b); // 255
```

> **NOTE:** throws a RangeError when the range is invalid, E.G. `min > max`.
### lerp

Linear interpolation of a value in the range `[0, 1]` to a value in the range `[from, to]`.

<details>
<summary>Function Signature</summary>
<p>

```ts
function lerp(from: number, to: number, value: number): number;
```

</p>
</details>

```ts
import { lerp } from "kdim";

// Interpolate 0.4 in range [0,1] to range [1,99]
const value = 0.4;
const interpolated = lerp(1, 99, value); // 40.2
```

> **NOTE:** throws a RangeError when the value is outside of `[0, 1]`
### gcf

Find the Greatest Common Factor of two integers.

<details>
<summary>Function Signature</summary>
<p>

```ts
function gcf(a: number, b: number): number;
```

</p>
</details>

```ts
import { gcf } from "kdim";

gcf(45, 420); // 15
```

> **NOTE:** throws a RangeError when `a` or `b` are non-integral.
### lcm

Find the Least Common Multiple of two integers.

<details>
<summary>Function Signature</summary>
<p>

```ts
function lcm(a: number, b: number): number;
```

</p>
</details>

```ts
import { lcm } from "kdim";

lcm(6, 20); // 60
```

> **NOTE:** throws a RangeError when `a` or `b` are non-integral.
### trailingZeros

Compute the number of trailing zeros in a number's 32-bit representation, equivalent to its largest power-of-two divisor.

<details>
<summary>Function Signature</summary>
<p>

```ts
function trailingZeros(n: number): number;
```

</p>
</details>

```ts
import { trailingZeros } from "kdim";

trailingZeros(24); // 3
```

> **NOTE:** throws a RangeError when `n` is non-integral.
### objectHash

A hashing function for arbitrary objects and primitives that digests the value into a pseudo-unique base-16 hash string. Uses structural hashing, such that objects of identical structure will produce the same hash.

<details>
<summary>Function Signature</summary>
<p>

```ts
type ObjectHashAlgorithm = "SHA-1" | "SHA-256" | "SHA-384" | "SHA-512";

type ObjectHashOptions = {
algorithm?: ObjectHashAlgorithm;
};

async function objectHash<T>(
obj: T,
options?: ObjectHashOptions
): Promise<string>;
```

</p>
</details>

```ts
import { objectHash } from "kdim";

const hash = await objectHash({ foo: 7, bar: [] }, { algorithm: "SHA-1" });
// "1448bf86764e7ff7f9df0cb61b2d77c946ba854"
```

## Assertions

### castInteger
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kdim",
"version": "0.6.2",
"version": "0.6.3",
"description": "Utility data stuctures, math, and types for JS",
"author": {
"name": "Tobias Fried",
Expand Down
1 change: 0 additions & 1 deletion src/math/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export * from "./range";
export * from "./rational";
export * from "./saturating";
export * from "./statistics";
export * from "./transforms";
export * from "./types";
export * from "./utils";
export * from "./wrapping";
2 changes: 1 addition & 1 deletion src/math/noise.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { KDTree } from "../data";
import { Range } from "./range";
import { Random, type PRNG } from "./random";
import { uncheckedLerp } from "./transforms";
import { uncheckedLerp } from "./utils";

const MAX_ENTROPY = 2 ** 16;

Expand Down
Loading

0 comments on commit 7a6522d

Please sign in to comment.