Is it null
function isRealNull(val) {
if(val == null || val.toString().replace(/\s/g."") = ="") {return true;
}
else{
return false; }}Copy the code
Is it a pure number
function isRealNum(val) {
if(val == null || val.toString().replace(/\s/g."") = = ="") {return false;
}
if(typeof val == "boolean") {return false;
}
if(!isNaN(val)){
return true;
}
else{
return false; }}Copy the code
Is it in Chinese?
function hasChinaword(s) {
let patrn = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/gi;
if(! patrn.exec(s)) {return false;
}
else {
return true; }}Copy the code
Gets the number of bytes of a single character
function checkWordByteLength(value) {
return Math.ceil(value.charCodeAt().toString(2).length / 8);
}
Copy the code
Calculates the character byte length
function getByteLen(val,subLen) {
if(subLen === 0) {return "";
}
if (val == null) {
return 0;
}
let len = 0;
for (let i = 0; i < val.length; i++) {
let a = val.charAt(i);
if (a.match(/[^\x00-\xff]/ig) != null) {
len += 2;
}
else {
len += 1;
}
if(isRealNum(subLen) && len === ~~subLen){
return val.substring(0,i)
}
}
return len;
};
Copy the code
Color RGB to hexadecimal
function rgbTohex(color) {
let rgb;
if (color.indexOf("rgba") > -1) {
rgb = color.replace("rgba("."").replace(")"."").split(', ');
}
else {
rgb = color.replace("rgb("."").replace(")"."").split(', ');
}
let r = parseInt(rgb[0]);
let g = parseInt(rgb[1]);
let b = parseInt(rgb[2]);
let hex = "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
return hex;
};
Copy the code
Color from hexadecimal to RGB
function hexToRgb(hex) {
let color = [], rgb = [];
hex = hex.replace(/ # /."");
if (hex.length == 3) { // Fix "# ABC "into "#aabbcc"
let tmp = [];
for (let i = 0; i < 3; i++) {
tmp.push(hex.charAt(i) + hex.charAt(i));
}
hex = tmp.join("");
}
for (let i = 0; i < 3; i++) {
color[i] = "0x" + hex.substr(i + 2.2);
rgb.push(parseInt(Number(color[i])));
}
return 'rgb(' + rgb.join(",") + ') ';
};
Copy the code
Convert columns from letters to numbers
function ABCatNum(a) {
if(a==null || a.length==0) {return NaN;
}
var str=a.toLowerCase().split("");
var num=0;
var al = str.length;
var getCharNumber = function(charx){
return charx.charCodeAt() -96;
};
var numout = 0;
var charnum = 0;
for(var i = 0; i < al; i++){
charnum = getCharNumber(str[i]);
numout += charnum * Math.pow(26, al-i-1);
};
// console.log(a, numout-1);
if(numout==0) {return NaN;
}
return numout-1;
};
Copy the code
Convert numbers to letters in column subscripts
function chatatABC(n) {
var orda = 'a'.charCodeAt(0);
var ordz = 'z'.charCodeAt(0);
var len = ordz - orda + 1;
var s = "";
while( n >= 0 ) {
s = String.fromCharCode(n % len + orda) + s;
n = Math.floor(n / len) - 1;
}
return s.toUpperCase();
};
Copy the code
Words in the form are converted to - forms such as fillColor -> fill-color
function camel2split(camel) {
return camel.replace(/([A-Z])/g.function(all, group) {
return The '-' + group.toLowerCase();
});
}
Copy the code