format.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.toFixed = void 0;
  4. var constant_1 = require("./constant");
  5. var util_1 = require("./util");
  6. function initials(py) {
  7. for (var i = 0, l = constant_1.INITIALS.length; i < l; i++) {
  8. if (py.indexOf(constant_1.INITIALS[i]) === 0) {
  9. return constant_1.INITIALS[i];
  10. }
  11. }
  12. return "";
  13. }
  14. var RE_PHONETIC_SYMBOL = new RegExp("([" + Object.keys(constant_1.PHONETIC_SYMBOL).join("") + "])", "g");
  15. var RE_TONE2 = /([aeoiuvnm])([0-4])$/;
  16. function toFixed(pinyin, style) {
  17. var tone = "";
  18. var first_letter;
  19. var py;
  20. switch (style) {
  21. case constant_1.ENUM_PINYIN_STYLE.INITIALS:
  22. return initials(pinyin);
  23. case constant_1.ENUM_PINYIN_STYLE.FIRST_LETTER:
  24. first_letter = pinyin.charAt(0);
  25. if ((0, util_1.hasKey)(constant_1.PHONETIC_SYMBOL, first_letter)) {
  26. first_letter = constant_1.PHONETIC_SYMBOL[first_letter].charAt(0);
  27. }
  28. return first_letter;
  29. case constant_1.ENUM_PINYIN_STYLE.NORMAL:
  30. return pinyin.replace(RE_PHONETIC_SYMBOL, function ($0, $1_phonetic) {
  31. return constant_1.PHONETIC_SYMBOL[$1_phonetic].replace(RE_TONE2, "$1");
  32. });
  33. case constant_1.ENUM_PINYIN_STYLE.PASSPORT:
  34. return pinyin.replace(RE_PHONETIC_SYMBOL, function ($0, $1_phonetic) {
  35. return constant_1.PHONETIC_SYMBOL[$1_phonetic].replace(RE_TONE2, "$1").replace("v", "YU");
  36. }).toUpperCase();
  37. case constant_1.ENUM_PINYIN_STYLE.TO3NE:
  38. return pinyin.replace(RE_PHONETIC_SYMBOL, function ($0, $1_phonetic) {
  39. return constant_1.PHONETIC_SYMBOL[$1_phonetic];
  40. });
  41. case constant_1.ENUM_PINYIN_STYLE.TONE2:
  42. py = pinyin.replace(RE_PHONETIC_SYMBOL, function ($0, $1) {
  43. tone = constant_1.PHONETIC_SYMBOL[$1].replace(RE_TONE2, "$2");
  44. return constant_1.PHONETIC_SYMBOL[$1].replace(RE_TONE2, "$1");
  45. });
  46. return py + tone;
  47. case constant_1.ENUM_PINYIN_STYLE.TONE:
  48. default:
  49. return pinyin;
  50. }
  51. }
  52. exports.toFixed = toFixed;
  53. //# sourceMappingURL=format.js.map