@polkadot/types
Version:
Implementation of the Parity codec
48 lines (47 loc) • 1.83 kB
JavaScript
import { getTypeDef, TypeDefInfo } from '@polkadot/types-create';
function extractSubSingle(_, { sub }) {
const { lookupName, type } = sub;
return extractTypes([lookupName || type]);
}
function extractSubArray(_, { sub }) {
return extractTypes(sub.map(({ lookupName, type }) => lookupName || type));
}
function unhandled(type, { info }) {
throw new Error(`Unhandled: Unable to create and validate type from ${type} (info=${TypeDefInfo[info]})`);
}
const mapping = {
[ ]: extractSubArray,
[ ]: extractSubSingle,
[ ]: extractSubSingle,
[ ]: unhandled,
[ ]: extractSubArray,
[ ]: extractSubArray,
[ ]: unhandled,
[ ]: extractSubSingle,
[ ]: unhandled,
[ ]: extractSubSingle,
[ ]: (_, typeDef) => typeDef.lookupName || typeDef.type,
[ ]: extractSubSingle,
[ ]: extractSubSingle,
[ ]: extractSubArray,
[ ]: extractSubArray,
[ ]: unhandled,
[ ]: extractSubArray,
[ ]: extractSubArray,
[ ]: unhandled,
[ ]: extractSubSingle,
[ ]: extractSubSingle,
[ ]: extractSubSingle,
[ ]: extractSubSingle
};
/** @internal */
export function extractTypes(types) {
const count = types.length;
const result = new Array(count);
for (let i = 0; i < count; i++) {
const type = types[i];
const typeDef = getTypeDef(type);
result[i] = mapping[typeDef.info](type, typeDef);
}
return result;
}