Skip to content

Commit

Permalink
Update, move isHangul function
Browse files Browse the repository at this point in the history
  • Loading branch information
mooniker committed Sep 29, 2019
1 parent 35a8206 commit 40a0ed6
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 76 deletions.
9 changes: 8 additions & 1 deletion isHangul.js → hangul/isHangul.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
const unicodeBlocks = require("./hangul/unicode/blocks");
const unicodeBlocks = require("./unicode/blocks");
const hangulBlocks = Object.entries(unicodeBlocks).filter(([blockName]) =>
blockName.startsWith("HANGUL")
);

/**
* Check whether a provided character belongs to a Hangul Unicode block
*
* Returns null if input is not a string.
*
* @param {*} char
*/
const isHangul = char => {
if (typeof char !== "string") {
return null;
Expand Down
90 changes: 90 additions & 0 deletions hangul/isHangul.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const isHangul = require("./isHangul");
const {
BASIC_LATIN,
CJK_UNIFIED_IDEOGRAPHS,
CJK_SYMBOLS_AND_PUNCTUATION,
HANGUL_SYLLABLES,
HANGUL_JAMO,
HANGUL_COMPATIBILITY_JAMO,
HANGUL_JAMO_EXTENDED_A,
HANGUL_JAMO_EXTENDED_B
} = require("./unicode/blocks");

const describeTestBlock = (description, jamoBlock, expected) =>
describe(description, () => {
const [start, stop] = jamoBlock;
for (let charCode = start; charCode <= stop; charCode += 1) {
const char = String.fromCodePoint(charCode);
test(`should determine ${char} is not hangul`, () => {
if (expected) {
expect(isHangul(char)).toBeTruthy();
} else {
expect(isHangul(char)).toBe(false);
}
});
}
});

describe("isHangul", () => {
describe("should return null", () => {
test("for an undefined input", () => {
expect(isHangul()).toBeNull();
});

test("for a null input", () => {
expect(isHangul(null)).toBeNull();
});

test("for NaN input", () => {
expect(isHangul(NaN)).toBeNull();
});

test("for an object input", () => {
expect(isHangul({})).toBeNull();
});

test("for a number input", () => {
expect(isHangul(1945)).toBeNull();
});
});

test("should return truthy for ㄱ", () => {
expect(isHangul("ㄱ")).toBeTruthy();
});

test("should return truthy for ㅏ", () => {
expect(isHangul("ㅏ")).toBeTruthy();
});

test("should return truthy for 가", () => {
expect(isHangul("가")).toBeTruthy();
});

describeTestBlock("basic Latin", BASIC_LATIN, false);

describeTestBlock(
"Chinese/Japanese/Korean unified ideographs",
CJK_UNIFIED_IDEOGRAPHS,
false
);

describeTestBlock(
"Chinese/Japanese/Korean symbols and punctuation",
CJK_SYMBOLS_AND_PUNCTUATION,
false
);

describeTestBlock("Hangul syllables", HANGUL_SYLLABLES, true);

describeTestBlock("Hangul jamo", HANGUL_JAMO, true);

describeTestBlock(
"Hangul compatibility jamo",
HANGUL_COMPATIBILITY_JAMO,
true
);

describeTestBlock("Hangul jamo extended-A", HANGUL_JAMO_EXTENDED_A, true);

describeTestBlock("Hangul jamo extended-B", HANGUL_JAMO_EXTENDED_B, true);
});
75 changes: 0 additions & 75 deletions isHangul.test.js

This file was deleted.

0 comments on commit 40a0ed6

Please sign in to comment.