Skip to content

Commit 8c9f109

Browse files
committed
barely started, added html page for testing
1 parent d24397c commit 8c9f109

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ A simple JavaScript function for converting pinyin containing tone numbers (`pin
55
The function `pinyinify()` takes a string parameter, which can be one of the following:
66

77
+ A single character, which must be one of: `a`, `e`, `i`, `o`, `u`, or <code>&#252;</code>, followed by a tone mark. `v` is an acceptable substitute for <code>&#252;</code>.
8-
+ A pinyin word or phrase using tone numbers. Examples: `mao1`, `chi1fan4`, `wo3men5`. For simplicity's sake, the function will process most words with tone numbers, including ones that may not be valid pinyin.
8+
+ A pinyin word or phrase using tone numbers. Examples: `mao1`, `chi1fan4`, `wo3 men5`. For simplicity's sake, the function will process most words with tone numbers, including ones that may not be valid pinyin.
99
+ A string containing both pinyin words and non-pinyin words, characters, or symbols. Example: `My Chinese name is yang2kai3wen2.` **Be careful:** any word that resembles the structure of pinyin (has a number at the end of it, for example) may be given tone marks. Therefore, it is recommended that you avoid this option if possible.
1010

1111
If you pass a string that meets one of the requirements above, the function will return a string with the tone numbers converted to tone marks. Otherwise, it will return an unchanged string.

pinyinify.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11

2+
var isAlpha = function(char) {
3+
return /^[A-Za-z]$/.test(char);
4+
}
5+
26
var pinyinify = function(str) {
37

4-
}
8+
var res = "";
9+
for (var i = 0; i < str.length; i++) {
10+
var char = str.charAt(i);
11+
if (isAlpha(char)) {
12+
//TODO look for index of tone number
13+
} else {
14+
res += char;
15+
}
16+
}
17+
18+
return res;
19+
}

test.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script>
5+
var submit = function(str) {
6+
// document.getElementById('result').innerHTML = pinyinify(str);
7+
document.getElementById('result').innerHTML = isAlpha(str);
8+
}
9+
</script>
10+
<script src="pinyinify.js"></script>
11+
</head>
12+
<body>
13+
<input type="text" id="textfield" name="textfield">
14+
<button onclick="submit(document.getElementById('textfield').value)" name="Submit">Submit</button>
15+
<p id="result"></p>
16+
</body>
17+
</html>

0 commit comments

Comments
 (0)