interface String {
  clearSpaces(pos?: string | null): string;
  clearBr(): string;
  clearSpacesAndBr(): string;
  clearLeftSpaces(): string;
  clearRightSpaces(): string;
  clearBothSidesSpaces(): string;
  clearInsideSpaces(): string;
  formatDate(format: string): string;
  replaceAll(searchValue: string | RegExp, replaceValue: string): string;
}

/**
 * 清除字符串所有空格
 * @param pos both(左右)|left|right|all|inner 默认all
 * @constructor
 */
String.prototype.clearSpaces = function (pos?: string | null = 'all') {
  if (pos === 'all') {
    return this.replace(/\s+/g, '')
  }
  if (pos === 'both') {
    return this.replace(/^\s+|\s+$/g, '')
  }
  if (pos === 'left') {
    return this.replace(/^\s*/, '')
  }
  if (pos === 'right') {
    return this.replace(/(\s*$)/g, '')
  }
  if (pos === 'inner') {
    return this.replace(/\s/g, '')
  }
  return this
}

/**
 * 清除字符串所有换行
 * @return {string}
 */
String.prototype.clearBr = function () {
  const str = this.replace(/<\/?.+?>/g, '')
  str.replace(/[\r\n]/g, '')
  return str
}

/**
 * 清除字符串所有空格和回车
 * @return {string}
 */
String.prototype.clearSpacesAndBr = function () {
  let str = this.clearBr()
  str = str.clearSpaces()
  return str
}

// /**
//  * 清除字符串左边空格
//  * @return {string}
//  */
// String.prototype.clearLeftSpaces = function () {
//   return this.replace(/^\s*/g, '')
// }

// /**
//  * 清除字符串右边空格
//  * @return {string}
//  */
// String.prototype.clearRightSpaces = function () {
//   return this.replace(/\s*$/g, '')
// }

// /**
//  * 清除字符串两边空格
//  * @return {string}
//  */
// String.prototype.clearBothSidesSpaces = function () {
//   return this.replace(/(^\s*)|(\s*$)/g, '')
// }

/**
 * 清除字符串中间空格
 * @return {string}
 */
String.prototype.clearInsideSpaces = function () {
  return this.replace(/\s/g, '')
}

String.prototype.formatDate = function (format) {
  const time = this.replaceAll(/-/g, "/");
  return new Date(time).format(format);
}
String.prototype.replaceAll = function (searchValue: string | RegExp, replaceValue: string): string{
  const target = this;
  return target.replace(new RegExp(searchValue, 'g'), replaceValue);
}