From a6ac6ed36ad744163cbef382049240e513f755d2 Mon Sep 17 00:00:00 2001 From: Fabian Vogelsteller Date: Wed, 22 Apr 2015 12:54:48 +0200 Subject: [PATCH] add new toTopic formatter in filters --- lib/utils/utils.js | 3 --- lib/web3/filter.js | 22 +++++++++++++++++++--- test/utils.toHex.js | 1 - 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/utils/utils.js b/lib/utils/utils.js index dec069b..e37421b 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -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); diff --git a/lib/web3/filter.js b/lib/web3/filter.js index 7280c80..0bebf4d 100644 --- a/lib/web3/filter.js +++ b/lib/web3/filter.js @@ -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 diff --git a/test/utils.toHex.js b/test/utils.toHex.js index 610f368..0a328e3 100644 --- a/test/utils.toHex.js +++ b/test/utils.toHex.js @@ -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'},