- 字符串是否是包含26个英文字母的短句
function isPangram(string){
string = string.toLowerCase();
return "abcdefghijklmnopqrstuvwxyz".split("").every(x => string.indexOf(x) !== -1);
}
- 英文首字母大写
String.prototype.camelCase = function () {
return this.trim().replace(/(?:^|\s+)(\w)/g, (_, c) => _.toUpperCase())
}
// 'my favorite animation!is jojo.'.camelCase()
// My Favorite Animation!Is Jojo.
- sleep 睡眠函数
export function sleep(delay: number) {
return new Promise(reslove => {
setTimeout(reslove, delay)
})
}
- dereplication 数组去重
export function unique(arg: any[]){
return [...new Set(arg)]
}
- 数字格式化 3000 -> 3,000
function formatNumber(str: string | number) {
return (''+str).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
// number [number].toLocaleString('en-US')
}
-
非大数取整
~~[number]
-
数组求和
arr.reduce((a, b) => a + b)
或者eval(arr.join("+"))
-
数字串转数组
[...arguments]
-
值对换
[a,b]
或者a^=b b^=a a^=b
-
数组乱序arr.sort(()=>Math.random()-.5)
// 信息脱敏
export function handle(param:string,left:number=0,right:number=0):string{
const len:number = param.length
const r = new RegExp("^(.{"+left+"})(?:\\d+)(.{"+right+"})$","gim");
let c:string = "";
for(let i=0;i<len-left-right;i++){
c+="*"
}
return param.replace(r, "$1"+c+"$2");
}
网友评论