vuejs filters收集

作者: zengde 分类: 技术相关 发布时间: 2018-11-10 09:26
filters:{
        date(str,format){
            let date = new Date();
            date.setTime(str*1000);
            
            return format
                  .replace('YYYY', `${date.getFullYear()}`)
                  .replace('YY', `${date.getFullYear() % 100}`)
                  .replace('MM', `${date.getMonth()}`.padStart(2, '0'))
                  .replace('M', `${date.getMonth() + 1}`)
                  .replace('DD', `${date.getDate()}`.padStart(2, '0'))
                  .replace('D', `${date.getDate()}`)
                  .replace('HH', `${date.getHours()}`.padStart(2, '0'))
                  .replace('H', `${date.getHours()}`)
                  .replace('hh', `${date.getHours() % 12}`.padStart(2, '0'))
                  .replace('h', `${date.getHours() % 12}`)
                  .replace('A', date.getHours() < 12 ? 'AM' : 'PM')
                  .replace('a', date.getHours() < 12 ? 'am' : 'pm')
                  .replace('mm', `${date.getMinutes()}`.padStart(2, '0'))
                  .replace('m', `${date.getMinutes()}`)
                  .replace('ss', `${date.getSeconds()}`.padStart(2, '0'))
                  .replace('s', `${date.getSeconds()}`);
        },
        truncate(text,length, clamp){
            text = text || '';
            clamp = clamp || '...';
            length = length || 30;
            
            if (text.length <= length) return text;
      
            var tcText = text.slice(0, length - clamp.length);
            var last = tcText.length - 1;
            
      
            while (last > 0 && tcText[last] !== ' ' && tcText[last] !== clamp[0]) last -= 1;
      
            // Fix for case when text dont have any `space`
            last = last || length - clamp.length;
      
            tcText =  tcText.slice(0, last);
      
            return tcText + clamp;
        },
        formatSize:function(size){
            if (size > 1024 * 1024 * 1024 * 1024) {
                return (size / 1024 / 1024 / 1024 / 1024).toFixed(2) + ' TB'
              } else if (size > 1024 * 1024 * 1024) {
                return (size / 1024 / 1024 / 1024).toFixed(2) + ' GB'
              } else if (size > 1024 * 1024) {
                return (size / 1024 / 1024).toFixed(2) + ' MB'
              } else if (size > 1024) {
                return (size / 1024).toFixed(2) + ' KB'
              }
              return size.toString() + ' B'
        },
        highlight(words,query){
            return words.replace(query, '<i class="text-danger">' + query + '</i>')
        }
    }