Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consolidate commonly used Hangul Unicode helpers
- Loading branch information
Showing
5 changed files
with
111 additions
and
48 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,56 @@ | ||
const { SBase, NCount, TCount, SCount } = require("./hangul.constraints"); | ||
|
||
/** | ||
* Returns an integer division quotient (rounded down) | ||
* | ||
* @param {number} dividend | ||
* @param {number} divisor | ||
*/ | ||
const intDiv = (dividend, divisor) => Math.floor(dividend / divisor); | ||
|
||
/** | ||
* | ||
* @param {(string|integer)} s | ||
*/ | ||
function computeSIndex(s) { | ||
const SIndex = (typeof s === "string" ? s.charCodeAt(0) : s) - SBase; | ||
|
||
if (0 > SIndex || SIndex >= SCount) { | ||
throw new Error(`Not a Hangul syllable: ${s}`); | ||
} | ||
|
||
return SIndex; | ||
} | ||
|
||
/** | ||
* | ||
* @param {integer} SIndex | ||
*/ | ||
const computeLIndex = SIndex => intDiv(SIndex, NCount); // integer division rounded down | ||
|
||
/** | ||
* | ||
* @param {integer} SIndex | ||
*/ | ||
const computeVIndex = SIndex => intDiv(SIndex % NCount, TCount); | ||
|
||
/** | ||
* | ||
* @param {integer} SIndex | ||
*/ | ||
const computeTIndex = SIndex => SIndex % TCount; | ||
|
||
/** | ||
* | ||
* @param {integer} SIndex | ||
*/ | ||
const computeLVIndex = SIndex => (SIndex / TCount) * TCount; | ||
|
||
module.exports = { | ||
intDiv, | ||
computeSIndex, | ||
computeLIndex, | ||
computeVIndex, | ||
computeTIndex, | ||
computeLVIndex | ||
}; |
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,28 @@ | ||
const { | ||
intDiv, | ||
computeSIndex, | ||
computeVIndex, | ||
computeLIndex | ||
} = require("./hangul.computations"); | ||
|
||
describe("Integer division function", () => { | ||
test("should divide 5 by 2 and round the quotient down to the nearest integer", () => { | ||
expect(intDiv(5, 2)).toBe(2); | ||
}); | ||
|
||
test("should divide 4 by 2 and arrive at an integer quotient (no rounding needed)", () => { | ||
expect(intDiv(4, 2)).toBe(2); | ||
}); | ||
}); | ||
|
||
describe("computeSIndex function", () => { | ||
test.todo("should be tested"); | ||
}); | ||
|
||
describe("computeVIndex function", () => { | ||
test.todo("should be tested"); | ||
}); | ||
|
||
describe("computeLIndex function", () => { | ||
test.todo("should be tested"); | ||
}); |
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
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
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