Skip to content

Commit

Permalink
added typescript support
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Jul 26, 2022
1 parent 2f3cf64 commit c192886
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 81 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,4 @@ dist
.tern-port

*-numbers.json
pnpm-lock.yaml
22 changes: 22 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type ARRAY_TYPE =
| Array<number>
| Int8Array
| Uint8Array
| Uint8ClampedArray
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Float32Array
| Float64Array
| BigInt64Array
| BigUint64Array;

export default function fastMin(
numbers: ARRAY_TYPE,
options?: {
debug?: boolean | undefined;
no_data?: number | undefined;
theoretical_max?: number | undefined;
}
): number;
19 changes: 16 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const getTheoreticalMin = require("typed-array-ranges/get-min");

module.exports = function fastMin(
function fastMin(
numbers,
{ debug = false, no_data = undefined, theoretical_min = undefined } = {
debug: false,
no_data: undefined,
theoretical_min: undefined,
theoretical_min: undefined
}
) {
if (debug)
Expand Down Expand Up @@ -96,4 +96,17 @@ module.exports = function fastMin(

if (debug) console.log("[fast-min] returning", min);
return min;
};
}

if (typeof module === "object") {
module.exports = fastMin;
module.exports.default = fastMin;
}

if (typeof self === "object") {
self.fastMin = fastMin;
}

if (typeof window === "object") {
window.fastMin = fastMin;
}
68 changes: 0 additions & 68 deletions package-lock.json

This file was deleted.

13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
"version": "0.2.0",
"description": "Quickest Way to get the Minimum Value of an Array of Numbers (Typed or Untyped)",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
],
"scripts": {
"format": "npx prettier --write *.js",
"f": "npm run format",
"format": "npx prettier --arrow-parens=avoid --trailing-comma=none --write *.js *.ts",
"setup": "node setup.js",
"perf": "./perf",
"test": "node test.js"
"test": "npm run test:js && npm run test:ts",
"test:js": "node test.js",
"test:ts": "npx ts-node ./test.ts"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -38,9 +43,9 @@
},
"homepage": "https://github.com/DanielJDufour/fast-min#readme",
"devDependencies": {
"flug": "^1.1.0",
"flug": "^2.3.1",
"lodash.min": "^4.0.1",
"underscore": "^1.13.1"
"underscore": "^1.13.4"
},
"dependencies": {
"typed-array-ranges": "^0.0.0"
Expand Down
4 changes: 2 additions & 2 deletions perf.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const [_node, _perfjs, array_type, min_lib] = process.argv;
const MIN_FUNCS = {
lodash: require("lodash.min"),
underscore: require("underscore").min,
"fast-min": require("./index"),
"fast-min": require("./index")
};

const ARRAY_CONSTRUCTORS = {
Expand All @@ -17,7 +17,7 @@ const ARRAY_CONSTRUCTORS = {
Int32: Int32Array,
Uint32: Uint32Array,
BigInt64: BigInt64Array,
BigUint64: BigUint64Array,
BigUint64: BigUint64Array
};

const numbers = ARRAY_CONSTRUCTORS[array_type].from(
Expand Down
2 changes: 1 addition & 1 deletion setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const getRandomNums = (nbits, signed) => {
{ name: "int32", bits: 32, signed: true },
{ name: "uint32", bits: 32, signed: false },
{ name: "bigint64", bits: 64, signed: true },
{ name: "biguint64", bits: 64, signed: false },
{ name: "biguint64", bits: 64, signed: false }
].forEach(({ name, bits, signed }) => {
const filename = name + "-numbers.json";
fs.writeFileSync(
Expand Down
6 changes: 3 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test("getting minimum from typed arrays", ({ eq }) => {
[Int8Array, -128],
[Uint8Array, 0],
[Int16Array, -32768],
[Uint16Array, 0],
[Uint16Array, 0]
].forEach(([array_type, expected_min]) => {
const filename =
array_type.name.replace("Array", "").toLowerCase() + "-numbers.json";
Expand All @@ -36,7 +36,7 @@ test("getting minimum from typed arrays", ({ eq }) => {

/// new
test("getting no minimum from normal arrays with all no data values", ({
eq,
eq
}) => {
const numbers = [99, 99, 99, 99];
const result = min(numbers, { no_data: 99 });
Expand All @@ -50,7 +50,7 @@ test("getting minimum from normal arrays with some data values", ({ eq }) => {
});

test("getting no minimum from typed arrays with all no data values", ({
eq,
eq
}) => {
const numbers = Uint8Array.from([99, 99, 99, 99]);
const result = min(numbers, { no_data: 99 });
Expand Down
64 changes: 64 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { readFileSync } from "fs";
import test from "flug";
import fastMin from "./index";

test("gettings minimum from a normal array", ({ eq }) => {
const numbers = [920, -550, 340, 690, 550, -340, 840, 700, 550, 210, 540];
const result = fastMin(numbers, { debug: false });
eq(result, -550);
});

test("getting minimum from an array of image band values", ({ eq }) => {
const numbers = Uint8Array.from(
JSON.parse(readFileSync("uint8-numbers.json", "utf-8"))
);
console.log("loaded uint8 numbers of length:", numbers.length);
const result = fastMin(numbers, { debug: true });
eq(result, 0);
});

test("getting minimum from typed arrays", ({ eq }) => {
[
[Int8Array, -128] as const,
[Uint8Array, 0] as const,
[Int16Array, -32768] as const,
[Uint16Array, 0] as const
].forEach(([array_type, expected_min]) => {
const filename =
array_type.name.replace("Array", "").toLowerCase() + "-numbers.json";
const numbers = array_type.from(
JSON.parse(readFileSync(filename, "utf-8"))
);
const result = fastMin(numbers, { debug: true });
eq(result, expected_min);
});
});

/// new
test("getting no minimum from normal arrays with all no data values", ({
eq
}) => {
const numbers = [99, 99, 99, 99];
const result = fastMin(numbers, { no_data: 99 });
eq(result, undefined);
});

test("getting minimum from normal arrays with some data values", ({ eq }) => {
const numbers = [1, 99, 2, 99, 4, 99, 6, 99, -10];
const result = fastMin(numbers, { no_data: 99 });
eq(result, -10);
});

test("getting no minimum from typed arrays with all no data values", ({
eq
}) => {
const numbers = Uint8Array.from([99, 99, 99, 99]);
const result = fastMin(numbers, { no_data: 99 });
eq(result, undefined);
});

test("getting minimum from typed arrays with some data values", ({ eq }) => {
const numbers = Int8Array.from([1, 99, 2, 99, 4, 99, 6, 99, -10]);
const result = fastMin(numbers, { no_data: 99 });
eq(result, -10);
});

0 comments on commit c192886

Please sign in to comment.