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:
William Chargin 2018-06-14 13:06:26 -07:00 committed by GitHub
parent 7199586262
commit 6ba6d885ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View File

@ -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,

View File

@ -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();