Adjust support fo US English as per substrate (#811)

* Rename ExtinsicStatus Finalised -> Finalized

* Finalised heads -> Finalized
This commit is contained in:
Jaco Greeff
2019-03-29 14:12:56 +01:00
committed by GitHub
parent 2528ac4bd5
commit 6fef86cc78
26 changed files with 193 additions and 111 deletions
+26 -13
View File
@@ -20,6 +20,10 @@ type Decoded = {
value: Codec
};
type Aliasses = {
[index: string]: string
};
/**
* @name EnumType
* @description
@@ -34,8 +38,8 @@ export default class EnumType<T> extends Base<Codec> implements Codec {
private _index: number;
private _indexes: Array<number>;
constructor (def: TypesDef, value?: any, index?: number | EnumType<T>) {
const decoded = EnumType.decodeEnumType(def, value, index);
constructor (def: TypesDef, value?: any, index?: number | EnumType<T>, aliasses?: Aliasses) {
const decoded = EnumType.decodeEnumType(def, aliasses || {}, value, index);
super(decoded.value);
@@ -44,7 +48,7 @@ export default class EnumType<T> extends Base<Codec> implements Codec {
this._index = this._indexes.indexOf(decoded.index) || 0;
}
private static decodeEnumType<T> (def: TypesDef, value?: any, index?: number | EnumType<T>): Decoded {
private static decodeEnumType<T> (def: TypesDef, aliasses: Aliasses, value?: any, index?: number | EnumType<T>): Decoded {
// If `index` is set, we parse it.
if (index instanceof EnumType) {
return EnumType.createValue(def, index._index, index.raw);
@@ -53,10 +57,10 @@ export default class EnumType<T> extends Base<Codec> implements Codec {
}
// Or else, we just look at `value`
return EnumType.decodeViaValue(def, value);
return EnumType.decodeViaValue(def, aliasses, value);
}
private static decodeViaValue (def: TypesDef, value?: any): Decoded {
private static decodeViaValue (def: TypesDef, aliasses: Aliasses, value?: any): Decoded {
if (value instanceof EnumType) {
return EnumType.createValue(def, value._index, value.raw);
} else if (isU8a(value)) {
@@ -67,26 +71,35 @@ export default class EnumType<T> extends Base<Codec> implements Codec {
const _str = value.toString();
return isHex(_str)
? EnumType.decodeViaValue(def, hexToU8a(_str))
: EnumType.createViaJSON(def, _str);
? EnumType.decodeViaValue(def, aliasses, hexToU8a(_str))
: EnumType.createViaJSON(def, aliasses, _str);
} else if (isObject(value)) {
const key = Object.keys(value)[0];
return EnumType.createViaJSON(def, key, value[key]);
return EnumType.createViaJSON(def, aliasses, key, value[key]);
}
// Worst-case scenario, return the first with default
return EnumType.createValue(def, 0);
}
private static createViaJSON (def: TypesDef, key: string, value?: any) {
private static createViaJSON (def: TypesDef, aliasses: Aliasses, key: string, value?: any) {
// JSON comes in the form of { "<type (lowercased)>": "<value for type>" }, here we
// additionally force to lower to ensure forward compat
const lowerKey = key.toLowerCase();
const keys = Object.keys(def).map((k) => k.toLowerCase());
const index = keys.indexOf(lowerKey);
const keys = Object.keys(def).map((u) => u.toLowerCase());
const index = keys
.map((k) => {
const a = Object
.keys(aliasses)
.find((a) => aliasses[a].toLowerCase() === k);
assert(index !== -1, 'Unable to reliably map input on JSON');
return a
? a.toLowerCase()
: k;
})
.indexOf(key.toLowerCase());
assert(index !== -1, `Unable to reliably map input on JSON, cannot find '${key}' in ${keys.join(', ')}`);
return EnumType.createValue(def, index, value);
}
@@ -11,7 +11,7 @@ describe('ExtrinsicStatus', () => {
it('has the correct type', () => {
expect(
status.type
).toEqual('Finalised');
).toEqual('Finalized');
});
it('has the correct hash', () => {
+60 -9
View File
@@ -25,11 +25,11 @@ export class Dropped extends Null {
}
/**
* @name Finalised
* @name Finalized
* @description
* An [[ExtrinsicStats] indicating that the [[Extrinsic]]] has been finalised and included
* An [[ExtrinsicStatus] indicating that the [[Extrinsic]]] has been finalized and included
*/
export class Finalised extends Hash {
export class Finalized extends Hash {
}
/**
@@ -69,17 +69,19 @@ export class Usurped extends Hash {
* @description
* An [[EnumType]] that indicates the status of the [[Extrinsic]] as been submitted
*/
export default class ExtrinsicStatus extends EnumType<Future | Ready | Finalised | Usurped | Broadcast | Dropped | Invalid> {
export default class ExtrinsicStatus extends EnumType<Future | Ready | Finalized | Usurped | Broadcast | Dropped | Invalid> {
constructor (value: any, index?: number) {
super({
Future,
Ready,
Finalised,
Finalized,
Usurped,
Broadcast,
Dropped,
Invalid
}, value, index);
}, value, index, {
'Finalised': 'Finalized' // old UK spelling to US spelling
});
}
/**
@@ -97,10 +99,10 @@ export default class ExtrinsicStatus extends EnumType<Future | Ready | Finalised
}
/**
* @description Returns the item as a [[Finalised]]
* @description Returns the item as a [[Finalized]]
*/
get asFinalised (): Finalised {
return this.value as Finalised;
get asFinalized (): Finalized {
return this.value as Finalized;
}
/**
@@ -130,4 +132,53 @@ export default class ExtrinsicStatus extends EnumType<Future | Ready | Finalised
get asUsurped (): Usurped {
return this.value as Usurped;
}
/**
* @description Returns true if the status is boadcast
*/
get isBroadcast (): boolean {
return this.type === 'Broadcast';
}
/**
* @description Returns true if the status is dropped
*/
get isDropped (): boolean {
return this.type === 'Dropped';
}
/**
* @description Returns true if the status is finalized
*/
get isFinalized (): boolean {
return this.type === 'Finalized';
}
/**
* @description Returns true if the status is future
*/
get isFuture (): boolean {
return this.type === 'Future';
}
/**
* @description Returns true if the status is invalid
*/
get isInvalid (): boolean {
return this.type === 'Invalid';
}
/**
* @description Returns true if the status is eady
*/
get isReady (): boolean {
return this.type === 'Ready';
}
/**
* @description Returns true if the status is usurped
*/
get isUsurped (): boolean {
return this.type === 'Usurped';
}
}