Skip to content

Commit d58edc3

Browse files
committed
added ts support
1 parent ce8d3ff commit d58edc3

File tree

7 files changed

+132
-16
lines changed

7 files changed

+132
-16
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,4 @@ dist
105105

106106
numbers.json
107107
*-numbers.json
108-
108+
pnpm-lock.yaml

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,39 @@ so if we encounter a 255 in the array, we can stop searching for a higher value.
1414
# usage
1515
# getting maximum value of a normal array
1616
```javascript
17-
const max = require("fast-max");
17+
const fastMax = require("fast-max"); // or import max from "fast-max";
1818

19-
const result = max([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
19+
const result = fastMax([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
2020
// result is 10
2121
```
2222

2323
# getting maximum value of a typed array
2424
```javascript
25-
const max = require("fast-max");
25+
const fastMax = require("fast-max");
2626

2727
const pixel_values = Uint8Array.from([0, 128, 255, 34, ...]);
28-
const result = max(pixel_values);
28+
const result = fastMax(pixel_values);
2929
// result is 255
3030
```
3131

3232
# setting theoretical maximum
3333
If you know that an array's values can't exceed a specific number,
3434
you can set the theoretical_max.
3535
```javascript
36-
const max = require("fast-max");
36+
const fastMax = require("fast-max");
3737

3838
const numbers = [0, 9, 4, 2, 10, ...]);
39-
const result = max(numbers, { theoretical_max: 10 });
39+
const result = fastMax(numbers, { theoretical_max: 10 });
4040
// result is 10
4141
```
4242

4343
# no data value
4444
If you want to ignore a specific value, you can set the no_data value.
4545
```javascript
46-
const max = require("fast-max");
46+
const fastMax = require("fast-max");
4747

4848
const numbers = [99, 0, 7, 99, 5, ...]);
49-
const result = max(numbers, { no_data: 99 });
49+
const result = fastMax(numbers, { no_data: 99 });
5050
// result is 7
5151
```
5252

index.d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
type ARRAY_TYPE =
2+
| Array<number>
3+
| Int8Array
4+
| Uint8Array
5+
| Uint8ClampedArray
6+
| Int16Array
7+
| Uint16Array
8+
| Int32Array
9+
| Uint32Array
10+
| Float32Array
11+
| Float64Array
12+
| BigInt64Array
13+
| BigUint64Array;
14+
15+
export default function fastMax(
16+
numbers: ARRAY_TYPE,
17+
options?: {
18+
debug?: boolean | undefined;
19+
no_data?: number | undefined;
20+
theoretical_max?: number | undefined;
21+
}
22+
): number;

index.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const getTheoreticalMax = require("typed-array-ranges/get-max");
22

3-
module.exports = function fastMax(
3+
function fastMax(
44
numbers,
55
{ debug = false, no_data = undefined, theoretical_max = undefined } = {
66
debug: false,
@@ -74,4 +74,23 @@ module.exports = function fastMax(
7474

7575
if (debug) console.log("[fast-max] returning", max);
7676
return max;
77-
};
77+
}
78+
79+
if (typeof define === "function" && define.amd) {
80+
define(function () {
81+
return fastMax;
82+
});
83+
}
84+
85+
if (typeof module === "object") {
86+
module.exports = fastMax;
87+
module.exports.default = fastMax;
88+
}
89+
90+
if (typeof self === "object") {
91+
self.fastMax = fastMax;
92+
}
93+
94+
if (typeof window === "object") {
95+
window.fastMax = fastMax;
96+
}

package.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@
33
"version": "0.3.0",
44
"description": "Quickest Way to get the Maximum Value of an Array of Numbers (Typed or Untyped)",
55
"main": "index.js",
6+
"types": "index.d.ts",
7+
"files": [
8+
"index.d.ts",
9+
"index.js"
10+
],
611
"scripts": {
7-
"format": "npx prettier --arrow-parens=avoid --print-width=120 --write *.js",
12+
"f": "npm run format",
13+
"format": "npx prettier --arrow-parens=avoid --print-width=120 --write *.js *.ts",
814
"setup": "node setup.js",
915
"perf": "./perf",
10-
"test": "node test.js"
16+
"test": "npm run test:js && npm run test:ts",
17+
"test:js": "node test.js",
18+
"test:ts": "npx ts-node ./test.ts"
1119
},
1220
"repository": {
1321
"type": "git",
@@ -35,9 +43,9 @@
3543
},
3644
"homepage": "https://github.com/DanielJDufour/fast-max#readme",
3745
"devDependencies": {
38-
"flug": "^1.1.0",
46+
"flug": "^2.3.1",
3947
"lodash.max": "^4.0.1",
40-
"underscore": "^1.13.1"
48+
"underscore": "^1.13.4"
4149
},
4250
"dependencies": {
4351
"typed-array-ranges": "^0.0.0"

test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ test("getting maximum from typed arrays", ({ eq }) => {
3737
});
3838

3939
test("getting maximum from a very large untyped array", ({ eq }) => {
40-
const numbers = JSON.parse(fs.readFileSync("uint16-numbers.json")).map(n => Number(n));
40+
const numbers = JSON.parse(fs.readFileSync("uint16-numbers.json", "utf-8")).map(n => Number(n));
4141
const result = max(numbers, { debug: true });
4242
eq(result, 65535);
4343
});

test.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { readFileSync } from "fs";
2+
import test from "flug";
3+
import max from "./index";
4+
5+
test("gettings maximum from a normal array", ({ eq }) => {
6+
const numbers = [920, 550, 340, 690, 550, 340, 840, 700, 550, 210, 540];
7+
const result = max(numbers, { debug: false });
8+
eq(result, 920);
9+
});
10+
11+
test("getting maximum from an array of image band values", ({ eq }) => {
12+
const numbers = Uint8Array.from(JSON.parse(readFileSync("uint8-numbers.json", "utf-8")));
13+
console.log("loaded uint8 numbers of length:", numbers.length);
14+
const result = max(numbers, { debug: false });
15+
eq(result, 255);
16+
});
17+
18+
test("setting theoretical maximum", ({ eq }) => {
19+
const numbers = Array.from(Uint8Array.from(JSON.parse(readFileSync("uint8-numbers.json", "utf-8"))));
20+
console.log("loaded uint8 numbers of length:", numbers.length);
21+
const result = max(numbers, { debug: false, theoretical_max: 255 });
22+
eq(result, 255);
23+
});
24+
25+
test("getting maximum from typed arrays", ({ eq }) => {
26+
[
27+
[Int8Array, 127] as const,
28+
[Uint8Array, 255] as const,
29+
[Int16Array, 32767] as const,
30+
[Uint16Array, 65535] as const,
31+
].forEach(([array_type, expected_max]) => {
32+
const filename = array_type.name.replace("Array", "").toLowerCase() + "-numbers.json";
33+
const numbers = array_type.from(JSON.parse(readFileSync(filename, "utf-8")));
34+
const result = max(numbers, { debug: false });
35+
eq(result, expected_max);
36+
});
37+
});
38+
39+
test("getting maximum from a very large untyped array", ({ eq }) => {
40+
const numbers = JSON.parse(readFileSync("uint16-numbers.json", "utf-8")).map(n => Number(n));
41+
const result = max(numbers, { debug: true });
42+
eq(result, 65535);
43+
});
44+
45+
test("getting no maximum from normal arrays with all no data values", ({ eq }) => {
46+
const numbers = [99, 99, 99, 99];
47+
const result = max(numbers, { no_data: 99 });
48+
eq(result, undefined);
49+
});
50+
51+
test("getting maximum from normal arrays with some data values", ({ eq }) => {
52+
const numbers = [1, 99, 2, 99, 4, 99, 6, 99, -10];
53+
const result = max(numbers, { no_data: 99 });
54+
eq(result, 6);
55+
});
56+
57+
test("getting no maximum from typed arrays with all no data values", ({ eq }) => {
58+
const numbers = Uint8Array.from([99, 99, 99, 99]);
59+
const result = max(numbers, { no_data: 99 });
60+
eq(result, undefined);
61+
});
62+
63+
test("getting maximum from typed arrays with some data values", ({ eq }) => {
64+
const numbers = Int8Array.from([1, 99, 2, 99, 4, 99, 6, 99, -10]);
65+
const result = max(numbers, { no_data: 99 });
66+
eq(result, 6);
67+
});

0 commit comments

Comments
 (0)