Skip to content

Commit

Permalink
Change jamo lib to nested arrays with objects
Browse files Browse the repository at this point in the history
  • Loading branch information
mooniker committed Sep 24, 2019
1 parent 0a5235d commit 5365d52
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 101 deletions.
9 changes: 5 additions & 4 deletions jamo.compose.js
@@ -1,13 +1,14 @@
// The precomposed hangul syllables in the Hangul Syllables block in Unicode are algorithmically defined, using the following formula:
// [(initial) × 588 + (medial) × 28 + (final)] + 44032

const { initialConsonants, medialVowels, finalConsonants } = require("./jamo");
const _ = require("lodash");
const [initialConsonants, medialVowels, finalConsonants] = require("./jamo");

const compose = (initial, medial, final = null) => {
const hex =
initialConsonants.indexOf(initial) * 588 +
medialVowels.indexOf(medial) * 28 +
finalConsonants.indexOf(final) +
_.findIndex(initialConsonants, { jamo: initial }) * 588 +
_.findIndex(medialVowels, { jamo: medial }) * 28 +
(final ? _.findIndex(finalConsonants, { jamo: final }) : 0) +
44032;
return String.fromCodePoint(hex);
};
Expand Down
9 changes: 5 additions & 4 deletions jamo.decompose.js
Expand Up @@ -4,13 +4,13 @@
const { HANGUL } = require("./unicode-blocks");
const [START] = HANGUL.SYLLABLES;

const { initialConsonants, medialVowels, finalConsonants } = require("./jamo");
const [initialConsonants, medialVowels, finalConsonants] = require("./jamo");

const getInitialJamoIdx = syllable =>
Math.floor((syllable.charCodeAt(0) - START) / 588);

const getInitialJamo = syllable =>
initialConsonants[getInitialJamoIdx(syllable)];
initialConsonants[getInitialJamoIdx(syllable)].jamo;

