All Downloads are FREE. Search and download functionalities are using the official Maven repository.

js.share.common-utils.js Maven / Gradle / Ivy

The newest version!
/**
 * @namespace
 */
sahagin = {};

sahagin.CommonUtils = {};

/**
 * @returns {string}
 */
sahagin.CommonUtils.formatVersion = function() {
   return "0.9.2";
};

/**
 * copied from goog.inherits.
 * see http://docs.closure-library.googlecode.com/git/namespace_goog.html
 */
sahagin.inherits = function(childCtor, parentCtor) {
  function tempCtor() {};
  tempCtor.prototype = parentCtor.prototype;
  childCtor.superClass_ = parentCtor.prototype;
  childCtor.prototype = new tempCtor();
  childCtor.prototype.constructor = childCtor;
  childCtor.base = function(me, methodName, var_args) {
    var args = Array.prototype.slice.call(arguments, 2);
    return parentCtor.prototype[methodName].apply(me, args);
  };
};

/**
 * copied from goog.base.
 * see http://docs.closure-library.googlecode.com/git/namespace_goog.html
 */
sahagin.base = function(me, opt_methodName, var_args) {
  var caller = arguments.callee.caller;

  if (caller.superClass_) {
    // This is a constructor. Call the superclass constructor.
    return caller.superClass_.constructor.apply(
        me, Array.prototype.slice.call(arguments, 1));
  }

  var args = Array.prototype.slice.call(arguments, 2);
  var foundCaller = false;
  for (var ctor = me.constructor;
       ctor; ctor = ctor.superClass_ && ctor.superClass_.constructor) {
    if (ctor.prototype[opt_methodName] === caller) {
      foundCaller = true;
    } else if (foundCaller) {
      return ctor.prototype[opt_methodName].apply(me, args);
    }
  }

  if (me[opt_methodName] === caller) {
    return me.constructor.prototype[opt_methodName].apply(me, args);
  } else {
    throw Error(
        'sahagin.base called from a method of one name ' +
        'to a method of a different name');
  }
};

/**
 * @param {string} format
 * @param {...*} args
 * @returns {string}
 */
sahagin.CommonUtils.strFormat = function(format, args) {
  if (!format) {
    return format;
  }
  // use arguments to get all optional arguments
  var formatArgs = arguments;
  return format.replace(/{(\d+)}/g, function(match, number) {
    return formatArgs[parseInt(number) + 1];
  });
};

/**
 * @param {string} str
 * @param {string} pattern
 * @returns {boolean} true if str starts with pattern
 */
sahagin.CommonUtils.startsWith = function(str, pattern) {
  if (pattern == null || pattern == "" || pattern == undefined) {
    throw new Error("invalid pattern: " + pattern);
  }
  if (str == null || str == undefined) {
    return false;
  }
  return (str.lastIndexOf(pattern, 0) == 0);
};




© 2015 - 2024 Weber Informatics LLC | Privacy Policy