mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 21:49:15 +00:00
Replace optional function with ?? operator
This commit is contained in:
parent
9983ac7a4c
commit
a2571295b4
@ -17,7 +17,6 @@ import {
|
||||
dictionaryToStringMap,
|
||||
Integer,
|
||||
may,
|
||||
optional,
|
||||
} from "../encodings";
|
||||
import { hashTx } from "../hasher";
|
||||
import * as responses from "../responses";
|
||||
@ -110,13 +109,13 @@ interface RpcAttribute {
|
||||
/** base64 encoded */
|
||||
readonly key: string;
|
||||
/** base64 encoded */
|
||||
readonly value: string;
|
||||
readonly value?: string | null;
|
||||
}
|
||||
|
||||
function decodeAttribute(attribute: RpcAttribute): responses.Attribute {
|
||||
return {
|
||||
key: fromBase64(assertNotEmpty(attribute.key)),
|
||||
value: fromBase64(optional(attribute.value, "")),
|
||||
value: fromBase64(assertString(attribute.value ?? "")),
|
||||
};
|
||||
}
|
||||
|
||||
@ -154,13 +153,13 @@ interface RpcTxData {
|
||||
|
||||
function decodeTxData(data: RpcTxData): responses.TxData {
|
||||
return {
|
||||
code: Integer.parse(assertNumber(optional<number>(data.code, 0))),
|
||||
code: Integer.parse(assertNumber(data.code ?? 0)),
|
||||
codeSpace: data.codespace,
|
||||
log: data.log,
|
||||
data: may(fromBase64, data.data),
|
||||
events: data.events ? decodeEvents(data.events) : [],
|
||||
gasWanted: Integer.parse(optional<string>(data.gas_wanted, "0")),
|
||||
gasUsed: Integer.parse(optional<string>(data.gas_used, "0")),
|
||||
gasWanted: Integer.parse(data.gas_wanted ?? "0"),
|
||||
gasUsed: Integer.parse(data.gas_used ?? "0"),
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -123,11 +123,6 @@ export function assertNotEmpty<T>(value: T): T {
|
||||
return value;
|
||||
}
|
||||
|
||||
// optional uses the value or provides a default
|
||||
export function optional<T>(value: T | null | undefined, fallback: T): T {
|
||||
return value === undefined || value === null ? fallback : value;
|
||||
}
|
||||
|
||||
// may will run the transform if value is defined, otherwise returns undefined
|
||||
export function may<T, U>(transform: (val: T) => U, value: T | null | undefined): U | undefined {
|
||||
return value === undefined || value === null ? undefined : transform(value);
|
||||
|
@ -17,7 +17,6 @@ import {
|
||||
dictionaryToStringMap,
|
||||
Integer,
|
||||
may,
|
||||
optional,
|
||||
} from "../encodings";
|
||||
import { hashTx } from "../hasher";
|
||||
import * as responses from "../responses";
|
||||
@ -111,13 +110,13 @@ function decodeAbciQuery(data: RpcAbciQueryResponse): responses.AbciQueryRespons
|
||||
*/
|
||||
interface RpcEventAttribute {
|
||||
readonly key: string;
|
||||
readonly value: string;
|
||||
readonly value?: string;
|
||||
}
|
||||
|
||||
function decodeEventAttribute(attribute: RpcEventAttribute): responses.EventAttribute {
|
||||
return {
|
||||
key: assertNotEmpty(attribute.key),
|
||||
value: optional(attribute.value, ""),
|
||||
value: attribute.value ?? "",
|
||||
};
|
||||
}
|
||||
|
||||
@ -155,13 +154,13 @@ interface RpcTxData {
|
||||
|
||||
function decodeTxData(data: RpcTxData): responses.TxData {
|
||||
return {
|
||||
code: Integer.parse(assertNumber(optional<number>(data.code, 0))),
|
||||
code: Integer.parse(assertNumber(data.code ?? 0)),
|
||||
codeSpace: data.codespace,
|
||||
log: data.log,
|
||||
data: may(fromBase64, data.data),
|
||||
events: data.events ? decodeEvents(data.events) : [],
|
||||
gasWanted: Integer.parse(optional<string>(data.gas_wanted, "0")),
|
||||
gasUsed: Integer.parse(optional<string>(data.gas_used, "0")),
|
||||
gasWanted: Integer.parse(data.gas_wanted ?? "0"),
|
||||
gasUsed: Integer.parse(data.gas_used ?? "0"),
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -123,11 +123,6 @@ export function assertNotEmpty<T>(value: T): T {
|
||||
return value;
|
||||
}
|
||||
|
||||
// optional uses the value or provides a default
|
||||
export function optional<T>(value: T | null | undefined, fallback: T): T {
|
||||
return value === undefined || value === null ? fallback : value;
|
||||
}
|
||||
|
||||
// may will run the transform if value is defined, otherwise returns undefined
|
||||
export function may<T, U>(transform: (val: T) => U, value: T | null | undefined): U | undefined {
|
||||
return value === undefined || value === null ? undefined : transform(value);
|
||||
|
Loading…
x
Reference in New Issue
Block a user