Filter undefined values (#105)

filter out fields with undefined values
This commit is contained in:
Nathan Nichols
2020-09-08 17:17:14 -05:00
parent 2c99932c3c
commit a5559825ae
6 changed files with 69 additions and 4 deletions

View File

@@ -1,5 +1,8 @@
# ripple-binary-codec Release History
## 1.0.1 (2020-09-08)
- Filter out fields with undefined values
## 1.0.0 (2020-08-17)
- Migrate to TypeScript

View File

@@ -27,7 +27,7 @@ Decode a hex-string into a transaction object.
```
### encode(json: object): string
Encode a transaction object into a hex-string.
Encode a transaction object into a hex-string. Note that encode filters out fields with undefined values.
```js
> api.encode({
LedgerEntryType: 'AccountRoot',

View File

@@ -1,6 +1,6 @@
{
"name": "ripple-binary-codec",
"version": "1.0.0",
"version": "1.0.1",
"description": "XRP Ledger binary codec",
"files": [
"dist/*",

View File

@@ -10,7 +10,7 @@ Each ledger's state tree contain [ledger objects](https://xrpl.org/ledger-object
## Fields
These are Serialization Fields (`sf`) [defined in rippled's SField.cpp](https://github.com/ripple/rippled/blob/develop/src/ripple/protocol/impl/SField.cpp).
These are Serialization Fields (`sf`) [defined in rippled's SField.cpp](https://github.com/ripple/rippled/blob/develop/src/ripple/protocol/impl/SField.cpp). Fields with undefined values are omitted before encoding.
### Key

View File

@@ -109,7 +109,12 @@ class STObject extends SerializedType {
let sorted = Object.keys(xAddressDecoded)
.map((f: string): FieldInstance => Field[f] as FieldInstance)
.filter((f: FieldInstance): boolean => f !== undefined && f.isSerialized)
.filter(
(f: FieldInstance): boolean =>
f !== undefined &&
xAddressDecoded[f.name] !== undefined &&
f.isSerialized
)
.sort((a, b) => {
return a.ordinal - b.ordinal;
});

View File

@@ -50,6 +50,53 @@ const PaymentChannel = {
},
};
let json_undefined = {
TakerPays: "223174650",
Account: "rPk2dXr27rMw9G5Ej9ad2Tt7RJzGy8ycBp",
TransactionType: "OfferCreate",
Memos: [
{
Memo: {
MemoType: "584D4D2076616C7565",
MemoData: "322E3230393635",
MemoFormat: undefined,
},
},
],
Fee: "15",
OfferSequence: undefined,
TakerGets: {
currency: "XMM",
value: "100",
issuer: "rExAPEZvbkZqYPuNcZ7XEBLENEshsWDQc8",
},
Flags: 524288,
Sequence: undefined,
LastLedgerSequence: 6220135,
};
let json_omitted = {
TakerPays: "223174650",
Account: "rPk2dXr27rMw9G5Ej9ad2Tt7RJzGy8ycBp",
TransactionType: "OfferCreate",
Memos: [
{
Memo: {
MemoType: "584D4D2076616C7565",
MemoData: "322E3230393635",
},
},
],
Fee: "15",
TakerGets: {
currency: "XMM",
value: "100",
issuer: "rExAPEZvbkZqYPuNcZ7XEBLENEshsWDQc8",
},
Flags: 524288,
LastLedgerSequence: 6220135,
};
const NegativeUNL = require("./fixtures/negative-unl.json");
function bytesListTest() {
@@ -179,6 +226,15 @@ function NegativeUNLTest() {
});
}
function omitUndefinedTest() {
test("omits fields with undefined value", () => {
let encodedOmitted = encode(json_omitted);
let encodedUndefined = encode(json_undefined);
expect(encodedOmitted).toEqual(encodedUndefined);
expect(decode(encodedOmitted)).toEqual(decode(encodedUndefined));
});
}
describe("Binary Serialization", function () {
describe("nestedObjectTests", () => nestedObjectTests());
describe("BytesList", () => bytesListTest());
@@ -188,4 +244,5 @@ describe("Binary Serialization", function () {
describe("Escrow", () => EscrowTest());
describe("PaymentChannel", () => PaymentChannelTest());
describe("NegativeUNLTest", () => NegativeUNLTest());
describe("OmitUndefined", () => omitUndefinedTest());
});