Skip to content

Commit 7e680a7

Browse files
8 feature error missing parameters (#9)
* feat: add error for missing parameter * chore(npm): change version to 1.0.3
1 parent c1a3203 commit 7e680a7

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tfjs-image-node",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "A simple image classifier using tfjs and running in node.js",
55
"main": "./dist/index.js",
66
"module": "./dist/index.mjs",

src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ type ClassifyImageType = (
1414
MODEL_DIR_PATH: string,
1515
IMAGE_FILE_PATH: string,
1616
METADATA: IMetadata
17-
) => Promise<ResultType[]>;
17+
) => Promise<ResultType[] | Error>;
1818

1919
const classifyImage: ClassifyImageType = async (
2020
MODEL_DIR_PATH: string,
2121
IMAGE_FILE_PATH: string,
2222
METADATA: IMetadata
2323
) => {
24-
let labels: string[] = METADATA.labels;
24+
if (!MODEL_DIR_PATH || !IMAGE_FILE_PATH || !METADATA) {
25+
return new Error("MISSING_PARAMETER");
26+
}
27+
28+
let labels: string[] = METADATA["labels"];
2529

2630
const model = await tf.loadLayersModel(`${MODEL_DIR_PATH}/model.json`);
2731

test/classifyImage.test.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,41 @@ const imageNoHand =
1010
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/Black_colour.jpg/640px-Black_colour.jpg";
1111

1212
describe("classifyImage function", async () => {
13-
it("returns hand when shown a picture of a hand", async () => {
14-
const result = await classifyImage(model, imageHand, metadata);
13+
describe("returns", async () => {
14+
it("returns hand when shown a picture of a hand", async () => {
15+
const result = await classifyImage(model, imageHand, metadata);
16+
if (result instanceof Error) {
17+
return new Error();
18+
} else {
19+
assert.equal(result[0].label, "Hand");
20+
}
21+
});
1522

16-
assert.equal(result[0].label, "Hand");
17-
});
23+
it("returns 'No hand' when shown a picture not including hand", async () => {
24+
const result = await classifyImage(model, imageNoHand, metadata);
1825

19-
it("returns 'No hand' when shown a picture not including hand", async () => {
20-
const result = await classifyImage(model, imageNoHand, metadata);
26+
if (result instanceof Error) {
27+
return new Error();
28+
} else {
29+
assert.equal(result[0].label, "No hand");
30+
}
31+
});
2132

22-
assert.equal(result[0].label, "No hand");
33+
it("returns a probability level", async () => {
34+
const result = await classifyImage(model, imageNoHand, metadata);
35+
if (result instanceof Error) {
36+
return new Error();
37+
} else {
38+
assert.notEqual(result[0].probability, null);
39+
}
40+
});
2341
});
42+
describe("Error boundries", async () => {
43+
it("returns an error when missing a parameter", async () => {
44+
//@ts-expect-error
45+
const result = await classifyImage(imageNoHand, metadata);
2446

25-
it("returns a probability level", async () => {
26-
const result = await classifyImage(model, imageNoHand, metadata);
27-
28-
assert.notEqual(result[0].probability, null);
47+
assert.ok(result instanceof Error);
48+
});
2949
});
3050
});

0 commit comments

Comments
 (0)