Created
October 30, 2012 05:09
-
-
Save zhuzhuaicoding/3978421 to your computer and use it in GitHub Desktop.
Util function
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(util) { | |
| var toString = Object.prototype.toString; | |
| var AP = Array.prototype; | |
| util.isString = function(val) { | |
| return toString.call(val) === '[object String]'; | |
| }; | |
| util.isObject = function(val) { | |
| return val === Object(val); | |
| }; | |
| util.isFunction = function(val) { | |
| return toString.call(val) === '[object Function]'; | |
| }; | |
| util.isArray = Array.isArray || function(val) { | |
| return toString.call(val) === '[object Array]'; | |
| }; | |
| util.indexOf = AP.indexOf ? | |
| function(arr, item) { | |
| return arr.indexOf(item); | |
| } : | |
| function(arr, item) { | |
| for (var i = 0, len = arr.length; i < len; i++) { | |
| if (arr[i] === item) { | |
| return i; | |
| } | |
| } | |
| return -1; | |
| }; | |
| var forEach = util.forEach = AP.forEach ? | |
| function(arr, fn) { | |
| arr.forEach(fn); | |
| } : | |
| function(arr, fn) { | |
| for (var i = 0, len = arr.length; i < len; i++) { | |
| fn(arr[i], i, arr); | |
| } | |
| }; | |
| util.map = AP.map ? | |
| function(arr, fn) { | |
| return arr.map(fn); | |
| } : | |
| function(arr, fn) { | |
| var ret = []; | |
| forEach(arr, function(item, i, arr) { | |
| ret.push(fn(item, i, arr)); | |
| }); | |
| return ret; | |
| }; | |
| util.filter = AP.filter ? | |
| function(arr, fn) { | |
| return arr.filter(fn); | |
| } : | |
| function(arr, fn) { | |
| var ret = []; | |
| forEach(arr, function(item, i, arr) { | |
| if (fn(item, i, arr)) { | |
| ret.push(item); | |
| } | |
| }); | |
| return ret; | |
| }; | |
| util.unique = function(arr) { | |
| var ret = []; | |
| var o = {}; | |
| forEach(arr, function(item) { | |
| o[item] = 1; | |
| }); | |
| if (Object.keys) { | |
| ret = Object.keys(o); | |
| } | |
| else { | |
| for (var p in o) { | |
| if (o.hasOwnProperty(p)) { | |
| ret.push(p); | |
| } | |
| } | |
| } | |
| return ret; | |
| }; | |
| util.now = Date.now || function() { | |
| return new Date().getTime(); | |
| }; | |
| })(birdNS._util); | |
| //from SeaJS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment