1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
const handlerStyleKey = key => key.replaceAll(/[A-Z]/g, (val, index) => index === '0' ? val : '-' + val.toLowerCase())
const getStyleStrByObj = obj => Object.entries(obj).reduce((result, [ key, item ]) => result += `${handlerStyleKey(key)}: ${item};`, '')
const defaultStyle = { titleStyle: { padding: '3px', color: 'white', backgroundColor: '#35495e', fontSize: '12px', borderRadius: '4px 0 0 4px', paddingLeft: '13px' }, contentStyle: { padding: '3px', color: 'white', backgroundColor: '#28518a', fontSize: '12px', borderRadius: '0 4px 4px 0', paddingRight: '13px' }, countStyle: { padding: '3px', color: 'white', backgroundColor: 'green', fontSize: '12px', borderRadius: '4px', marginLeft: '5px', textAlign: 'center' } }
const initLog = ({ titleStyle, contentStyle, countStyle, delay = 500 } = {}) => { const map = new Map(); let time const printTemplate = (title, value, logCount = 1) => console.log( `%c${title}:%c${value}%c` + (logCount === 1 ? '': `×${logCount}`), getStyleStrByObj(Object.assign(defaultStyle.titleStyle, titleStyle)), getStyleStrByObj(Object.assign(defaultStyle.contentStyle, contentStyle)), getStyleStrByObj(Object.assign(defaultStyle.countStyle, countStyle)) ) return (title, value) => { const key = JSON.stringify({ title, value }) let newCount = 1 if (map.has(key)) newCount = map.get(key) + 1 map.set(key, newCount) printTemplate(title, value, newCount) if (delay === -1) return setTimeout(() => map.delete(key), delay) } }
export { initLog }
|