Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions src/core/Dish.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@ class Dish {
constructor(dishOrInput=null, type = null) {
this.value = new ArrayBuffer(0);
this.type = Dish.ARRAY_BUFFER;
const hasInput = Boolean(dishOrInput) || dishOrInput === 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vetrovk Is there a reason for this approach instead of the input === undefined || input === null recommended in the issue?


// Case: dishOrInput is dish object
if (dishOrInput &&
if (hasInput &&
Object.prototype.hasOwnProperty.call(dishOrInput, "value") &&
Object.prototype.hasOwnProperty.call(dishOrInput, "type")) {
this.set(dishOrInput.value, dishOrInput.type);
// input and type defined separately
} else if (dishOrInput && type !== null) {
} else if (hasInput && type !== null) {
this.set(dishOrInput, type);
// No type declared, so infer it.
} else if (dishOrInput) {
} else if (hasInput) {
const inferredType = Dish.typeEnum(dishOrInput.constructor.name);
this.set(dishOrInput, inferredType);
}
Expand Down
2 changes: 1 addition & 1 deletion src/node/api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function transformArgs(opArgsList, newArgs) {
* @param input
*/
function ensureIsDish(input) {
if (!input) {
if (!input && input !== 0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vetrovk Is there a reason for this approach instead of the input === undefined || input === null recommended in the issue?

return new NodeDish();
}

Expand Down
3 changes: 3 additions & 0 deletions tests/node/tests/NodeDish.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ TestRegister.addApiTests([
const numberDish = new Dish(333);
assert.strictEqual(numberDish.type, 2);

const zeroDish = new Dish(0);
assert.strictEqual(zeroDish.type, 2);

const arrayBufferDish = new Dish(Buffer.from("some buffer input").buffer);
assert.strictEqual(arrayBufferDish.type, 4);

Expand Down
10 changes: 10 additions & 0 deletions tests/node/tests/nodeApi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ TestRegister.addApiTests([
assert(result instanceof NodeDish);
}),

it("should preserve numeric zero input", () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't use "preserve" for the title of this test - I think "process" would make more sense here?

const result = chef.toHex(0);
assert.strictEqual(result.toString(), "30");
}),

it("should coerce to a string as you expect", () => {
const result = chef.fromBase32(chef.toBase32("something"));
assert.equal(String(result), "something");
Expand Down Expand Up @@ -180,6 +185,11 @@ TestRegister.addApiTests([
assert.strictEqual(result.toString(), "ONXW2ZJANFXHA5LU");
}),

it("chef.bake: should preserve numeric zero input", async () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't use "preserve" for the title of this test - I think "process" would make more sense here?

const result = await chef.bake(0, "to hex");
assert.strictEqual(result.toString(), "30");
}),

it("chef.bake: should complain if recipe isnt a valid object", async () => {
await assert.rejects(() => chef.bake("some input", 3264), {
name: "TypeError",
Expand Down
7 changes: 7 additions & 0 deletions tests/node/tests/operations.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ TestRegister.addApiTests([
assert.strictEqual(result.toString(), "7");
}),

it("ADD: preserves numeric zero input", () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't use "preserve" for the title of this test - I think "process" would make more sense here?

const result = chef.ADD(0, {
key: "4",
});
assert.strictEqual(result.toString(), "4");
}),

it("addLineNumbers: No arguments", () => {
const result = addLineNumbers("sample input");
assert.equal(result.toString(), "1 sample input");
Expand Down
Loading