Skip to content

Instantly share code, notes, and snippets.

@atheiman
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atheiman/7a6a6667cf44794e7301 to your computer and use it in GitHub Desktop.
Save atheiman/7a6a6667cf44794e7301 to your computer and use it in GitHub Desktop.
js-utils
function debug(logString) {
DEBUG = (typeof DEBUG === "undefined") ? true : DEBUG;
if (DEBUG)
console.log('DEBUG: ' + logString);
}
function checkArrayForDuplicates(array, arrayNameStr) {
// return false if no dups, return array of error strings if dups
// parameters:
// array | required | Array with potential duplicates
// arrayNameStr | optional | String describing the array
arrayNameStr = (typeof arrayNameStr === "undefined") ? 'unnamed array' : arrayNameStr;
var duplicates = [], errors = [];
array.forEach(function (element) {
if (duplicates.indexOf(element) !== -1)
errors.push(arrayNameStr + ' duplicate found: ' + element);
duplicates.push(element);
});
if (errors.length === 0)
return false;
else
return errors;
}
function getProp(object, prop, norm) {
// return the value of object.prop, or the value of norm if undefined.
// norm is optional, and defaults to null
// parameters:
// object | required | Object
// prop | required | String
// norm | optional | any type, defaults to null
norm = (typeof norm === "undefined") ? null : norm;
if (object.hasOwnProperty(prop))
return object[prop];
else
return norm;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment