utils cleanup

This commit is contained in:
Marek Kotewicz 2015-04-20 23:01:21 +02:00
parent a88be00cef
commit 501e3bc56e
12 changed files with 85 additions and 204 deletions

69
dist/web3-light.js vendored
View File

@ -817,36 +817,8 @@ var getConstructor = function (abi, numberOfArgs) {
})[0]; })[0];
}; };
/**
* Filters all functions from input abi
*
* @method filterFunctions
* @param {Array} abi
* @returns {Array} abi array with filtered objects of type 'function'
*/
var filterFunctions = function (json) {
return json.filter(function (current) {
return current.type === 'function';
});
};
/**
* Filters all events from input abi
*
* @method filterEvents
* @param {Array} abi
* @returns {Array} abi array with filtered objects of type 'event'
*/
var filterEvents = function (json) {
return json.filter(function (current) {
return current.type === 'event';
});
};
module.exports = { module.exports = {
getConstructor: getConstructor, getConstructor: getConstructor
filterFunctions: filterFunctions,
filterEvents: filterEvents
}; };
@ -950,9 +922,9 @@ module.exports = {
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>. along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
*/ */
/** @file utils.js /**
* @authors: * @file utils.js
* Marek Kotewicz <marek@ethdev.com> * @author Marek Kotewicz <marek@ethdev.com>
* @date 2015 * @date 2015
*/ */
@ -1078,6 +1050,22 @@ var fromAscii = function(str, pad) {
return "0x" + hex; return "0x" + hex;
}; };
/**
* Should be used to create full function/event name from json abi
*
* @method transformToFullName
* @param {Object} json-abi
* @return {String} full fnction/event name
*/
var transformToFullName = function (json) {
if (json.name.indexOf('(') !== -1) {
return json.name;
}
var typeName = json.inputs.map(function(i){return i.type; }).join();
return json.name + '(' + typeName + ')';
};
/** /**
* Should be called to get display name of contract function * Should be called to get display name of contract function
* *
@ -1390,6 +1378,7 @@ module.exports = {
fromDecimal: fromDecimal, fromDecimal: fromDecimal,
toAscii: toAscii, toAscii: toAscii,
fromAscii: fromAscii, fromAscii: fromAscii,
transformToFullName: transformToFullName,
extractDisplayName: extractDisplayName, extractDisplayName: extractDisplayName,
extractTypeName: extractTypeName, extractTypeName: extractTypeName,
toWei: toWei, toWei: toWei,
@ -1660,18 +1649,6 @@ var contract = function (abi) {
var Contract = function (abi, options) { var Contract = function (abi, options) {
// workaround for invalid assumption that method.name is the full anonymous prototype of the method.
// it's not. it's just the name. the rest of the code assumes it's actually the anonymous
// prototype, so we make it so as a workaround.
// TODO: we may not want to modify input params, maybe use copy instead?
abi.forEach(function (method) {
if (method.name.indexOf('(') === -1) {
var displayName = method.name;
var typeName = method.inputs.map(function(i){return i.type; }).join();
method.name = displayName + '(' + typeName + ')';
}
});
this.address = ''; this.address = '';
this._isTransaction = null; this._isTransaction = null;
this._options = {}; this._options = {};
@ -2091,7 +2068,7 @@ var web3 = require('../web3');
*/ */
var SolidityEvent = function (json, address) { var SolidityEvent = function (json, address) {
this._params = json.inputs; this._params = json.inputs;
this._name = json.name; this._name = utils.transformToFullName(json);
this._address = address; this._address = address;
}; };
@ -2633,7 +2610,7 @@ var SolidityFunction = function (json) {
return i.type; return i.type;
}); });
this._constant = json.constant; this._constant = json.constant;
this._name = json.name; this._name = utils.transformToFullName(json);
}; };
/** /**

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

69
dist/web3.js vendored
View File

@ -817,36 +817,8 @@ var getConstructor = function (abi, numberOfArgs) {
})[0]; })[0];
}; };
/**
* Filters all functions from input abi
*
* @method filterFunctions
* @param {Array} abi
* @returns {Array} abi array with filtered objects of type 'function'
*/
var filterFunctions = function (json) {
return json.filter(function (current) {
return current.type === 'function';
});
};
/**
* Filters all events from input abi
*
* @method filterEvents
* @param {Array} abi
* @returns {Array} abi array with filtered objects of type 'event'
*/
var filterEvents = function (json) {
return json.filter(function (current) {
return current.type === 'event';
});
};
module.exports = { module.exports = {
getConstructor: getConstructor, getConstructor: getConstructor
filterFunctions: filterFunctions,
filterEvents: filterEvents
}; };
@ -950,9 +922,9 @@ module.exports = {
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>. along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
*/ */
/** @file utils.js /**
* @authors: * @file utils.js
* Marek Kotewicz <marek@ethdev.com> * @author Marek Kotewicz <marek@ethdev.com>
* @date 2015 * @date 2015
*/ */
@ -1078,6 +1050,22 @@ var fromAscii = function(str, pad) {
return "0x" + hex; return "0x" + hex;
}; };
/**
* Should be used to create full function/event name from json abi
*
* @method transformToFullName
* @param {Object} json-abi
* @return {String} full fnction/event name
*/
var transformToFullName = function (json) {
if (json.name.indexOf('(') !== -1) {
return json.name;
}
var typeName = json.inputs.map(function(i){return i.type; }).join();
return json.name + '(' + typeName + ')';
};
/** /**
* Should be called to get display name of contract function * Should be called to get display name of contract function
* *
@ -1390,6 +1378,7 @@ module.exports = {
fromDecimal: fromDecimal, fromDecimal: fromDecimal,
toAscii: toAscii, toAscii: toAscii,
fromAscii: fromAscii, fromAscii: fromAscii,
transformToFullName: transformToFullName,
extractDisplayName: extractDisplayName, extractDisplayName: extractDisplayName,
extractTypeName: extractTypeName, extractTypeName: extractTypeName,
toWei: toWei, toWei: toWei,
@ -1660,18 +1649,6 @@ var contract = function (abi) {
var Contract = function (abi, options) { var Contract = function (abi, options) {
// workaround for invalid assumption that method.name is the full anonymous prototype of the method.
// it's not. it's just the name. the rest of the code assumes it's actually the anonymous
// prototype, so we make it so as a workaround.
// TODO: we may not want to modify input params, maybe use copy instead?
abi.forEach(function (method) {
if (method.name.indexOf('(') === -1) {
var displayName = method.name;
var typeName = method.inputs.map(function(i){return i.type; }).join();
method.name = displayName + '(' + typeName + ')';
}
});
this.address = ''; this.address = '';
this._isTransaction = null; this._isTransaction = null;
this._options = {}; this._options = {};
@ -2091,7 +2068,7 @@ var web3 = require('../web3');
*/ */
var SolidityEvent = function (json, address) { var SolidityEvent = function (json, address) {
this._params = json.inputs; this._params = json.inputs;
this._name = json.name; this._name = utils.transformToFullName(json);
this._address = address; this._address = address;
}; };
@ -2633,7 +2610,7 @@ var SolidityFunction = function (json) {
return i.type; return i.type;
}); });
this._constant = json.constant; this._constant = json.constant;
this._name = json.name; this._name = utils.transformToFullName(json);
}; };
/** /**

12
dist/web3.js.map vendored

File diff suppressed because one or more lines are too long

5
dist/web3.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -34,35 +34,7 @@ var getConstructor = function (abi, numberOfArgs) {
})[0]; })[0];
}; };
/**
* Filters all functions from input abi
*
* @method filterFunctions
* @param {Array} abi
* @returns {Array} abi array with filtered objects of type 'function'
*/
var filterFunctions = function (json) {
return json.filter(function (current) {
return current.type === 'function';
});
};
/**
* Filters all events from input abi
*
* @method filterEvents
* @param {Array} abi
* @returns {Array} abi array with filtered objects of type 'event'
*/
var filterEvents = function (json) {
return json.filter(function (current) {
return current.type === 'event';
});
};
module.exports = { module.exports = {
getConstructor: getConstructor, getConstructor: getConstructor
filterFunctions: filterFunctions,
filterEvents: filterEvents
}; };

View File

@ -14,9 +14,9 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>. along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
*/ */
/** @file utils.js /**
* @authors: * @file utils.js
* Marek Kotewicz <marek@ethdev.com> * @author Marek Kotewicz <marek@ethdev.com>
* @date 2015 * @date 2015
*/ */
@ -142,6 +142,22 @@ var fromAscii = function(str, pad) {
return "0x" + hex; return "0x" + hex;
}; };
/**
* Should be used to create full function/event name from json abi
*
* @method transformToFullName
* @param {Object} json-abi
* @return {String} full fnction/event name
*/
var transformToFullName = function (json) {
if (json.name.indexOf('(') !== -1) {
return json.name;
}
var typeName = json.inputs.map(function(i){return i.type; }).join();
return json.name + '(' + typeName + ')';
};
/** /**
* Should be called to get display name of contract function * Should be called to get display name of contract function
* *
@ -454,6 +470,7 @@ module.exports = {
fromDecimal: fromDecimal, fromDecimal: fromDecimal,
toAscii: toAscii, toAscii: toAscii,
fromAscii: fromAscii, fromAscii: fromAscii,
transformToFullName: transformToFullName,
extractDisplayName: extractDisplayName, extractDisplayName: extractDisplayName,
extractTypeName: extractTypeName, extractTypeName: extractTypeName,
toWei: toWei, toWei: toWei,

View File

@ -76,18 +76,6 @@ var contract = function (abi) {
var Contract = function (abi, options) { var Contract = function (abi, options) {
// workaround for invalid assumption that method.name is the full anonymous prototype of the method.
// it's not. it's just the name. the rest of the code assumes it's actually the anonymous
// prototype, so we make it so as a workaround.
// TODO: we may not want to modify input params, maybe use copy instead?
abi.forEach(function (method) {
if (method.name.indexOf('(') === -1) {
var displayName = method.name;
var typeName = method.inputs.map(function(i){return i.type; }).join();
method.name = displayName + '(' + typeName + ')';
}
});
this.address = ''; this.address = '';
this._isTransaction = null; this._isTransaction = null;
this._options = {}; this._options = {};

View File

@ -29,7 +29,7 @@ var web3 = require('../web3');
*/ */
var SolidityEvent = function (json, address) { var SolidityEvent = function (json, address) {
this._params = json.inputs; this._params = json.inputs;
this._name = json.name; this._name = utils.transformToFullName(json);
this._address = address; this._address = address;
}; };

View File

@ -35,7 +35,7 @@ var SolidityFunction = function (json) {
return i.type; return i.type;
}); });
this._constant = json.constant; this._constant = json.constant;
this._name = json.name; this._name = utils.transformToFullName(json);
}; };
/** /**

View File

@ -1,49 +0,0 @@
var assert = require('assert');
var utils = require('../lib/solidity/utils');
describe('lib/utils/utils', function() {
it('should filter functions and events from input array properly', function () {
// given
var description = [{
"name": "test",
"type": "function",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
],
}, {
"name": "test2",
"type": "event",
"inputs": [{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
]
}];
// when
var events = utils.filterEvents(description);
var functions = utils.filterFunctions(description);
// then
assert.equal(events.length, 1);
assert.equal(events[0].name, 'test2');
assert.equal(functions.length, 1);
assert.equal(functions[0].name, 'test');
});
});