objects.js 883 B

123456789101112131415161718192021222324252627282930
  1. import pinyin from 'js-pinyin'
  2. // 中文转拼音 传入仅首字母
  3. Object.defineProperty(String.prototype, 'toPinyin', {
  4. writable: false,
  5. enumerable: false,
  6. configurable: true,
  7. value: function (first) {
  8. let str = this
  9. if (first) {
  10. return pinyin.getCamelChars(str).replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, '')
  11. }
  12. return pinyin.getFullChars(str).replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, '')
  13. }
  14. })
  15. // 字符检索 传入检索值
  16. Object.defineProperty(String.prototype, 'filter', {
  17. writable: false,
  18. enumerable: false,
  19. configurable: true,
  20. value: function (input) {
  21. let str = this
  22. let en = str.toLowerCase().includes(input.toLowerCase())
  23. let zhFull = str.toPinyin().toLowerCase().includes(input.toLowerCase())
  24. let zhFirst = str.toPinyin(true).toLowerCase().includes(input.toLowerCase())
  25. return en || zhFull || zhFirst
  26. }
  27. })