Last active
August 29, 2015 14:16
-
-
Save atheiman/7a6a6667cf44794e7301 to your computer and use it in GitHub Desktop.
js-utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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