Skip to content

Commit 6b440d5

Browse files
committed
allow specifying alias to override all of type e.g bytes or uint int
1 parent c94440f commit 6b440d5

File tree

5 files changed

+60
-8
lines changed

5 files changed

+60
-8
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ const mapper = new Mapper();
2525
```JavaScript
2626
const mapper = new Mapper({
2727
types: {
28+
// convert ALL int types to string
29+
int: function (val) {
30+
return Web3.utils.BN(val).toString();
31+
},
2832
// convert all uint256 to string; default is BN
2933
uint256: function(val) {
3034
return Web3.utils.BN(val).toString();

example.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use strict';
22

3-
const Mapper = require('./');
43
const Web3 = require('web3');
4+
const moment = require('moment');
5+
6+
const Mapper = require('./');
57

68
function bNToString(val) {
79
return Web3.utils.BN(val).toString();
@@ -11,19 +13,28 @@ const mapper = new Mapper({
1113
mapping: {
1214
state: [
1315
{
14-
key: 'state',
16+
key: 'stateName',
1517
transform: function (val) {
1618
const index = Number(val.toString());
1719
const states = ['Funding', 'Active', 'Matured'];
1820
return states[index];
1921
},
2022
},
21-
'stateName',
23+
{
24+
key: 'state',
25+
transform: val => val.toNumber(),
26+
},
2227
],
28+
maturityDeadline: {
29+
transform: function (val) {
30+
return moment.unix(val.toNumber());
31+
},
32+
},
2333
},
2434
types: {
25-
int8: bNToString,
26-
uint256: bNToString,
35+
bytes: Web3.utils.hexToUtf8,
36+
int: bNToString,
37+
uint: bNToString,
2738
},
2839
workingDirectory: __dirname,
2940
});

lib/object-mapper.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,29 @@ class TruffleObjectMapper {
6666
&& !node.inputs.length);
6767
}
6868

69-
getDefaultTransformation(type) {
70-
return this._types[type] || noop;
69+
getAliasForType(type) {
70+
const aliases = Object.keys(TruffleObjectMapper.typeAliasesRegex);
71+
for (let i = 0; i < aliases.length; i++) {
72+
const alias = aliases[i];
73+
const regex = TruffleObjectMapper.typeAliasesRegex[alias];
74+
if (regex.test(type)) {
75+
return alias;
76+
}
77+
}
78+
return false;
79+
}
80+
81+
getDefaultTransformationForAlias(type) {
82+
const alias = this.getAliasForType(type);
83+
if (!alias) {
84+
return false;
85+
}
86+
return this._types[alias];
87+
}
88+
89+
getDefaultTransformationForType(type) {
90+
const defaultForAlias = this.getDefaultTransformationForAlias(type);
91+
return this._types[type] ? this._types[type] : defaultForAlias || noop;
7192
}
7293

7394
async map(contractName, at, mapping = {}) {
@@ -100,7 +121,11 @@ class TruffleObjectMapper {
100121
}
101122

102123
buildOutputMapping(key, type, customMapping) {
103-
const transform = this.getDefaultTransformation(type);
124+
// order:
125+
// - exact match on type, ex: bytes32
126+
// - general match on alias, ex: uint8 matches uint
127+
// - noop
128+
const transform = this.getDefaultTransformationForType(type);
104129
const defaultMapping = { key, transform };
105130
if (!customMapping) {
106131
return defaultMapping;
@@ -119,6 +144,12 @@ class TruffleObjectMapper {
119144
}
120145
}
121146

147+
TruffleObjectMapper.typeAliasesRegex = {
148+
bytes: /^bytes/,
149+
int: /^int/,
150+
uint: /^uint/,
151+
};
152+
122153
TruffleObjectMapper.types = types;
123154

124155
module.exports = TruffleObjectMapper;

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"camel-case": "^4.1.0",
2929
"deepmerge": "^4.2.2",
3030
"glob": "^7.1.6",
31+
"moment": "^2.24.0",
3132
"object-mapper": "^6.0.1",
3233
"truffle-hdwallet-provider-privkey": "^0.3.0",
3334
"web3": "^1.2.4"

0 commit comments

Comments
 (0)