Add `empty` (monoid identity) to address modules (#392)
Summary: This can make invocations of `FooAddress.fromParts([])` a bit more succinct. Paired with @decentralion. Test Plan: Unit tests added. Run `yarn travis`. wchargin-branch: address-empty
This commit is contained in:
parent
7199586262
commit
6ba6d885ad
|
@ -18,6 +18,12 @@ export interface AddressModule<Address> {
|
|||
*/
|
||||
assertValidParts(parts: $ReadOnlyArray<string>, what?: string): void;
|
||||
|
||||
/**
|
||||
* The empty address (the identity for `append`). Equivalent to
|
||||
* `fromParts([])`.
|
||||
*/
|
||||
empty: Address;
|
||||
|
||||
/**
|
||||
* Convert an array of address parts to an address. The input must be
|
||||
* a non-null array of non-null strings, none of which contains the
|
||||
|
@ -184,6 +190,8 @@ export function makeAddressModule(options: Options): AddressModule<string> {
|
|||
return nonce + separator + nullDelimited(parts);
|
||||
}
|
||||
|
||||
const empty = fromParts([]);
|
||||
|
||||
function toParts(address: Address): string[] {
|
||||
assertValid(address);
|
||||
const parts = address.split(separator);
|
||||
|
@ -210,6 +218,7 @@ export function makeAddressModule(options: Options): AddressModule<string> {
|
|||
const result = {
|
||||
assertValid,
|
||||
assertValidParts,
|
||||
empty,
|
||||
fromParts,
|
||||
toParts,
|
||||
toString,
|
||||
|
|
|
@ -154,6 +154,16 @@ describe("core/address", () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe("empty", () => {
|
||||
const {FooAddress} = makeModules();
|
||||
it("is a valid address", () => {
|
||||
FooAddress.assertValid(FooAddress.empty);
|
||||
});
|
||||
it("has empty parts", () => {
|
||||
expect(FooAddress.toParts(FooAddress.empty)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("fromParts", () => {
|
||||
const {FooAddress, BarAddress} = makeModules();
|
||||
|
||||
|
|
Loading…
Reference in New Issue