1. 時間格式化
function formatTime(time, cFormat) {
if (arguments.length === 0) return null
if ((time + '').length === 10) {
time = +time * 1000
}
var format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}', date
if (typeof time === 'object') {
date = time
} else {
date = new Date(time)
}
var formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
}
var time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
var value = formatObj[key]
if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
if (result.length > 0 && value < 10) {
value = '0' + value
}
return value || 0
})
return time_str
}
複製代碼
2. 獲取某月有多少天
function getMonthOfDay (time) {
var date = new Date(time)
var year = date.getFullYear()
var mouth = date.getMonth() + 1
var days
//當月份爲二月時,根據閏年仍是非閏年判斷天數
if (mouth == 2) {
days = year % 4 == 0 ? 29 : 28
} else if (mouth == 1 || mouth == 3 || mouth == 5 || mouth == 7 || mouth == 8 || mouth == 10 || mouth == 12) {
//月份爲:1,3,5,7,8,10,12 時,爲大月.則天數爲31;
days = 31
} else {
//其餘月份,天數爲:30.
days = 30
}
return days
}
複製代碼
3. 數組排序
/**
* @param {arr} 數組
* @param {type} 1:從小到大 2:從大到小 3:隨機
* @return {Array}
*/
function sort (arr, type = 1) {
return arr.sort( (a, b) => {
switch(type) {
case 1:
return a - b;
case 2:
return b - a;
case 3:
return Math.random() - 0.5;
default:
return arr;
}
})
}
複製代碼
4. 數組去重
function unique (arr) {
if ( Array.hasOwnProperty('from') ) {
return Array.from(new Set(arr));
}else{
var n = {},r=[];
for(var i = 0; i < arr.length; i++){
if (!n[arr[i]]){
n[arr[i]] = true;
r.push(arr[i]);
}
}
return r;
}
}
複製代碼
5. 兩個集合並集
function union (a, b) {
var newArr = a.concat(b);
return unique(newArr);
}
複製代碼
6. 兩個集合交集
function intersect (a, b) {
var _this = this;
a = unique(a);
return a.map(item => {
return b.includes(item) ? b : null
})
}
複製代碼
7. 貨幣格式化
function currency (value, currency, decimals) => {
value = parseFloat(value)
if (!isFinite(value) || (!value && value !== 0)) return ''
currency = currency != null ? currency : '$'
decimals = decimals != null ? decimals : 2
var stringified = Math.abs(value).toFixed(decimals)
var _int = decimals
? stringified.slice(0, -1 - decimals)
: stringified
var i = _int.length % 3
var head = i > 0
? (_int.slice(0, i) + (_int.length > 3 ? ',' : ''))
: ''
var _float = decimals
? stringified.slice(-1 - decimals)
: ''
var sign = value < 0 ? '-' : ''
return sign + currency + head +
_int.slice(i).replace(digitsRE, '$1,') +
_float
},
複製代碼
8.長文本截斷方法
function textEllipsis (value, maxLength = 12) {
if (!isString(value)) { return value }
if (value.length > maxLength) {
return `${value.slice(0, maxLength - 3)}...${value.slice(value.length - 3, value.length)}`
} else {
return value
}
}
複製代碼
判斷參數是不是其中之一
function oneOf (value, validList) {
for (let i = 0; i < validList.length; i++) {
if (value === validList[i]) {
return true
}
}
return false
}
複製代碼
deepCopy
function deepCopy (data) {
const t = typeOf(data)
let o
if (t === 'array') {
o = []
} else if (t === 'object') {
o = {}
} else {
return data
}
if (t === 'array') {
for (let i = 0; i < data.length; i++) {
o.push(deepCopy(data[i]))
}
} else if (t === 'object') {
for (let i in data) {
o[i] = deepCopy(data[i])
}
}
return o
}
複製代碼
JS提交表單
const JSForm = ({ url = '', params = {}, type = 'POST'}) => {
let form = document.createElement('form')
document.body.appendChild(form)
for (let item in params) {
let input = document.createElement('input')
input.type = 'hidden'
input.name = item
input.value = params[item]
form.appendChild(input)
}
form.method = type
form.action = url
form.submit()
document.body.removeChild(form)
}
複製代碼
獲取query的值
const getQueryString = name => {
let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
let r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}
複製代碼