Created
August 21, 2014 11:34
-
-
Save kanakiyajay/739f71affd05ab086c58 to your computer and use it in GitHub Desktop.
Angular: Function to calculate the length of string
This file contains 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
var isArray = (function() { | |
if (!isFunction(Array.isArray)) { | |
return function(value) { | |
return toString.call(value) === '[object Array]'; | |
}; | |
} | |
return Array.isArray; | |
})(); |
This file contains 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 isObject(value){return value != null && typeof value === 'object';} |
This file contains 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 isString(value){return typeof value === 'string';} |
This file contains 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
/* | |
@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