// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart if (!String.prototype.padStart) { String.prototype.padStart = function padStart(targetLength,padString) { targetLength = targetLength>>0; //floor if number or convert non-number to 0; padString = String((typeof padString !== 'undefined' ? padString : ' ')); if (this.length > targetLength) { return String(this); } else { targetLength = targetLength-this.length; if (targetLength > padString.length) { padString += padString.repeat(targetLength/padString.length); //append to original to ensure we are longer than needed } return padString.slice(0,targetLength) + String(this); } }; } export function copy(obj) { return JSON.parse(JSON.stringify(obj)); } export function named(value) { return function (target) { target.definedName = value; } } export function formatCurrency(name) { if (name === 'USD' ) { return '$'; } else if (name === 'RMB') { return '¥'; } else if (name === 'EUR') { return '€' } else if (name === 'GBP') { return '£' } else { return name + ' '; } } export function formatPrice(price) { return Math.ceil(price).toLocaleString(); } export function formatPercent(number) { return Math.round(number * 100) + '%' } export function formatDate(date) { if (isEmpty(date)) { return 'NaN'; } const year = date.getFullYear(); const month = date.getMonth() + 1; const day = date.getDate(); const monthStr = ('' + month).padStart(2, 0); const dayStr = ('' + day).padStart(2, 0); const formatted = year + '-' + monthStr + '-' + dayStr; return formatted; } export function formatTime(date) { const hours = date.getHours(); const minutes = date.getMinutes(); const hoursStr = ('' + hours).padStart(2, 0); const minutesStr = ('' + minutes).padStart(2, 0); const formatted = hoursStr + ':' + minutesStr; return formatted; } export function formatDatetime(date) { const year = date.getFullYear(); const month = date.getMonth() + 1; const day = date.getDate(); const monthStr = ('' + month).padStart(2, 0); const dayStr = ('' + day).padStart(2, 0); const hours = date.getHours(); const minutes = date.getMinutes(); const hoursStr = ('' + hours).padStart(2, 0); const minutesStr = ('' + minutes).padStart(2, 0); const formatted = year + '-' + monthStr + '-' + dayStr + ' ' + hoursStr + ':' + minutesStr; return formatted; } export function mixins(...list) { return function (target) { list.forEach(val => { const mixinObj = Object.create(val.prototype, {}) const name = Object.getPrototypeOf(mixinObj).constructor.name; const camelCase = name.substr(0, 1).toLowerCase() + name.substr(1); Object.assign(target.prototype, {[camelCase]: mixinObj}) }) } } export function camelCase(name) { return name.substr(0, 1).toLowerCase() + name.substr(1); } export class UrlBuilder { constructor(url) { this.url = url; this.paramList = []; } append(name, value) { if (isNotEmpty(value)) { this.paramList.push({'name': name, 'value': value}); } return this; } build() { this.paramList.forEach((e, i, a) => { if (i === 0) { this.url += '?'; } else { this.url += '&'; } this.url += e.name + '=' + e.value; }); return this.url; } } export function isNotEmpty(val) { return val !== undefined && val !== null && val !== ''; } export function isEmpty(val) { return val === undefined || val === null || val === ''; } export function empty(a) { if (a === "") return true; //检验空字符串 if (a === "null") return true; //检验字符串类型的null if (a === "undefined") return true; //检验字符串类型的 undefined if (!a && a !== 0 && a !== "") return true; //检验 undefined 和 null if (Array.prototype.isPrototypeOf(a) && a.length === 0) return true; //检验空数组 if (Object.prototype.isPrototypeOf(a) && Object.keys(a).length === 0) return true; //检验空对象 return false; } export function prepareUrl(url) { return new UrlBuilder(url); } export function debounce(fn, delay = 500) { let timer; return (e) => { e.persist(); clearTimeout(timer); timer = setTimeout(() => { fn(e); }, delay); }; }; export function throttle(fn, delay, atleast) { let timeout = null, startTime = new Date(); return function() { let curTime = new Date(); clearTimeout(timeout); if (curTime - startTime >= atleast) { fn(); startTime = curTime; } else { timeout = setTimeout(fn, delay); } } } export function clickUrl(url) { const httpLink = document.createElement('a'); httpLink.href = url; httpLink.target = '_blank'; httpLink.click(); }