-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1509 from cosmos/omitDefault-instantiate2
Use omitDefault to fix Amino JSON converter for Instantiate2
- Loading branch information
Showing
8 changed files
with
132 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { omitDefault } from "./omitdefault"; | ||
|
||
describe("omitDefault", () => { | ||
it("works for numbers", () => { | ||
expect(omitDefault(17)).toEqual(17); | ||
expect(omitDefault(0)).toEqual(undefined); | ||
}); | ||
|
||
it("works for bigint", () => { | ||
expect(omitDefault(17n)).toEqual(17n); | ||
expect(omitDefault(0n)).toEqual(undefined); | ||
}); | ||
|
||
it("works for boolean", () => { | ||
expect(omitDefault(true)).toEqual(true); | ||
expect(omitDefault(false)).toEqual(undefined); | ||
}); | ||
|
||
it("works for strings", () => { | ||
expect(omitDefault("hi")).toEqual("hi"); | ||
expect(omitDefault("")).toEqual(undefined); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Returns the given input. If the input is the default value | ||
* of protobuf, undefined is retunred. Use this when creating Amino JSON converters. | ||
*/ | ||
export function omitDefault<T extends string | number | bigint | boolean>(input: T): T | undefined { | ||
switch (typeof input) { | ||
case "string": | ||
return input === "" ? undefined : input; | ||
case "number": | ||
return input === 0 ? undefined : input; | ||
case "bigint": | ||
return input === BigInt(0) ? undefined : input; | ||
case "boolean": | ||
return !input ? undefined : input; | ||
default: | ||
throw new Error(`Got unsupported type '${typeof input}'`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters