Skip to content

Commit

Permalink
add comments and a test for kata2hira
Browse files Browse the repository at this point in the history
  • Loading branch information
boarwell committed Mar 24, 2022
1 parent 40de8ed commit a22b3b2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
20 changes: 19 additions & 1 deletion dist/main.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hira2kata, hira2kataAll } from './main.js';
import { hira2kata, hira2kataAll, kata2hira } from './main.js';
const test_hira2kata = () => {
const cases = [
{ input: 'あ', expected: 'ア' },
Expand Down Expand Up @@ -31,8 +31,26 @@ const test_hira2kataAll = () => {
console.error(`input: ${input}, expected ${expected}, but got ${hira2kata(input)}`);
}
};
const test_kata2hira = () => {
const cases = [
{ input: 'ア', expected: 'あ' },
{ input: 'ヲ', expected: 'を' },
{ input: 'コ', expected: 'こ' },
{ input: 'Ú', expected: 'z' },
{ input: '慻', expected: '愛' }
];
const failedCases = cases.filter(({ input, expected }) => kata2hira(input) !== expected);
if (failedCases.length <= 0) {
return;
}
console.error('[Failed]: kata2hira()');
for (const { input, expected } of failedCases) {
console.error(`input: ${input}, expected ${expected}, but got ${hira2kata(input)}`);
}
};
const main = () => {
test_hira2kata();
test_hira2kataAll();
test_kata2hira();
};
main();
28 changes: 27 additions & 1 deletion src/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hira2kata, hira2kataAll } from './main.js';
import { hira2kata, hira2kataAll, kata2hira } from './main.js';

type Case = {
input: string;
Expand Down Expand Up @@ -53,9 +53,35 @@ const test_hira2kataAll = () => {
}
};

const test_kata2hira = () => {
const cases: Case[] = [
{ input: 'ア', expected: 'あ' },
{ input: 'ヲ', expected: 'を' },
{ input: 'コ', expected: 'こ' },
{ input: 'Ú', expected: 'z' },
{ input: '慻', expected: '愛' }
];

const failedCases = cases.filter(
({ input, expected }) => kata2hira(input) !== expected
);

if (failedCases.length <= 0) {
return;
}

console.error('[Failed]: kata2hira()');
for (const { input, expected } of failedCases) {
console.error(
`input: ${input}, expected ${expected}, but got ${hira2kata(input)}`
);
}
};

const main = () => {
test_hira2kata();
test_hira2kataAll();
test_kata2hira();
};

main();
21 changes: 20 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const BETWEEN_KATAKANA_HIRAGANA = 96;
/**
* converts the first character of the argument to a Katakana (if it is Hiragana).
*
* if the argument is not Hiragana, it would return unexpected value.
* if the character is not Hiragana, it would return unexpected value.
* to avoid this, use isHiragana().
*
* @example
Expand Down Expand Up @@ -38,6 +38,25 @@ export const hira2kata = (c: string): string =>
export const hira2kataAll = (s: string): string =>
[...s].map(c => (isHiragana(c) ? hira2kata(c) : c)).join('');

/**
* converts the first character of the argument to a Hiragana (if it is Katakana).
*
* if the character is not Katakana, it would return unexpected value.
* to avoid this, use isKatakana().
*
* @example
* // returns 'あ'
* kata2hira('ア')
*
* @example
* // returns '受'
* hira2kata('吷')
*
* @example
* const c = '受';
* // returns '受'
* isKatakana(c) ? kata2hira(c) : c;
*/
export const kata2hira = (c: string): string =>
String.fromCodePoint(c.codePointAt(0)! - BETWEEN_KATAKANA_HIRAGANA);

Expand Down

0 comments on commit a22b3b2

Please sign in to comment.