github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/integration/messagebus/glue/client/src/utils.js (about)

     1  /*
     2   *  Glue - Robust Go and Javascript Socket Library
     3   *  Copyright (C) 2015  Roland Singer <roland.singer[at]desertbit.com>
     4   *
     5   *  This program is free software: you can redistribute it and/or modify
     6   *  it under the terms of the GNU General Public License as published by
     7   *  the Free Software Foundation, either version 3 of the License, or
     8   *  (at your option) any later version.
     9   *
    10   *  This program is distributed in the hope that it will be useful,
    11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   *  GNU General Public License for more details.
    14   *
    15   *  You should have received a copy of the GNU General Public License
    16   *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   */
    18  
    19  /*
    20   *  This code lives inside the glue function.
    21   */
    22  
    23  var utils = (function() {
    24      /*
    25       * Constants
    26       */
    27  
    28      var Delimiter = "&";
    29  
    30  
    31  
    32      /*
    33       * Variables
    34       */
    35  
    36       var instance = {}; // Our public instance object returned by this function.
    37  
    38  
    39  
    40      /*
    41       * Public Methods
    42       */
    43  
    44      // Mimics jQuery's extend method.
    45      // Source: http://stackoverflow.com/questions/11197247/javascript-equivalent-of-jquerys-extend-method
    46      instance.extend = function() {
    47        for(var i=1; i<arguments.length; i++)
    48            for(var key in arguments[i])
    49                if(arguments[i].hasOwnProperty(key))
    50                    arguments[0][key] = arguments[i][key];
    51        return arguments[0];
    52      };
    53  
    54      // Source: http://stackoverflow.com/questions/5999998/how-can-i-check-if-a-javascript-variable-is-function-type.
    55      instance.isFunction = function(v) {
    56          var getType = {};
    57          return v && getType.toString.call(v) === '[object Function]';
    58      };
    59  
    60      // unmarshalValues splits two values from a single string.
    61      // This function is chainable to extract multiple values.
    62      // An object with two strings (first, second) is returned.
    63      instance.unmarshalValues = function(data) {
    64          if (!data) {
    65              return false;
    66          }
    67  
    68          // Find the delimiter position.
    69          var pos = data.indexOf(Delimiter);
    70  
    71          // Extract the value length integer of the first value.
    72          var len = parseInt(data.substring(0, pos), 10);
    73          data = data.substring(pos + 1);
    74  
    75          // Validate the length.
    76          if (len < 0 || len > data.length) {
    77              return false;
    78          }
    79  
    80          // Now split the first value from the second.
    81          var firstV = data.substr(0, len);
    82          var secondV = data.substr(len);
    83  
    84          // Return an object with both values.
    85          return {
    86              first:  firstV,
    87              second: secondV
    88          };
    89      };
    90  
    91      // marshalValues joins two values into a single string.
    92      // They can be decoded by the unmarshalValues function.
    93      instance.marshalValues = function(first, second) {
    94          return String(first.length) + Delimiter + first + second;
    95      };
    96  
    97  
    98      return instance;
    99  })();