Skip to content

Instantly share code, notes, and snippets.

@kanakiyajay
Created August 21, 2014 11:34
Show Gist options
  • Save kanakiyajay/739f71affd05ab086c58 to your computer and use it in GitHub Desktop.
Save kanakiyajay/739f71affd05ab086c58 to your computer and use it in GitHub Desktop.
Angular: Function to calculate the length of string
var isArray = (function() {
if (!isFunction(Array.isArray)) {
return function(value) {
return toString.call(value) === '[object Array]';
};
}
return Array.isArray;
})();
function isObject(value){return value != null && typeof value === 'object';}
function isString(value){return typeof value === 'string';}
/*
@param {Object|Array|string} obj Object, array, or string to inspect.
@param {boolean} [ownPropsOnly=false] Count only "own" properties in an object
@returns {number} The size of `obj` or `0` if `obj` is neither an object nor an array.
*/
function size(obj, ownPropsOnly) {
var count = 0, key;
if (isArray(obj) || isString(obj)) {
return obj.length;
} else if (isObject(obj)) {
for (key in obj)
if (!ownPropsOnly || obj.hasOwnProperty(key))
count++;
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment