add new toTopic formatter in filters

This commit is contained in:
Fabian Vogelsteller 2015-04-22 12:54:48 +02:00
parent f37057e80b
commit a6ac6ed36a
3 changed files with 19 additions and 7 deletions

View File

@ -198,9 +198,6 @@ var fromDecimal = function (value) {
var toHex = function (val) {
/*jshint maxcomplexity:7 */
if(val === null || typeof val === 'undefined')
return val;
if (isBoolean(val))
return fromDecimal(+val);

View File

@ -28,6 +28,22 @@ var RequestManager = require('./requestmanager');
var formatters = require('./formatters');
var utils = require('../utils/utils');
/**
* Converts a given topic to a hex string, but also allows null values.
*
* @param {Mixed} value
* @return {String}
*/
var toTopic = function(value){
if(value === null || typeof value === 'undefined')
return value;
else if(value.indexOf('0x') === 0)
return value;
else
return utils.fromAscii(String(value));
};
/// This method should be called on options object, to verify deprecated properties && lazy load dynamic ones
/// @param should be string or object
/// @returns options string or object
@ -42,9 +58,9 @@ var getOptions = function (options) {
// make sure topics, get converted to hex
options.topics = options.topics || [];
options.topics = options.topics.map(function(topic){
return (utils.isArray(topic))
? topic.map(utils.toHex)
: utils.toHex(topic);
return (utils.isArray(topic))
? topic.map(toTopic)
: toTopic(topic);
});
// lazy load

View File

@ -4,7 +4,6 @@ var BigNumber = require('bignumber.js');
var assert = chai.assert;
var tests = [
{ value: null, expected: null },
{ value: 1, expected: '0x1' },
{ value: '1', expected: '0x1' },
{ value: '0x1', expected: '0x1'},