fix: preserve zero input in Node API
This commit is contained in:
parent
a0a369a7ef
commit
fb671952e9
@ -37,17 +37,18 @@ class Dish {
|
|||||||
constructor(dishOrInput=null, type = null) {
|
constructor(dishOrInput=null, type = null) {
|
||||||
this.value = new ArrayBuffer(0);
|
this.value = new ArrayBuffer(0);
|
||||||
this.type = Dish.ARRAY_BUFFER;
|
this.type = Dish.ARRAY_BUFFER;
|
||||||
|
const hasInput = Boolean(dishOrInput) || dishOrInput === 0;
|
||||||
|
|
||||||
// Case: dishOrInput is dish object
|
// Case: dishOrInput is dish object
|
||||||
if (dishOrInput &&
|
if (hasInput &&
|
||||||
Object.prototype.hasOwnProperty.call(dishOrInput, "value") &&
|
Object.prototype.hasOwnProperty.call(dishOrInput, "value") &&
|
||||||
Object.prototype.hasOwnProperty.call(dishOrInput, "type")) {
|
Object.prototype.hasOwnProperty.call(dishOrInput, "type")) {
|
||||||
this.set(dishOrInput.value, dishOrInput.type);
|
this.set(dishOrInput.value, dishOrInput.type);
|
||||||
// input and type defined separately
|
// input and type defined separately
|
||||||
} else if (dishOrInput && type !== null) {
|
} else if (hasInput && type !== null) {
|
||||||
this.set(dishOrInput, type);
|
this.set(dishOrInput, type);
|
||||||
// No type declared, so infer it.
|
// No type declared, so infer it.
|
||||||
} else if (dishOrInput) {
|
} else if (hasInput) {
|
||||||
const inferredType = Dish.typeEnum(dishOrInput.constructor.name);
|
const inferredType = Dish.typeEnum(dishOrInput.constructor.name);
|
||||||
this.set(dishOrInput, inferredType);
|
this.set(dishOrInput, inferredType);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -98,7 +98,7 @@ function transformArgs(opArgsList, newArgs) {
|
|||||||
* @param input
|
* @param input
|
||||||
*/
|
*/
|
||||||
function ensureIsDish(input) {
|
function ensureIsDish(input) {
|
||||||
if (!input) {
|
if (!input && input !== 0) {
|
||||||
return new NodeDish();
|
return new NodeDish();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -38,6 +38,9 @@ TestRegister.addApiTests([
|
|||||||
const numberDish = new Dish(333);
|
const numberDish = new Dish(333);
|
||||||
assert.strictEqual(numberDish.type, 2);
|
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);
|
const arrayBufferDish = new Dish(Buffer.from("some buffer input").buffer);
|
||||||
assert.strictEqual(arrayBufferDish.type, 4);
|
assert.strictEqual(arrayBufferDish.type, 4);
|
||||||
|
|
||||||
|
|||||||
@ -97,6 +97,11 @@ TestRegister.addApiTests([
|
|||||||
assert(result instanceof NodeDish);
|
assert(result instanceof NodeDish);
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
it("should preserve numeric zero input", () => {
|
||||||
|
const result = chef.toHex(0);
|
||||||
|
assert.strictEqual(result.toString(), "30");
|
||||||
|
}),
|
||||||
|
|
||||||
it("should coerce to a string as you expect", () => {
|
it("should coerce to a string as you expect", () => {
|
||||||
const result = chef.fromBase32(chef.toBase32("something"));
|
const result = chef.fromBase32(chef.toBase32("something"));
|
||||||
assert.equal(String(result), "something");
|
assert.equal(String(result), "something");
|
||||||
@ -180,6 +185,11 @@ TestRegister.addApiTests([
|
|||||||
assert.strictEqual(result.toString(), "ONXW2ZJANFXHA5LU");
|
assert.strictEqual(result.toString(), "ONXW2ZJANFXHA5LU");
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
it("chef.bake: should preserve numeric zero input", async () => {
|
||||||
|
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 () => {
|
it("chef.bake: should complain if recipe isnt a valid object", async () => {
|
||||||
await assert.rejects(() => chef.bake("some input", 3264), {
|
await assert.rejects(() => chef.bake("some input", 3264), {
|
||||||
name: "TypeError",
|
name: "TypeError",
|
||||||
|
|||||||
@ -59,6 +59,13 @@ TestRegister.addApiTests([
|
|||||||
assert.strictEqual(result.toString(), "7");
|
assert.strictEqual(result.toString(), "7");
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
it("ADD: preserves numeric zero input", () => {
|
||||||
|
const result = chef.ADD(0, {
|
||||||
|
key: "4",
|
||||||
|
});
|
||||||
|
assert.strictEqual(result.toString(), "4");
|
||||||
|
}),
|
||||||
|
|
||||||
it("addLineNumbers: No arguments", () => {
|
it("addLineNumbers: No arguments", () => {
|
||||||
const result = addLineNumbers("sample input");
|
const result = addLineNumbers("sample input");
|
||||||
assert.equal(result.toString(), "1 sample input");
|
assert.equal(result.toString(), "1 sample input");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user