pinyin2.js 880 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import { pinyin } from './pinyin.js'
  2. export default {
  3. chineseToPinYin: function (l1) {
  4. var l2 = l1.length
  5. var I1 = ''
  6. var reg = new RegExp('[a-zA-Z0-9]')
  7. for (var i = 0; i < l2; i++) {
  8. var val = l1.substr(i, 1)
  9. var name = this.arraySearch(val, pinyin)
  10. if (reg.test(val)) {
  11. I1 += val
  12. } else if (name !== false) {
  13. I1 += name
  14. }
  15. }
  16. I1 = I1.replace(/ /g, '-')
  17. while (I1.indexOf('--') > 0) {
  18. I1 = I1.replace('--', '-')
  19. }
  20. return I1
  21. },
  22. arraySearch: function (l1, l2) {
  23. for (var name in pinyin) {
  24. if (pinyin[name].indexOf(l1) !== -1) {
  25. return this.ucfirst(name)
  26. }
  27. }
  28. return false
  29. },
  30. ucfirst: function (l1) {
  31. if (l1.length > 0) {
  32. var first = l1.substr(0, 1).toUpperCase()
  33. var spare = l1.substr(1, l1.length)
  34. return first + spare
  35. }
  36. }
  37. }