Skip to content

Instantly share code, notes, and snippets.

@Javison666
Last active August 12, 2020 17:01
Show Gist options
  • Save Javison666/87c7502a37c96d6475ab3f3a2c499976 to your computer and use it in GitHub Desktop.
Save Javison666/87c7502a37c96d6475ab3f3a2c499976 to your computer and use it in GitHub Desktop.
js-core
export class LockMap extends Map {
constructor (args) {
const map = new Map(args)
map.set = () => {
console.error('禁止修改枚举常量')
}
map.delete = () => {
console.error('禁止修改枚举常量')
}
map.clear = () => {
console.error('禁止修改枚举常量')
}
return map
}
}
/**
* 获取当前时间
* @return string
* @param ("Y-M-D h:m:s") 或 ("Y-M-D h:m:s",时间戳)
* YMDhms直接替换对应时间单位,格式可自由替换
* 没有参数直接返回Y-M-D h:m:s(如2018-01-01 12:21:45)
* 有参数则直接替换
*/
function showDate(format, time) {
let now;
const add0 = (m) => (m < 10 ? "0" + m : m + "");
const applyNew = (ctor, args) => {
let applyArgs = [{}].concat(args || []);
let f = Function.prototype.bind.apply(ctor, applyArgs);
return new f();
};
if (
arguments.length == 2 &&
time &&
String(new Date(time)) === "Invalid Date"
) {
let arr = time.match(/\d+/g);
now = applyNew(Date, arr);
} else if (arguments.length != 2) {
now = new Date();
} else {
now = new Date(time);
}
let year = now.getFullYear();
let month = add0(now.getMonth() + 1);
let date = add0(now.getDate());
let hour = add0(now.getHours());
let minute = add0(now.getMinutes());
let seconds = add0(now.getSeconds());
if (!arguments.length) {
return (
year +
"-" +
month +
"-" +
date +
" " +
hour +
":" +
minute +
":" +
seconds
);
} else if (arguments.length > 0) {
if (format.indexOf("Y") > -1) {
format = format.replace(/Y/g, year);
}
if (format.indexOf("M") > -1) {
format = format.replace(/M/g, month);
}
if (format.indexOf("D") > -1) {
format = format.replace(/D/g, date);
}
if (format.indexOf("h") > -1) {
format = format.replace(/h/g, hour);
}
if (format.indexOf("m") > -1) {
format = format.replace(/m/g, minute);
}
if (format.indexOf("s") > -1) {
format = format.replace(/s/g, seconds);
}
return format;
}
}
console.log(showDate("Y-M-D h:m:s"))
/**
* 将编码由utf8转为utf16
* @param {需要编码的字符串} str
*/
function utf8to16(str) {
let out, i, len, c;
let char2, char3;
out = "";
len = str.length;
i = 0;
while (i < len) {
c = str.charCodeAt(i++);
switch (c >> 4) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
// 0xxxxxxx
out += str.charAt(i - 1);
break;
case 12:
case 13:
// 110x xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
char3 = str.charCodeAt(i++);
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
}
}
return out;
}
/**
* @param {string} num1
* @param {string} num2
* @return {string}
*/
var multiply = function(num1, num2) {
if(num1==0 || num2==0) return "0"
const res=[];// 结果集
for(let i=0;i<num1.length;i++){
let tmp1=num1[num1.length-1-i]; // num1尾元素
for(let j=0;j<num2.length;j++){
let tmp2 = num2[num2.length-1-j]; // num2尾元素
let pos = res[i+j] ? res[i+j]+tmp1*tmp2 : tmp1*tmp2;// 目标值 ==》三元表达式,判断结果集索引位置是否有值
res[i+j]=pos%10; // 赋值给当前索引位置
// 目标值是否大于10 ==》是否进位 这样简化res去除不必要的"0"
pos >=10 && (res[i+j+1]=res[i+j+1] ? res[i+j+1]+Math.floor(pos/10) : Math.floor(pos/10));
}
}
return res.reverse().join("");
};
/**
* 数值转换为两位数的金额
* @param {*} rmb 为整数
*/
function showMoney(a) {
a = String(a);
const l = a.length;
if (l > 2) {
return a.replace(/(\d{2})$/g, "\.$1")
} else if (l == 2) {
return "0." + a;
} else if (l == 1) {
return "0.0" + a;
}
}
/**
* 单位为元的金额的千位符显示
* @param {*} rmb 为整数
*/
function showThousandMoney(rmb) {
return parseFloat(rmb).toFixed(2).replace(/(\d{1,3})(?=(\d{3})+(?:\.))/g, "$1,")
}
/**
* 整数的千位符显示
* @param {*} num 为整数
*/
export function showThousandNum (num) {
return String(num).replace(/\d{1,3}(?=(\d{3})+$)/g, '$&,')
}
// 给url添加参数
function urlAddParams(url, params) {
if (typeof (url) == 'undefined' || url == null || url == '') {
return '';
}
if (typeof (params) == 'undefined' || params == null || typeof (params) != 'object') {
return url;
}
url += (url.indexOf("?") != -1) ? "" : "?";
for (const k in params) {
url += ((url.indexOf("=") != -1) ? "&" : "") + k + "=" + encodeURI(params[k]);
}
return url;
}
// 节流
function throttle(func, wait){
var timer
return function(){
if(!timer){
func.apply(this,arguments)
timer = setTimeout(()=>{
timer = null
},wait)
}
}
}
// 防抖
function debounce(func,wait){
var timer
return function(){
if(timer) clearTimeout(timer)
var isRun = !timer
setTimeout(()=>{
timer = null
},wait)
if(isRun){
func.apply(this,arguments)
}
}
}
/**  
* 获取距离今天的N天的日期  N可正可负 
* @param {Number} interval default 0  -n 表示前几天  n表示后几天 
*/
function getIntervalDate(interval = 0)s{
interval = Number(interval)
let currentDate = new Date();
currentDate.setDate(currentDate.getDate() + interval);
let year = currentDate.getFullYear();
let month = (currentDate.getMonth() + 1) < 10 ? "0" + (currentDate.getMonth() + 1) : (currentDate.getMonth() + 1);
let day = currentDate.getDate() < 10 ? "0" + currentDate.getDate() : currentDate.getDate();
return year + "-" + month + "-" + day;
}
/**
* 获取距离当前的时差,天/时/分/秒
* @param {*} time
*/
function diffToNow(time) {
let now = new Date().getTime();
if (String(new Date(time)) == 'Invalid Date') {
let arr = time.match(/\d+/g);
time = applyNew(Date, arr);
}
time = new Date(time).getTime()
let diffTime = Math.abs(time - now);
let diffDays = parseInt(String(diffTime / 1000 / 3600 / 24))
// 大于1天
let diffYears = parseInt(String(diffDays / 365))
if (diffYears > 1) {
return `${String(diffYears)}年`
}
if (diffDays >= 1) {
return `${String(diffDays)}天`
}
let diffHours = parseInt(String(diffTime / 1000 / 3600))
// 大于1小时
if (diffHours >= 1) {
return `${String(diffHours)}小时`
}
let mins = parseInt(String(diffTime / 1000 / 60));
let sec = parseInt(String(diffTime / 1000)) % 60;
if (mins >= 1) {
mins = mins % 60;
return `${String(mins)}分`;
} else {
return `${String(sec)}秒`;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment