51.格式化数字:
/**
* 格式化数字
* @param {number} num
*/
static formatNumber$(num) {
if (isNaN(num)) return "0";
if (num > 1000000) {
num /= 1000000;
if (num >= 100) {
return Math.round(num) + "m"
} else if (num >= 10) {
num = num.toFixed(1);
return num + "m"
} else {
num = num.toFixed(2);
return num + "m"
}
} else if (num > 1000) {
num /= 1000;
if (num >= 100) {
return Math.round(num) + "k"
} else if (num >= 10) {
num = num.toFixed(1);
return num + "k"
} else {
num = num.toFixed(2);
return num + "k"
}
} else {
return Math.round(num);
}
}
52.钱的单位转化:
// 钱单位
static cMoney$(value) {
let absVal = Math.abs(Math.floor(value));
let sVal = absVal.toLocaleString().replace(/[,]/g, '');
let cnt = Math.floor((sVal.length - 1) / 3);
let unit = Utils$.moneyUnit[cnt];
let subStr = sVal.substring(0, sVal.length - (cnt > 0 ? cnt * 3 - 1 : 0));
let val = (value < 0 ? -1 : 1) * Number(subStr);
return cnt > 0 ? val / 10 + unit : val;
}
Utils$.moneyUnit = ["", "K", "M", "B", "T", "aa", "bb", "cc", "dd", "ee",
"ff", "gg", "hh", "ii", "jj", "kk", "ll", "mm", "nn", "oo",
"pp", "qq", "rr", "ss", "tt", "uu", "vv", "ww", "xx", "yy",
"zz", "AA", "BB", "CC", "DD", "EE", "FF", "GG", "HH", "II",
"JJ", "KK", "LL", "MM", "NN", "OO", "PP", "QQ", "RR", "SS",
"TT", "UU", "VV", "WW", "XX", "YY", "ZZ",]
53.日期转化:
Date.prototype.format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o){
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
}
}
return fmt;
};
54.执行回调:
/**
* 回调函数(允许带参)
* @param {Function} cb 待回调的函数
* @param args 参数
* @returns {any} 回调函数执行后的返回值
*/
function doCallback$1 ( cb ) {
// body...
if (typeof cb === "function") {
let args = Array.prototype.slice.call(arguments);
args.shift();
// cb
return cb.apply(null, args)
}
return null
}
55.生成指定位数的随机字符串:
/**
* 生成从min到max之间的随机数,其中min和max都必须大于0的整数
* 若为一个参数,则生成1到min之间的随机数
* 若为两个参数,则生成min到max之间的随机数
* 其他参数个数,返回错误0
* @param {number} min 最小随机数
* @param {number} max 最大随机数
* @returns {number}
*/
function random (min, max) {
// body...
if (min < 0 || max <= 0) {
return 0;
}
switch(arguments.length) {
case 1:
return Math.floor(Math.random() * min + 1);
case 2:
return Math.floor(Math.random() * (max - min + 1) + min);
default:
return 0;
}
}
/**
* 生成指定位数的随机字符串
* @param {number} count 需要生成的字符数量
* @returns {string}
*/
function generateString ( count ) {
// body...
let str = '';
if (typeof count === "number") {
for (let i = 0; i < count; i++) {
if (Math.random() < 0.5) {
str += String.fromCharCode(random('0'.charCodeAt(), '0'.charCodeAt() + 9));
}
else {
str += String.fromCharCode(random('a'.charCodeAt(), 'a'.charCodeAt() + 25));
}
}
}
return str
}
56.超过长度的字符串截取指定长度,超出部分...显示:
/**
* 超过长度的字符截取指定长度,超出部分以...显示
* @param {string} str 需要裁剪的字符
* @param {number} len 最大字符长度
* @returns {string}
*/
function cutString (str, len) {
var newLength = 0;
var newStr = "";
var chineseRegex = /[^\x00-\xff]/g;
var singleChar = "";
var strLength = str.replace(chineseRegex, "**").length;
if (strLength > len) {
for (var i = 0; i < strLength; i++) {
singleChar = str.charAt(i).toString();
if (singleChar.match(chineseRegex) != null) {
newLength += 2;
}
else {
newLength++;
}
if (newLength > len) {
break;
}
newStr += singleChar;
}
if (strLength > len) {
newStr += "...";
}
} else {
newStr = str;
}
return newStr;
}
57.打乱数组:
/**
* 打乱数组(影响原数组对象)
* @param {Array} arr
*/
function shuffleArray (arr) {
if (Array.isArray(arr)) {
for (let i = 0; i < arr.length; i++) {
let randomIndex = Math.round(Math.random() * (arr.length - 1 - i)) + i;
let temp = arr[randomIndex];
arr[randomIndex] = arr[i];
arr[i] = temp;
}
}
}
58.数字按照粒度拆分,个十百千万:
static checkNum(num: string, size: number) {
let count = parseInt(num).toString();
let len = count.length;
if (len <= size) return num;
let r = len % size;
let regExp = new RegExp('\\d{' + size + '}', 'g')
return r > 0 ? count.slice(0, r) + ',' + count.slice(r, len).match(regExp).join(',') : count.match(regExp).join(',')
}
59.定时刷新网页:
var timeout = prompt("设置刷新时间间隔[S]");
var current = location.href;
if(timeout > 0)
{
setTimeout('reload()', 1000 * timeout);
}
else
{
location.replace(current);
}
function reload()
{
setTimeout('reload()', 1000 * timeout);
var fr4me = '';
with(document)
{
write(fr4me);
void(close());
};
}
60读取地址栏中的参数:
/*
当前网址获取url 中的参数
paramName:参数名
*/
static getUrlParam(paramName: string): string {
let url = location.href; //取得整个地址栏
let indexQuestion = url.indexOf("?");
let paramInUrl = '';
paramInUrl = url.substr(indexQuestion + 1); //取得所有参数
let arrParam = paramInUrl.split("&"); //各个参数放到数组里
for (let i = 0; i < arrParam.length; i++) {
let indexEqual = arrParam[i].indexOf("=");
if (indexEqual > 0) {
let name = arrParam[i].substring(0, indexEqual);
let value = arrParam[i].substr(indexEqual + 1);
if(name==paramName) return value;
}
}
console.log('没有在url中找到 参数' + paramName);
return ;
}