github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/cmd/mist/assets/ext/ethereum.js/lib/utils.js (about)

     1  /*
     2      This file is part of ethereum.js.
     3  
     4      ethereum.js is free software: you can redistribute it and/or modify
     5      it under the terms of the GNU Lesser General Public License as published by
     6      the Free Software Foundation, either version 3 of the License, or
     7      (at your option) any later version.
     8  
     9      ethereum.js is distributed in the hope that it will be useful,
    10      but WITHOUT ANY WARRANTY; without even the implied warranty of
    11      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12      GNU Lesser General Public License for more details.
    13  
    14      You should have received a copy of the GNU Lesser General Public License
    15      along with ethereum.js.  If not, see <http://www.gnu.org/licenses/>.
    16  */
    17  /** @file utils.js
    18   * @authors:
    19   *   Marek Kotewicz <marek@ethdev.com>
    20   * @date 2015
    21   */
    22  
    23  var c = require('./const');
    24  
    25  /// Finds first index of array element matching pattern
    26  /// @param array
    27  /// @param callback pattern
    28  /// @returns index of element
    29  var findIndex = function (array, callback) {
    30      var end = false;
    31      var i = 0;
    32      for (; i < array.length && !end; i++) {
    33          end = callback(array[i]);
    34      }
    35      return end ? i - 1 : -1;
    36  };
    37  
    38  /// @returns ascii string representation of hex value prefixed with 0x
    39  var toAscii = function(hex) {
    40  // Find termination
    41      var str = "";
    42      var i = 0, l = hex.length;
    43      if (hex.substring(0, 2) === '0x') {
    44          i = 2;
    45      }
    46      for (; i < l; i+=2) {
    47          var code = parseInt(hex.substr(i, 2), 16);
    48          if (code === 0) {
    49              break;
    50          }
    51  
    52          str += String.fromCharCode(code);
    53      }
    54  
    55      return str;
    56  };
    57      
    58  var toHex = function(str) {
    59      var hex = "";
    60      for(var i = 0; i < str.length; i++) {
    61          var n = str.charCodeAt(i).toString(16);
    62          hex += n.length < 2 ? '0' + n : n;
    63      }
    64  
    65      return hex;
    66  };
    67  
    68  /// @returns hex representation (prefixed by 0x) of ascii string
    69  var fromAscii = function(str, pad) {
    70      pad = pad === undefined ? 0 : pad;
    71      var hex = toHex(str);
    72      while (hex.length < pad*2)
    73          hex += "00";
    74      return "0x" + hex;
    75  };
    76  
    77  /// @returns display name for function/event eg. multiply(uint256) -> multiply
    78  var extractDisplayName = function (name) {
    79      var length = name.indexOf('('); 
    80      return length !== -1 ? name.substr(0, length) : name;
    81  };
    82  
    83  /// @returns overloaded part of function/event name
    84  var extractTypeName = function (name) {
    85      /// TODO: make it invulnerable
    86      var length = name.indexOf('(');
    87      return length !== -1 ? name.substr(length + 1, name.length - 1 - (length + 1)).replace(' ', '') : "";
    88  };
    89  
    90  /// Filters all function from input abi
    91  /// @returns abi array with filtered objects of type 'function'
    92  var filterFunctions = function (json) {
    93      return json.filter(function (current) {
    94          return current.type === 'function'; 
    95      }); 
    96  };
    97  
    98  /// Filters all events form input abi
    99  /// @returns abi array with filtered objects of type 'event'
   100  var filterEvents = function (json) {
   101      return json.filter(function (current) {
   102          return current.type === 'event';
   103      });
   104  };
   105  
   106  /// used to transform value/string to eth string
   107  /// TODO: use BigNumber.js to parse int
   108  /// TODO: add tests for it!
   109  var toEth = function (str) {
   110      var val = typeof str === "string" ? str.indexOf('0x') === 0 ? parseInt(str.substr(2), 16) : parseInt(str) : str;
   111      var unit = 0;
   112      var units = c.ETH_UNITS;
   113      while (val > 3000 && unit < units.length - 1)
   114      {
   115          val /= 1000;
   116          unit++;
   117      }
   118      var s = val.toString().length < val.toFixed(2).length ? val.toString() : val.toFixed(2);
   119      var replaceFunction = function($0, $1, $2) {
   120          return $1 + ',' + $2;
   121      };
   122  
   123      while (true) {
   124          var o = s;
   125          s = s.replace(/(\d)(\d\d\d[\.\,])/, replaceFunction);
   126          if (o === s)
   127              break;
   128      }
   129      return s + ' ' + units[unit];
   130  };
   131  
   132  module.exports = {
   133      findIndex: findIndex,
   134      toAscii: toAscii,
   135      fromAscii: fromAscii,
   136      extractDisplayName: extractDisplayName,
   137      extractTypeName: extractTypeName,
   138      filterFunctions: filterFunctions,
   139      filterEvents: filterEvents,
   140      toEth: toEth
   141  };
   142