|
1 | 1 | var toneMarks = { |
2 | | - a: ["a", "\u0101", "\u00e1", "\u01ce", "\u00e0", "a"], |
3 | | - e: ["e", "\u0113", "\u00e9", "\u011b", "\u00e8", "e"], |
4 | | - i: ["i", "\u012b", "\u00ed", "\u01d0", "\u00ec", "i"], |
5 | | - o: ["o", "\u014d", "\u00f3", "\u01d2", "\u00f2", "o"], |
6 | | - u: ["u", "\u016b", "\u00fa", "\u01d4", "\u00f9", "u"], |
7 | | - v: ["\u00fc", "\u01d6", "\u01d8", "\u01da", "\u01dc", "\u00fc"] |
| 2 | + a: ["a", "\u0101", "\u00e1", "\u01ce", "\u00e0", "a"], |
| 3 | + e: ["e", "\u0113", "\u00e9", "\u011b", "\u00e8", "e"], |
| 4 | + i: ["i", "\u012b", "\u00ed", "\u01d0", "\u00ec", "i"], |
| 5 | + o: ["o", "\u014d", "\u00f3", "\u01d2", "\u00f2", "o"], |
| 6 | + u: ["u", "\u016b", "\u00fa", "\u01d4", "\u00f9", "u"], |
| 7 | + v: ["\u00fc", "\u01d6", "\u01d8", "\u01da", "\u01dc", "\u00fc"] |
8 | 8 | }; |
9 | 9 |
|
10 | 10 | String.prototype.isAlpha = function() { |
11 | 11 |
|
12 | | - return /^[A-Za-z]$/.test(this); |
| 12 | + return /^[A-Za-z]$/.test(this); |
13 | 13 | } |
14 | 14 |
|
15 | 15 | String.prototype.isPinyinVowel = function() { |
16 | 16 |
|
17 | | - return /^[aeiouv\u00fc]$/.test(this); |
| 17 | + return /^[aeiouv\u00fc]$/.test(this); |
18 | 18 | } |
19 | 19 |
|
20 | 20 | String.prototype.lastIndexOfRegex = function(regExp) { |
21 | 21 |
|
22 | | - var lastIndex = -1; |
23 | | - for (var i = 0; i < this.length; i++) { |
| 22 | + var lastIndex = -1; |
| 23 | + for (var i = 0; i < this.length; i++) { |
24 | 24 |
|
25 | | - if (regExp.test(this.charAt(i))) { |
| 25 | + if (regExp.test(this.charAt(i))) { |
26 | 26 |
|
27 | | - lastIndex = i; |
28 | | - } |
29 | | - } |
| 27 | + lastIndex = i; |
| 28 | + } |
| 29 | + } |
30 | 30 |
|
31 | | - return lastIndex; |
| 31 | + return lastIndex; |
32 | 32 | } |
33 | 33 |
|
34 | 34 | String.prototype.replaceAt = function(index, replacement) { |
35 | 35 |
|
36 | | - //TODO |
| 36 | + if (index >= 0 && index < this.length && typeof replacement === "string") { |
| 37 | + |
| 38 | + return this.substring(0, index) + replacement |
| 39 | + + this.substring(index + 1); |
| 40 | + } else { |
| 41 | + |
| 42 | + return this; |
| 43 | + } |
37 | 44 | } |
38 | 45 |
|
39 | 46 | /** |
40 | 47 | * Takes a single pinyin word using tone numbers and converts to tone symbols. |
41 | 48 | */ |
42 | 49 | String.prototype.convertPinyin = function() { |
43 | 50 |
|
44 | | - var str = this.toLocaleLowerCase(); |
| 51 | + var str = this.toLocaleLowerCase(); |
45 | 52 |
|
46 | | - var toneNumIndex = str.search(/[1-5]/); |
47 | | - var firstVowelIndex = str.search(/[aeiouv\u00fc]/); |
48 | | - if (str.length > 7 || toneNumIndex < 1 || |
49 | | - toneNumIndex !== str.length - 1 || |
50 | | - firstVowelIndex < 0) { |
| 53 | + var toneNumIndex = str.search(/[1-5]/); |
| 54 | + var firstVowelIndex = str.search(/[aeiouv\u00fc]/); |
| 55 | + if (str.length > 7 || toneNumIndex < 1 || |
| 56 | + toneNumIndex !== str.length - 1 || |
| 57 | + firstVowelIndex < 0) { |
51 | 58 |
|
52 | | - console.log("String.prototype.convertPinyin:" + this + |
53 | | - " is not a valid pinyin word.") |
54 | | - return this; |
55 | | - } |
| 59 | + console.log("String.prototype.convertPinyin:" + this + |
| 60 | + " is not a valid pinyin word.") |
| 61 | + return this; |
| 62 | + } |
56 | 63 |
|
57 | | - var toneNum = parseInt(str[toneNumIndex]); |
58 | | - if (/[ae]/.test(str)) { |
| 64 | + var toneNum = parseInt(str[toneNumIndex]); |
| 65 | + if (/[ae]/.test(str)) { |
59 | 66 |
|
60 | | - var index = str.search(/[ae]/); |
61 | | - str = str.replaceAt(index, toneMarks[str.charAt(index)][toneNum]); |
62 | | - } else if (/ou/.test(str)) { |
| 67 | + var index = str.search(/[ae]/); |
| 68 | + str = str.replaceAt(index, toneMarks[str.charAt(index)][toneNum]); |
| 69 | + } else if (/ou/.test(str)) { |
63 | 70 |
|
64 | | - var index = str.search(/ou/); |
65 | | - str = str.replaceAt(index, toneMarks[str.charAt(index)][toneNum]); |
66 | | - } else { |
| 71 | + var index = str.search(/ou/); |
| 72 | + str = str.replaceAt(index, toneMarks[str.charAt(index)][toneNum]); |
| 73 | + } else { |
67 | 74 |
|
68 | | - var index = str.lastIndexOfRegex(/[aeiouv\u00fc]/); |
69 | | - var vowel = str.charAt(index); |
70 | | - if (vowel == "\u00fc") { |
| 75 | + var index = str.lastIndexOfRegex(/[aeiouv\u00fc]/); |
| 76 | + var vowel = str.charAt(index); |
| 77 | + if (vowel == "\u00fc") { |
71 | 78 |
|
72 | | - vowel = "v"; |
73 | | - } |
74 | | - str = str.replaceAt(index, toneMarks[vowel][toneNum]); |
75 | | - } |
| 79 | + vowel = "v"; |
| 80 | + } |
| 81 | + str = str.replaceAt(index, toneMarks[vowel][toneNum]); |
| 82 | + } |
76 | 83 |
|
77 | | - return str; |
| 84 | + str = str.substring(0, str.length - 1); |
| 85 | + return str; |
78 | 86 | } |
79 | 87 |
|
80 | 88 | var pinyinify = function(str) { |
81 | 89 |
|
82 | | - if (typeof str !== 'string') { |
| 90 | + if (typeof str !== 'string') { |
83 | 91 |
|
84 | | - return str; |
85 | | - } |
| 92 | + return str; |
| 93 | + } |
86 | 94 |
|
87 | | - var res = ""; |
88 | | - var i = 0; |
89 | | - while (str.length > 0) { |
| 95 | + var res = ""; |
| 96 | + var i = 0; |
| 97 | + while (str.length > 0) { |
90 | 98 |
|
91 | | - var char = str.charAt(i); |
92 | | - if (char.isAlpha()) { |
| 99 | + var char = str.charAt(i); |
| 100 | + if (char.isAlpha()) { |
93 | 101 |
|
94 | | - if (i !== 0) { |
| 102 | + if (i !== 0) { |
95 | 103 |
|
96 | | - res += str.substring(0, i); |
97 | | - str = str.substring(i); |
98 | | - i = 0; |
99 | | - } |
100 | | - var toneNumIndex = str.search(/[1-5]/); |
101 | | - if (toneNumIndex > 0 && toneNumIndex < 7) { |
| 104 | + res += str.substring(0, i); |
| 105 | + str = str.substring(i); |
| 106 | + i = 0; |
| 107 | + } |
| 108 | + var toneNumIndex = str.search(/[1-5]/); |
| 109 | + if (toneNumIndex > 0 && toneNumIndex < 7) { |
102 | 110 |
|
103 | | - res += str.substring(0, toneNumIndex + 1).convertPinyin(); |
104 | | - str = str.substring(toneNumIndex + 1); |
105 | | - } else { |
| 111 | + res += str.substring(0, toneNumIndex + 1).convertPinyin(); |
| 112 | + str = str.substring(toneNumIndex + 1); |
| 113 | + } else { |
106 | 114 |
|
107 | | - var whitespaceIndex = str.search(/\s/); |
108 | | - if (whitespaceIndex < 0) { |
| 115 | + var whitespaceIndex = str.search(/\s/); |
| 116 | + if (whitespaceIndex < 0) { |
109 | 117 |
|
110 | | - res += str.substring(0); |
111 | | - str = ""; |
112 | | - } else { |
| 118 | + res += str.substring(0); |
| 119 | + str = ""; |
| 120 | + } else { |
113 | 121 |
|
114 | | - res += str.substring(0, whitespaceIndex + 1); |
115 | | - str = str.substring(whitespaceIndex + 1); |
116 | | - } |
117 | | - } |
| 122 | + res += str.substring(0, whitespaceIndex + 1); |
| 123 | + str = str.substring(whitespaceIndex + 1); |
| 124 | + } |
| 125 | + } |
118 | 126 |
|
119 | | - } else { |
| 127 | + } else { |
120 | 128 |
|
121 | | - i++; |
122 | | - } |
123 | | - } |
| 129 | + i++; |
| 130 | + } |
| 131 | + } |
124 | 132 |
|
125 | | - return res; |
| 133 | + return res; |
126 | 134 | } |
0 commit comments