Skip to content

Instantly share code, notes, and snippets.

@xuanfeng
Created August 14, 2017 12:26
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 xuanfeng/abcb73d7321f2ae260286ae62ec156d5 to your computer and use it in GitHub Desktop.
Save xuanfeng/abcb73d7321f2ae260286ae62ec156d5 to your computer and use it in GitHub Desktop.
util
_.type = function (obj) {
return Object.prototype.toString.call(obj).replace(/\[object\s|\]/g, '')
}
_.isArray = function isArray (list) {
return _.type(list) === 'Array'
}
_.slice = function slice (arrayLike, index) {
return Array.prototype.slice.call(arrayLike, index)
}
_.truthy = function truthy (value) {
return !!value
}
_.isString = function isString (list) {
return _.type(list) === 'String'
}
_.each = function each (array, fn) {
for (var i = 0, len = array.length; i < len; i++) {
fn(array[i], i)
}
}
_.toArray = function toArray (listLike) {
if (!listLike) {
return []
}
var list = []
for (var i = 0, len = listLike.length; i < len; i++) {
list.push(listLike[i])
}
return list
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment