返回列表

String.prototype.startsWith, String.prototype.startWith, String.prototype.endsWith, String.prototype.endWith, String.prototype.trim

默认分类 2018/07/11 10:05

代码如下

// 去重
Array.prototype.unique = function (copy) {
  var list1 = this
  list1.reverse()
  var i, j, list2 = []
  for (i = list1.length - 1; i > -1; i--) {
    var item = copy === 'copy' ? list1[i] : list1.pop()
    var exist = false
    for (j = list2.length - 1; !exist && j > -1; j--) {
      if ('number,string'.indexOf(typeof item) > -1 && 'number,string'.indexOf(typeof list2[j]) > -1) {
        if (String(item) === String(list2[j])) {
          if (typeof item === 'number') list2[j] = item
          exist = true
        }
      } else if (item === list2[j]) exist = true
    }
    if (!exist) list2.push(item)
  }

  if (copy === 'copy') {
    this.reverse()
    return list2
  } else {
    for (j = list2.length - 1; j > -1; j--) this.push(list2[j])
    this.reverse()
    return this
  }
}
// 交集 (相同部分)
Array.prototype.joint = function (list2, copy) {
  if (Object.prototype.toString.call(list2) !== '[object Array]') return this
  var i, j, list1 = this
  var res = []
  for (i = list1.length - 1; i > -1; i--) {
    var item = list1[i]
    var exist = false
    for (j = list2.length - 1; !exist && j > -1; j--) {
      if ('number,string'.indexOf(typeof item) > -1 && 'number,string'.indexOf(typeof list2[j]) > -1) {
        if (String(item) === String(list2[j])) exist = true
      } else if (item === list2[j]) exist = true
    }
    if (exist) res.push(item)
  }
  if (copy === 'copy') {
    res.reverse()
    return res
  } else {
    this.length = 0
    for (j = res.length - 1; j > -1; j--) this.push(res[j])
    return this
  }
}
// 差集 (去掉 相同部分 剩下的源集合)
Array.prototype.sub = function (list2, copy) {
  if (Object.prototype.toString.call(list2) !== '[object Array]') return this
  var i, j, list1 = this
  var res = []
  for (i = list1.length - 1; i > -1; i--) {
    var item = list1[i]
    var exist = false
    for (j = list2.length - 1; !exist && j > -1; j--) {
      if ('number,string'.indexOf(typeof item) > -1 && 'number,string'.indexOf(typeof list2[j]) > -1) {
        if (String(item) === String(list2[j])) exist = true
      } else if (item === list2[j]) exist = true
    }
    if (!exist) res.push(item)
  }
  if (copy === 'copy') {
    res.reverse()
    return res
  } else {
    this.length = 0
    for (j = res.length - 1; j > -1; j--) this.push(res[j])
    return this
  }
}
// 存在否
Array.prototype.exist = function (vv, strict) {
  var list1 = this
  var exist = false
  for (var i = list1.length - 1; !exist && i > -1; i--) {
    if (strict) {
      if (vv === list1[i]) exist = true
    } else {
      if ('number,string'.indexOf(typeof vv) > -1 && 'number,string'.indexOf(typeof list1[i]) > -1) {
        if (String(vv) === String(list1[i])) exist = true
      } else if (vv === list1[i]) exist = true
    }
  }
  return exist
}
// 转MAP
Array.prototype.tomap = function (vv) {
  var res = {}
  var list = this
  for (var i = list.length - 1; i > -1; i--) {
    res[list[i]] = vv === undefined ? true : vv
  }
  return res
}

if (!String.prototype.startsWith) {
  String.prototype.startsWith = function (search, pos) {
    return this.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search
  }
}
if (!String.prototype.startWith) String.prototype.startWith = String.prototype.startsWith
if (!String.prototype.endsWith) {
  String.prototype.endsWith = function (search, this_len) {
    if (this_len === undefined || this_len > this.length) {
      this_len = this.length
    }
    return this.substring(this_len - search.length, this_len) === search
  }
}
if (!String.prototype.endWith) String.prototype.endWith = String.prototype.endsWith
if (!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '')
  }
}