/**
 * Program: cut String by length
 * Author: SDL
 * Description: cut String by length
 */
import _ from 'lodash';

export default (str, len) => {
  if (!_.isEmpty(str)) {
    let s = 0;
    for (let i = 0; i < str.length; i += 1) {
      s += str.charCodeAt(i) > 128 ? 2 : 1;
      if (s > len) return `${str.substring(0, i)}...`;
    }
  }
  return str;
};
