format.js 2.4 KB

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