const getMedialJamoIdx = syllable => {
const hangulCode = syllable.charCodeAt(0) - START;
Expand All @@ -19,7 +19,7 @@ const getMedialJamoIdx = syllable => {
return medial;
};

const getMedialJamo = syllable => medialVowels[getMedialJamoIdx(syllable)];
const getMedialJamo = syllable => medialVowels[getMedialJamoIdx(syllable)].jamo;

const getFinalJamoIdx = syllable => {
const hangulCode = syllable.charCodeAt(0) - START;
Expand All @@ -29,7 +29,8 @@ const getFinalJamoIdx = syllable => {
};

const getFinalJamo = syllable => {
return finalConsonants[getFinalJamoIdx(syllable)];
const finalJamoIdx = getFinalJamoIdx(syllable);
return finalJamoIdx ? finalConsonants[getFinalJamoIdx(syllable)].jamo : null;
};

const decomposeSyllable = syllable => {
Expand Down
162 changes: 79 additions & 83 deletions jamo.js
@@ -1,85 +1,81 @@
// choseong
const initialConsonants = [
"ㄱ", // 0
"ㄲ", // ㄱㄱ
"ㄴ",
"ㄷ",
"ㄸ", // ㄷㄷ
"ㄹ", // 5
"ㅁ",
"ㅂ",
"ㅃ", // ㅂㅂ
"ㅅ",
"ㅆ", // ㅅㅅ
"ㅇ",
"ㅈ",
"ㅉ", // ㅈㅈ
"ㅊ",
"ㅋ", // 15
"ㅌ",
"ㅍ",
"ㅎ"
];

// jungseong
const medialVowels = [
"ㅏ",
"ㅐ",
"ㅑ",
"ㅒ",
"ㅓ",
"ㅔ",
"ㅕ",
"ㅖ",
"ㅗ",
"ㅘ", // ㅗㅏ
"ㅙ", // ㅗㅐ
"ㅚ",
"ㅛ",
"ㅜ",
"ㅝ", // ㅜㅓ
"ㅞ", // ㅜㅔ
"ㅟ", // ㅜㅣ
"ㅠ",
"ㅡ",
"ㅢ", // ㅡㅣ
"ㅣ"
];
// "...what have the Romans ever done for us?"

// jongseong
const finalConsonants = [
null,
"ㄱ",
"ㄲ", // ㄱㄱ
"ㄳ", // ㄱㅅ
"ㄴ",
"ㄵ", // ㄴㅈ
"ㄶ", // ㄴㅎ
"ㄷ",
"ㄹ",
"ㄺ", // ㄹㄱ
"ㄻ", // ㄹㅁ
"ㄼ", // ㄹㅂ
"ㄽ", // ㄹㅅ
"ㄾ", // ㄹㅌ
"ㄿ", // ㄹㅍ
"ㅀ", // ㄹㅎ
"ㅁ",
"ㅂ",
"ㅄ", // ㅂㅅ
"ㅅ",
"ㅆ", // ㅅㅅ
"ㅇ",
"ㅈ",
"ㅊ",
"ㅋ",
"ㅌ",
"ㅍ",
"ㅎ"
module.exports = [
// choseong
[
{ jamo: "ㄱ", roman: "g" },
{ jamo: "ㄲ", roman: "gg" },
{ jamo: "ㄴ", roman: "n" },
{ jamo: "ㄷ", roman: "d" },
{ jamo: "ㄸ", roman: "dd" },
{ jamo: "ㄹ", roman: "r" },
{ jamo: "ㅁ", roman: "m" },
{ jamo: "ㅂ", roman: "b" },
{ jamo: "ㅃ", roman: "bb" },
{ jamo: "ㅅ", roman: "s" },
{ jamo: "ㅆ", roman: "ss" },
{ jamo: "ㅇ", roman: "" },
{ jamo: "ㅈ", roman: "j" },
{ jamo: "ㅉ", roman: "jj" },
{ jamo: "ㅊ", roman: "ch" },
{ jamo: "ㅋ", roman: "k" },
{ jamo: "ㅌ", roman: "t" },
{ jamo: "ㅍ", roman: "p" },
{ jamo: "ㅎ", roman: "h" }
],
// jungseong
[
{ jamo: "ㅏ", roman: "a" },
{ jamo: "ㅐ", roman: "ae" },
{ jamo: "ㅑ", roman: "yeo" },
{ jamo: "ㅒ", roman: "yae" },
{ jamo: "ㅓ", roman: "eo" },
{ jamo: "ㅔ", roman: "ye" },
{ jamo: "ㅕ", roman: "yeo" },
{ jamo: "ㅖ", roman: "ye" },
{ jamo: "ㅗ", roman: "o" },
{ jamo: "ㅘ", roman: "wa" },
{ jamo: "ㅙ", roman: "wae" },
{ jamo: "ㅚ", roman: "woe" },
{ jamo: "ㅛ", roman: "yo" },
{ jamo: "ㅜ", roman: "u" },
{ jamo: "ㅝ", roman: "wo" },
{ jamo: "ㅞ", roman: "we" },
{ jamo: "ㅟ", roman: "wi" },
{ jamo: "ㅠ", roman: "yu" },
{ jamo: "ㅡ", roman: "eu" },
{ jamo: "ㅢ", roman: "ui" },
{ jamo: "ㅣ", roman: "i" }
],
// jongseong
[
null,
{ jamo: "ㄱ", roman: "k" },
{ jamo: "ㄲ", roman: "k" },
{ jamo: "ㄳ", roman: "k" },
{ jamo: "ㄴ", roman: "n" },
{ jamo: "ㄵ", roman: "n" },
{ jamo: "ㄶ", roman: "n" },
{ jamo: "ㄷ", roman: "d" },
{ jamo: "ㄹ", roman: "l" },
{ jamo: "ㄺ", roman: "r" },
{ jamo: "ㄻ", roman: "lm" },
{ jamo: "ㄼ", roman: "lb" },
{ jamo: "ㄽ", roman: "ls" },
{ jamo: "ㄾ", roman: "lt" },
{ jamo: "ㄿ", roman: "lp" },
{ jamo: "ㅀ", roman: "lh" },
{ jamo: "ㅁ", roman: "m" },
{ jamo: "ㅂ", roman: "b" },
{ jamo: "ㅄ", roman: "bs" },
{ jamo: "ㅅ", roman: "s" },
{ jamo: "ㅆ", roman: "ss" },
{ jamo: "ㅇ", roman: "ng" },
{ jamo: "ㅈ", roman: "j" },
{ jamo: "ㅊ", roman: "ch" },
{ jamo: "ㅋ", roman: "k" },
{ jamo: "ㅌ", roman: "t" },
{ jamo: "ㅍ", roman: "p" },
{ jamo: "ㅎ", roman: "h" }
]
];

module.exports = {
initialConsonants,
medialVowels,
finalConsonants
};
18 changes: 10 additions & 8 deletions jamo.test.js
@@ -1,22 +1,24 @@
const { initialConsonants, medialVowels, finalConsonants } = require("./jamo");
const [initialConsonants, medialVowels, finalConsonants] = require("./jamo");
const _ = require("lodash");

describe("Jamo dictionary", () => {
test("should return zero value for lack of (null) batchim/final consonaunt", () => {
test("should return zero value for lack of (null) final consonant", () => {
expect(finalConsonants.indexOf(null)).toBe(0);
});

test("should return value for 27 of ㅎ as batchim/final consonaunt", () => {
expect(finalConsonants.indexOf("ㅎ")).toBe(27);
test("should return value for 27 of ㅎ as final consonant", () => {
expect(_.findIndex(finalConsonants, { jamo: "ㅎ" })).toBe(27);
});

test("should return value of zero for lack of ㅏ as medial vowel", () => {
expect(medialVowels.indexOf("ㅏ")).toBe(0);
expect(_.findIndex(medialVowels, { jamo: "ㅏ" })).toBe(0);
});

test("should return value of 20 for lack of ㅣ as medial vowel", () => {
expect(medialVowels.indexOf("ㅣ")).toBe(20);
expect(_.findIndex(medialVowels, { jamo: "ㅣ" })).toBe(20);
});

test("should return value of 18 for lack of ㅎ as initial consonaunt", () => {
expect(initialConsonants.indexOf("ㅎ")).toBe(18);
test("should return value of 18 for lack of ㅎ as initial consonant", () => {
expect(_.findIndex(initialConsonants, { jamo: "ㅎ" })).toBe(18);
});
});
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -9,8 +9,8 @@
},
"keywords": [
"alphabet",
"chosungul",
"chosun'gul",
"choson'gul",
"chosŏn'gŭl",
"조선글",
"hangeul",
"han-geul",
Expand Down Expand Up @@ -38,6 +38,7 @@
"license": "UNLICENSED",
"devDependencies": {
"jest": "^24.9.0",
"lodash": "^4.17.15",
"unicode": "^12.1.0"
},
"dependencies": {},
Expand Down

0 comments on commit 5365d52

Please sign in to comment.