all files / src/String/ index.js

24% Statements 6/25
31.25% Branches 5/16
16.67% Functions 1/6
24% Lines 6/25
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                                                                                                                                     
/**
* trim 去除空格
* @since 1.0.0
* @param {String} str
* @param {Number} type 1-所有空格 2-前后空格 3-前空格 4-后空格
* @returns {String}
*/
export function trim(str,type= 1){
  type = parseInt(type)
  switch (type) {
    case 1:
      return str.replace(/\s+/g, "");
    case 2:
      return str.replace(/(^\s*)|(\s*$)/g, "");
    case 3:
      return str.replace(/(^\s*)/g, "");
    case 4:
      return str.replace(/(\s*$)/g, "");
    default:
      return str;
  }
}
 
/**
* changeCase 变换书写形式
* @since 1.0.0
* @param {String} str
* @param {Number} type 1-首字母大写  2-首页母小写  3-大小写转换  4-全部大写  5-全部小写
* @returns {String}
*/
export function changeCase(str,type){
  type = type || 4
  switch (type) {
    case 1:
      return str.replace(/\b\w+\b/g, function (word) {
        return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase();
 
      });
    case 2:
      return str.replace(/\b\w+\b/g, function (word) {
        return word.substring(0, 1).toLowerCase() + word.substring(1).toUpperCase();
      });
    case 3:
      return str.split('').map( function(word){
        if (/[a-z]/.test(word)) {
          return word.toUpperCase();
        }else{
          return word.toLowerCase()
        }
      }).join('')
    case 4:
      return str.toUpperCase();
    case 5:
      return str.toLowerCase();
    default:
      return str;
  }
}
 
/**
* filterTag 过滤html标签
* @since 1.0.0
* @param {String} str
* @returns {String}
*/
export function filterTag(str){
  str = str.replace(/&/ig, "&");
  str = str.replace(/</ig, "&lt;");
  str = str.replace(/>/ig, "&gt;");
  str = str.replace(" ", "&nbsp;");
  return str;
}