Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Hangul syllable character name generator
- Loading branch information
Showing
3 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const { | ||
computeSIndex, | ||
computeLIndex, | ||
computeVIndex, | ||
computeTIndex | ||
} = require("./hangul.computations"); | ||
|
||
const { | ||
JAMO_L_TABLE, | ||
JAMO_V_TABLE, | ||
JAMO_T_TABLE | ||
} = require("./hangul.shortNames"); | ||
|
||
/** | ||
* Generates a letter/jamo-derived short name for a given precomposed Hangul syllable | ||
* | ||
* @param {(string|integer)} s | ||
* @returns {string} Unicode Hangul syllable short name | ||
*/ | ||
function getHangulCharShortName(s) { | ||
const SIndex = computeSIndex(s); | ||
const LIndex = computeLIndex(SIndex); | ||
const VIndex = computeVIndex(SIndex); | ||
const TIndex = computeTIndex(SIndex); | ||
|
||
return JAMO_L_TABLE[LIndex] + JAMO_V_TABLE[VIndex] + JAMO_T_TABLE[TIndex]; | ||
} | ||
|
||
/** | ||
* Constructs a precomposed Hangul syllable name | ||
* | ||
* Based on "Hangul Character Name Generation" as described in Unicode core | ||
* specification for sample code for Hangul algorithms, under section | ||
* "3.12 Conjoining Jamo Behavior" (pp. 142-151) | ||
* | ||
* @param {(string|integer)} s | ||
* @returns {string} Unicode Hangul syllable name | ||
*/ | ||
const getHangulCharName = s => "HANGUL SYLLABLE " + getHangulCharShortName(s); | ||
|
||
module.exports = { getHangulCharShortName, getHangulCharName }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const { getHangulCharName } = require("./hangul.characterNameGeneration"); | ||
const lo = require("unicode/category/Lo"); | ||
|
||
const hangulCharNames = { | ||
가: "ga", | ||
나: "na", | ||
다: "da" | ||
}; | ||
|
||
describe("getHangulCharName function", () => { | ||
Object.entries(hangulCharNames).forEach(([hangulChar, expectedShortName]) => { | ||
const expectedName = "HANGUL SYLLABLE " + expectedShortName.toUpperCase(); | ||
const name = getHangulCharName(hangulChar); | ||
|
||
test(`should fetch '${expectedName}' for ${hangulChar}`, () => { | ||
expect(name).toBe(expectedName); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
module.exports = { | ||
JAMO_L_TABLE: [ | ||
"G", | ||
"GG", | ||
"N", | ||
"D", | ||
"DD", | ||
"R", | ||
"M", | ||
"B", | ||
"BB", | ||
"S", | ||
"SS", | ||
"", | ||
"J", | ||
"JJ", | ||
"C", | ||
"K", | ||
"T", | ||
"P", | ||
"H" | ||
], | ||
JAMO_V_TABLE: [ | ||
"A", | ||
"AE", | ||
"YA", | ||
"YAE", | ||
"EO", | ||
"E", | ||
"YEO", | ||
"YE", | ||
"O", | ||
"WA", | ||
"WAE", | ||
"OE", | ||
"YO", | ||
"U", | ||
"WEO", | ||
"WE", | ||
"WI", | ||
"YU", | ||
"EU", | ||
"YI", | ||
"I" | ||
], | ||
JAMO_T_TABLE: [ | ||
"", | ||
"G", | ||
"GG", | ||
"GS", | ||
"N", | ||
"NJ", | ||
"NH", | ||
"D", | ||
"L", | ||
"LG", | ||
"LM", | ||
"LB", | ||
"LS", | ||
"LT", | ||
"LP", | ||
"LH", | ||
"M", | ||
"B", | ||
"BS", | ||
"S", | ||
"SS", | ||
"NG", | ||
"J", | ||
"C", | ||
"K", | ||
"T", | ||
"P", | ||
"H" | ||
] | ||
}; |