github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/client/src/app/utils/string.js (about)

     1  /**
     2   * 格式化字符串
     3   * @param {string} str 要格式化的字符串
     4   * @param  {...any} args 格式化参数
     5   * @return  {string} 格式化后的字符串
     6   * @example <caption>通过参数序号格式化</caption>
     7   *     var hello = $.format('{0} {1}!', 'Hello', 'world');
     8   *     // hello 值为 'Hello world!'
     9   * @example <caption>通过对象名称格式化</caption>
    10   *     var say = $.format('Say {what} to {who}', {what: 'hello', who: 'you'});
    11   *     // say 值为 'Say hello to you'
    12   */
    13  export const formatString = (str, ...args) => {
    14      let result = str;
    15      if (args.length > 0) {
    16          let reg;
    17          if (args.length === 1 && (typeof args[0] === 'object')) {
    18              args = args[0];
    19              Object.keys(args).forEach(key => {
    20                  if (args[key] !== undefined) {
    21                      reg = new RegExp(`({${key}})`, 'g');
    22                      result = result.replace(reg, args[key]);
    23                  }
    24              });
    25          } else {
    26              for (let i = 0; i < args.length; i++) {
    27                  if (args[i] !== undefined) {
    28                      reg = new RegExp(`({[${i}]})`, 'g');
    29                      result = result.replace(reg, args[i]);
    30                  }
    31              }
    32          }
    33      }
    34      return result;
    35  };
    36  
    37  /**
    38   * 字节单位表
    39   * @type {Object}
    40   */
    41  export const BYTE_UNITS = {
    42      B: 1,
    43      KB: 1024,
    44      MB: 1024 * 1024,
    45      GB: 1024 * 1024 * 1024,
    46      TB: 1024 * 1024 * 1024 * 1024,
    47  };
    48  
    49  /**
    50   * 格式化字节值为包含单位的字符串
    51   * @param {number} size 字节大小
    52   * @param {number} [fixed=2] 保留的小数点尾数
    53   * @param {string} [unit=''] 单位,如果留空,则自动使用最合适的单位
    54   * @return {string} 格式化后的字符串
    55   */
    56  export const formatBytes = (size, fixed = 2, unit = '') => {
    57      if (typeof size !== 'number') {
    58          size = Number.parseInt(size, 10);
    59      }
    60      if (Number.isNaN(size)) {
    61          return '?KB';
    62      }
    63      if (!unit) {
    64          if (size < BYTE_UNITS.KB) {
    65              unit = 'B';
    66          } else if (size < BYTE_UNITS.MB) {
    67              unit = 'KB';
    68          } else if (size < BYTE_UNITS.GB) {
    69              unit = 'MB';
    70          } else if (size < BYTE_UNITS.TB) {
    71              unit = 'GB';
    72          } else {
    73              unit = 'TB';
    74          }
    75      }
    76  
    77      return (size / BYTE_UNITS[unit]).toFixed(fixed) + unit;
    78  };
    79  
    80  /**
    81   * 检查字符串是否为未定义(`null` 或者 `undefined`)或者为空字符串
    82   * @param  {string} s 要检查的字符串
    83   * @return {boolean} 如果未定义或为空字符串则返回 `true`,否则返回 `false`
    84   */
    85  export const isEmptyString = s => (s === undefined || s === null || s === '');
    86  
    87  /**
    88   * 检查字符串是否不是空字符串
    89   * @param  {string} s 要检查的字符串
    90   * @return {boolean} 如果为非空字符串则返回 `true`,否则返回 `false`
    91   */
    92  export const isNotEmptyString = s => (s !== undefined && s !== null && s !== '');
    93  
    94  /**
    95   * 检查字符串是否不是空字符串,如果为空则返回第二个参数给定的字符串,否则返回字符串自身
    96   * @param  {string} str 要检查的字符串
    97   * @param  {string} thenStr 如果为空字符串时要返回的字符串
    98   * @return {boolean} 如果未定义或为空字符串则返回 [thenStr],否则返回 [str]
    99   */
   100  export const ifEmptyStringThen = (str, thenStr) => (isEmptyString(str) ? thenStr : str);
   101  
   102  /**
   103   * 确保字符串长度不超过指定值,如果超出则去掉截取的部分
   104   * @param {string} str 要操作的字符串
   105   * @param {number} length 要限制的最大长度
   106   * @param {string} suffix 如果超出显示要添加的后缀
   107   * @returns {string} 返回新的字符串
   108   */
   109  export const limitStringLength = (str, length, suffix) => {
   110      if (str.length > length) {
   111          str = str.substring(0, length);
   112          if (suffix) {
   113              str = `${str}${suffix}`;
   114          }
   115      }
   116      return str;
   117  };
   118  
   119  /**
   120   * 用于匹配 @ 用户的正则表达式
   121   * @type {string}
   122   */
   123  export const REGEXP_AT_USER = '@(#?[_.\\w\\d\\u4e00-\\u9fa5]{1,20})';
   124  
   125  /**
   126   * 还原包含 @ 成员的文本消息
   127   * @param {string} message @ 成员消息
   128   * @returns {string} 原是文本
   129   */
   130  export const restoreMessageContainAt = (message) => {
   131      if (typeof message !== 'string' || !message.replace) {
   132          return message;
   133      }
   134      return message.replace(new RegExp(`\\[(?<atuser>${REGEXP_AT_USER})\\]\\(\\@\\#\\d+\\)`, 'g'), '$<atuser>');
   135  };
   136  
   137  export default {
   138      format: formatString,
   139      isEmpty: isEmptyString,
   140      isNotEmpty: isNotEmptyString,
   141      formatBytes,
   142      ifEmptyThen: ifEmptyStringThen,
   143      limitLength: limitStringLength
   144  };