landers.base
Version:
landers.base
409 lines • 15.5 kB
JavaScript
; (function (window, $, Landers) {
Landers = Landers || {};
Landers.utils = {
is_string: function(x) {
return typeof(x) == 'string';
},
isString: function(x) {
return this.is_string(x);
},
is_defined: function(x) {
return typeof(x) != 'undefined';
},
isDefined: function(x) {
return this.is_defined(x);
},
is_null: function(x) {
return (x === null || !this.is_defined(x));
},
isNull: function(x){
return this.is_null(x);
},
is_fun: function(x) {
return typeof(x) == 'function';
},
isFunction: function(x){
return this.is_fun(x);
},
is_bool: function(x) {
return typeof(x) == 'boolean';
},
isBool: function(x){
return this.is_bool(x);
},
is_json: function(x) {
return typeof(x) == "object" &&
Object.prototype.toString.call(x).toLowerCase() == "[object object]" &&
!x.length;
},
isJson: function(x){
return this.is_json(x);
},
is_jquery: function(x) {
return Landers.utils.type(x) == 'jQuery';
},
isJQuery: function(x){
return this.is_jquery(x);
},
is_numeric: function(x) {
return $.isNumeric(x);
},
isNumeric: function(x){
return this.is_numeric(x);
},
is_array: function(x) {
try {
return x.constructor == Array;
} catch (e) {
return false;
}
},
isArray: function(x){
return this.is_array(x);
},
is_object: function(o) {
return (o !== null && typeof(o) == 'object');
},
isObject: function(o){
return this.is_object(o);
},
is_rgb: function(s) {
return (s.length === 4 || s.length === 7) && s.indexOf('#') === 0;
},
isRGB: function(s){
return this.is_rgb(s);
},
is_url: function(s) {
return s.indexOf('/') != -1 || s.indexOf('\\') != -1;
},
isUrl: function(s){
return this.is_url(s);
},
is_ie: function() {
return !!window.ActiveXObject;
},
isIE: function(){
return this.is_ie();
},
is_ie6: function() {
return !-[1, ] && !window.XMLHttpRequest;
},
isIE6: function(){
return this.is_ie6();
},
is_ie7: function() {
return this.ie() && navigator.appVersion.split(";")[1].replace(/[ ]/g, "") == "MSIE7.0";
},
isIE7: function(){
return this.is_ie7();
},
is_mobile: function() {
return navigator.userAgent.match(/(iPhone|iPod|Android|ios|iOS|iPad|Backerry|WebOS|Symbian|Windows Phone|Phone)/i);
},
isMobile: function(){
return this.is_mobile();
},
is_android: function() {
return navigator.userAgent.match(/Android/i);
},
isAndroid: function(){
return this.is_android();
},
is_ios: function() {
return navigator.userAgent.match(/(iPhone|iPod|iPad|iOS)/i);
},
isIOS: function(){
return this.is_ios();
},
to: {
bool:function (x){
return x === 'true' || x === 'TRUE' || x === 1 || x === true;
},
array:function(){
var args = arguments, dat = args[0], a;
switch(Landers.utils.type(dat)) {
case 'string' :
var split = args[1] || ',';
a = dat.split(split);
for(var i=0; i<a.length; i++) a[i] = $.trim(a[i]);
return a;
case 'json' :
a = []; for (var s in dat) a.push([s,dat[s]]);
return a;
case 'array' :
return dat;
}
},
json:{
encode:function(j){return JSON.stringify(j);},
decode:function(s){return $.parseJSON(s);}
},
utf8:{
encode:function(str){
var out = '', i, len = str.length, c;
for(i = 0; i < len; i++) {
c = str.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) {
out += str.charAt(i);
} else if (c > 0x07FF) {
out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
} else {
out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
}
}
return out;
}
},
hex:{
encode:function(){
var monyer = new Array(), i, s;
is = is === false ? false : true;
for (i = 0; i < str.length; i++) {
s = str.charCodeAt(i).toString(16);
monyer += is ? "\\x" + s : (new Array(5 - String(s).length).join("0") + s);
}
return monyer;
},
decode:function(){
var monyer = new Array();
var s = str.split("\\");
for (i = 1; i < s.length; i++) {
s[i] = s[i].replace('x', '');
monyer += String.fromCharCode(parseInt(s[i], 16));
}
return monyer;
}
},
constructor:function(obj) {
var con = obj.constructor;
if (!con) return undefined;
if (con.name) return con.name;
var str = con.toString();
var arr = str.charAt(0) == '[' ?
str.match(/\[\w+\s*(\w+)\]/) :
str.match(/function\s*(\w+)/);
if (arr && arr.length == 2) return arr[1];
return undefined;
}
},
time:{
toHuman : function(oDate){
var year = oDate.getFullYear();
var month = oDate.getMonth() + 1;
month = month < 10 ? "0" + month : month;
var day = oDate.getDate();
day = day < 10 ? "0" + day : day;
var hour = oDate.getHours();
hour = hour < 10 ? "0" + hour : hour;
var minute = oDate.getMinutes();
minute = minute < 10 ? "0" + minute : minute;
var second = oDate.getSeconds();
second = second < 10 ? "0" + second : second;
return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
},
//功能:把日期转为unix时间戳
data2unix : function(year, month, day, hour, minute, second) {
var UTC = Date.UTC(year, (month - 1), day, hour, minute, second);
var oDate = new Date(UTC);
return oDate.getTime() / 1000 - 8 * 60 * 60;
},
//功能:把unix时间戳转成Y-m-d H:i:s格式的日期
unix2human:function(unix) {
var now = new Date(unix * 1000);
return this.toHuman(now);
},
parse : function(oDate) {
return {
y : oDate.getFullYear(),
m : oDate.getMonth() + 1,
d : oDate.getDate(),
h : oDate.getHours(),
i : oDate.getMinutes(),
s : oDate.getSeconds()
};
},
nowUnix:function(){ //当前时间戳
var d = this.parse(new Date());
return this.data2unix(d.y, d.m, d.d, d.h, d.i, d.s);
}
},
str:{
sprintf : function(){
var ret = arguments[0];
for (var i=1; i<arguments.length; i++) {
ret = ret.replace('%s', arguments[i]);
}
return ret;
},
snakeCase : function(str) {
return str.replace(/[A-Z]/g, function($1) {
return "_" + ($1.toLowerCase());
});
},
camelCase : function(str) {
str = str.toLowerCase();
var reg = /\b(\w)|\s(\w)/g; // \b判断边界\s判断空格
return str.replace(reg, function(m){
return m.toUpperCase();
});
},
len:function(str){return str.replace(/[\u0391-\uFFE5]/g,' ').length;},
right:function(str, n){return str.slice(str.slice(-n).replace(/[\x00-\xff]/g,"").length-n);},
left:function(str, n){return str.slice(0,n-str.slice(0,n).replace(/[\x00-\xff]/g,"").length);},
replace:function(str, s1, s2) {
if (str === null || str === undefined) return '';
try {
str = str.toString();
} catch(e) {
console.log(str);
}
s1 = s1.replace(/\{/,'\\{').replace(/\}/,'\\}');
var re = new RegExp(s1,"gi");
return str.replace(re,s2);
},
random:function(n){
var s = '';
for ( var i=0;i<6;i++ ) {
s += Math.floor( Math.random()*10 ).toString();
}
return s;
},
stripHTML : function(str) {
var reTag = /<(?:.|\s)*?>/g;
return str.replace(reTag,"");
}
},
json: {
create:function(j, str, hasself) {
j = j || {};
var a = str.split('.'), jtmp = j;
if (hasself) a = a.splice(1);
for (var i=0; i<a.length; i++) {
if (!jtmp[a[i]]) jtmp[a[i]] = {};
jtmp = jtmp[a[i]];
}
},
count:function(j){var c = 0; for (var p in j) c++; return c;},
slice:function(j, keys) {
j = j || {};
var ret = {};
if ( !Landers.utils.is_array(keys)) keys = keys.split(',');
for (var i = 0; i < keys.length; i++ ) {
var key = $.trim(keys[i]);
ret[key] = j[key];
}
return ret;
},
count:function(j){var c=0; for (var k in j) c++; return c;},
keys:function(j){var a=[]; for (var k in j) a.push(k); return a;},
values:function(j){var a=[]; for (var k in j) a.push(j[k]); return a;},
filter: function(j, value){
var ret = {};
for (var k in j) {
if (value === j[k]) ret[k] = j[k];
};
return ret;
}
},
arr: {
remove_val:function(arr, v) {
for(var i=0;i<arr.length;i++){
if(v == arr[i]) arr.splice(i,1);
}
return arr;
},
search:function(x, arr, from){
return $.inArray(x, arr, from || 0);
},
is_in:function(x, arr, from){
return this.search(x, arr, from || 0) >= 0;
},
isIn: function(x, arr, from){
return this.is_in(x, arr, from);
},
random:function(arr){
return arr[Landers.utils.math.random(0, arr.length-1)];
},
sum:function(arr){
var r = 0;
for (var i=0; i<arr.length; i++){
var t = parseFloat(arr[i]);
if (t) r += t;
}; return r;
},
shuffle:function(arr) {
var a = arr.slice();
var s = new Array();
while (a.length) s.push(a.splice(Math.random() * a.length, 1)[0]);
while (s.length) a.push(s.pop());
return a;
},
rand:function(arr, n){
var a = arr.slice();
var aret = new Array();
for (var i=1; i<=n; i++){
var idx = Landers.utils.math.random(0,a.length-1);
var val = a[idx]; aret.push(val);
a.remove(idx,true);
}
return aret.length > 1 ? aret : aret[0];
},
cross:function(a1,a2){
var a = [];
for (var i=0;i<a1.length;i++){
var idx = a2.search(a1[i]);
if (idx!==null) a.push(a2[idx]);
}; return a;
},
unique:function(arr){
var aflag = new Array();
for (var i=0;i<=arr.length-1;i++) aflag[i] = false;
for (var i=0;i<=arr.length-1;i++){
for (var j=i+1;j<=arr.length-1;j++){
if (this[i]==arr[j]) aflag[j] = true;
}
}
for (var i=aflag.length-1;i>=0;i--){
if (aflag[i]) arr.splice(i,1);
}; return arr;
}
},
type: function(x){
try{if (x instanceof jQuery) return 'jQuery';}catch(e){}
var ret = $.type(x); if (ret != 'object') return ret;
if ( $.isPlainObject(x) ) return 'json';
var type = this.to.constructor(x);
var arr = type.match(/^HTML(.*)Element$/);
if (arr) return arr[1].toLowerCase();
if (type != 'Object') return type;
return ret;
},
math: {
add:function(n1, n2){
var r1, r2, m;
try{r1 = n1.toString().split(".")[1].length;} catch(e) {r1 = 0;}
try{r2 = n2.toString().split(".")[1].length;} catch(e) {r2 = 0;}
m = Math.pow(10 , Math.max(r1, r2));
return (n1 * m + n2 * m ) / m ;
},
round:function(num, n){
return Math.round(num*Math.pow(10,n || 0))/Math.pow(10,n || 0);
},
random:function(n1,n2){
var max = Math.max(n1,n2), min = Math.min(n1,n2);
return parseInt(Math.random()*(max-min+1)) + min;
}
},
dom:{
add_param:function(o,name,val){
var param = document.createElement('param');
param.setAttribute('name', name);
param.setAttribute('value', val);
o.appendChild(param);
}
}
};
})(this, jQuery, Landers);