Fix omitDefaults/omitDefault for Long
This commit is contained in:
parent
c0b714bc5f
commit
d9510d544c
@ -1,4 +1,5 @@
|
||||
import { fromHex } from "@cosmjs/encoding";
|
||||
import Long from "long";
|
||||
import { parse } from "protobufjs";
|
||||
|
||||
import { omitDefault, omitDefaults } from "./adr27";
|
||||
@ -25,6 +26,16 @@ describe("adr27", () => {
|
||||
expect(omitDefault(0.0)).toEqual(null);
|
||||
});
|
||||
|
||||
it("works for Long", () => {
|
||||
// unsigned
|
||||
expect(omitDefault(Long.fromNumber(123, true))).toEqual(Long.fromNumber(123, true));
|
||||
expect(omitDefault(Long.fromNumber(0, true))).toEqual(null);
|
||||
|
||||
// signed
|
||||
expect(omitDefault(Long.fromNumber(123, false))).toEqual(Long.fromNumber(123, false));
|
||||
expect(omitDefault(Long.fromNumber(0, false))).toEqual(null);
|
||||
});
|
||||
|
||||
it("works for booleans", () => {
|
||||
expect(omitDefault(true)).toEqual(true);
|
||||
expect(omitDefault(false)).toEqual(null);
|
||||
@ -73,6 +84,12 @@ describe("adr27", () => {
|
||||
expect(omitDefaults(1.234)).toEqual(1.234);
|
||||
expect(omitDefaults(0.0)).toEqual(null);
|
||||
|
||||
expect(omitDefaults(Long.fromNumber(123, true))).toEqual(Long.fromNumber(123, true));
|
||||
expect(omitDefaults(Long.fromNumber(0, true))).toEqual(null);
|
||||
|
||||
expect(omitDefaults(Long.fromNumber(123, false))).toEqual(Long.fromNumber(123, false));
|
||||
expect(omitDefaults(Long.fromNumber(0, false))).toEqual(null);
|
||||
|
||||
expect(omitDefaults(true)).toEqual(true);
|
||||
expect(omitDefaults(false)).toEqual(null);
|
||||
});
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import { isNonNullObject, isUint8Array } from "@cosmjs/utils";
|
||||
import Long from "long";
|
||||
|
||||
/**
|
||||
* Converts default values to null in order to tell protobuf.js
|
||||
@ -13,6 +14,10 @@ export function omitDefault<T>(input: T): T | null {
|
||||
return input || null;
|
||||
}
|
||||
|
||||
if (Long.isLong(input)) {
|
||||
return !input.isZero() ? input : null;
|
||||
}
|
||||
|
||||
if (Array.isArray(input) || isUint8Array(input)) {
|
||||
return input.length ? input : null;
|
||||
}
|
||||
@ -33,6 +38,7 @@ export function omitDefaults(input: any): any {
|
||||
typeof input === "number" ||
|
||||
typeof input === "boolean" ||
|
||||
typeof input === "string" ||
|
||||
Long.isLong(input) ||
|
||||
Array.isArray(input) ||
|
||||
isUint8Array(input)
|
||||
) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user