Skip to content

Commit

Permalink
Add Hangul syllable character name generator
Browse files Browse the repository at this point in the history
  • Loading branch information
mooniker committed Sep 23, 2019
1 parent fc8afc3 commit bb05c53
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
41 changes: 41 additions & 0 deletions hangul.characterNameGeneration.js
@@ -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 };
19 changes: 19 additions & 0 deletions hangul.characterNameGeneration.test.js
@@ -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);
});
});
});
76 changes: 76 additions & 0 deletions hangul.shortNames.js
@@ -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"
]
};

0 comments on commit bb05c53

Please sign in to comment.