diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-03-28 17:05:36 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-04-02 17:34:29 +0800 |
commit | 4d9029bb0e3b215efdf165704c80d3bacef0e85a (patch) | |
tree | 8f699dcd0a7b5fba3c31c065a62d640407bba8c3 /packages/base-contract/src/utils.ts | |
parent | bc49dde4d5d24b9a8b01459bde061f9b23fb5898 (diff) | |
download | dexon-0x-contracts-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar.gz dexon-0x-contracts-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar.zst dexon-0x-contracts-4d9029bb0e3b215efdf165704c80d3bacef0e85a.zip |
Add metacoin example project
Diffstat (limited to 'packages/base-contract/src/utils.ts')
-rw-r--r-- | packages/base-contract/src/utils.ts | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/packages/base-contract/src/utils.ts b/packages/base-contract/src/utils.ts new file mode 100644 index 000000000..4b86bb1ad --- /dev/null +++ b/packages/base-contract/src/utils.ts @@ -0,0 +1,25 @@ +import { DataItem } from '@0xproject/types'; +import * as _ from 'lodash'; + +// tslint:disable-next-line:completed-docs +export function formatABIDataItem(abi: DataItem, value: any, formatter: (type: string, value: any) => any): any { + const trailingArrayRegex = /\[\d*\]$/; + if (abi.type.match(trailingArrayRegex)) { + const arrayItemType = abi.type.replace(trailingArrayRegex, ''); + return _.map(value, val => { + const arrayItemAbi = { + ...abi, + type: arrayItemType, + }; + return formatABIDataItem(arrayItemAbi, val, formatter); + }); + } else if (abi.type === 'tuple') { + const formattedTuple: { [componentName: string]: DataItem } = {}; + _.forEach(abi.components, componentABI => { + formattedTuple[componentABI.name] = formatABIDataItem(componentABI, value[componentABI.name], formatter); + }); + return formattedTuple; + } else { + return formatter(abi.type, value); + } +} |