attila/savvior

View on GitHub
src/Helpers.js

Summary

Maintainability
A
0 mins
Test Coverage
/*jshint unused:false */

function addToDataset(element, key, value, forceCompat) {
  // Use dataset property or a fallback if unsupported.
  if (forceCompat || !element.dataset) {
    element.setAttribute('data-' + key, value);
  }
  else {
    element.dataset[key] = value;
  }

  return;
}

/**
 * Helper function for iterating over a collection
 *
 * @param collection
 * @param fn
 * @param scope
 */
function each(collection, fn, scope) {
  var i = 0,
    cont;

  for (i; i < collection.length; i++) {
    cont = fn.call(scope, collection[i], i);
    if (cont === false) {
      break; //allow early exit
    }
  }
}

/**
 * Helper function for determining whether target object is a function
 *
 * @param target the object under test
 * @return {Boolean} true if function, false otherwise
 */
function isFunction(target) {
  return typeof target === 'function';
}

/**
 * Helper function to determine if an object or array is empty.
 *
 * @param  {[type]}  obj The object or array to check.
 * @return {Boolean}     TRUE if empty, FALSE if not.
 */
function isEmpty(obj, p) {
  for (p in obj) {
    return !1;
  }
  return !0;
}

/**
 * Dirt simple extend.
 *
 * @param  {Object} target
 * @param  {Object} source
 * @return {Object}
 */
function extend (source, target) {
  for (var prop in source) {
    target[prop] = source[prop];
  }

  return target;
}