diff --git a/.changeset/loud-ducks-compete.md b/.changeset/loud-ducks-compete.md new file mode 100644 index 0000000..50de998 --- /dev/null +++ b/.changeset/loud-ducks-compete.md @@ -0,0 +1,5 @@ +--- +'k-popo': patch +--- + +Shave a few more bytes off diff --git a/README.md b/README.md index 6aa9ba6..249b7ab 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ ko`${schedule}(이)여서 추가할 수 없어요. ${role}(이)가 필요합니 ### `ko: (template: TemplateStringsArray, ...words: (string | [string, string])[]) => string` -Resolves all Korean [_postposition tokens_](#available-postposition-tokens) that are placed directly after a placeholder. +Replaces all Korean [_postposition tokens_](#available-postposition-tokens) that are placed directly after a placeholder. ```js expect(ko`${'디자이너'}(으)로서 좌시할 수 없다.`).toBe('디자이너로서 좌시할 수 없다.') @@ -47,7 +47,7 @@ expect(ko`${`너(당신)`}(은)는 모른다.`).toBe(`너(당신)는 모른다.` expect(ko`${`당신(너)`}(은)는 모른다.`).toBe(`당신(너)은 모른다.`) ``` -`ko` can also resolve tokens against numbers: +`ko` can also replace tokens against numbers: ```js expect(ko`알고 계셨나요? ${'1'}(은)는 미지의 수입니다.`).toBe('알고 계셨나요? 1은 미지의 수입니다.') diff --git a/src/getLastChar.ts b/src/getLastChar.ts index a69d077..5f7fcb4 100644 --- a/src/getLastChar.ts +++ b/src/getLastChar.ts @@ -1,6 +1,4 @@ -const getLastChar = (str: string): string => { - let stripped = str.replace(/'|"|\([^)]*\)/g, '') - return stripped[stripped.length - 1] // no path where str is empty -} +// str is guaranteed to be non-empty so this won't throw +const getLastChar = (str: string): string => (str = str.replace(/'|"|\([^)]*\)/g, ''))[str.length - 1] export { getLastChar } diff --git a/src/ko.ts b/src/ko.ts index ef64e58..1895134 100644 --- a/src/ko.ts +++ b/src/ko.ts @@ -3,10 +3,13 @@ import { resolve } from './resolve' type Parameter = string | readonly [display: string, pronunciation: string] /** - * Resolves all postposition tokens right after a template slot to a fitting one. + * Replaces all postposition tokens right after a template slot to a proper one. * * `(은)는`, `(이)가`, `(을)를`, `(과)와`, `(으)로`, `(이)여`, `(이)`, and `(아)야` are available. * + * Note that `ko()` does not support decomposed (NFD) Hangul characters. + * Use [`${word.normalize('NFC')}`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize) if you need to. + * * @example * ko`그러자 ${name}(을)를 찾으며 말씀하시었으니, "${name}"(아)야, 어디 있느냐?` * // name=재민: 그러자 재민을 찾으며 말씀하시었으니, 재민아, 어디 있느냐? diff --git a/src/resolve.ts b/src/resolve.ts index 9f745c0..7106c4e 100644 --- a/src/resolve.ts +++ b/src/resolve.ts @@ -27,13 +27,16 @@ const foreignCharsRecord: Readonly> = { p: 1, } -const hasNoCoda = (char: string, ignoreRieul: boolean): boolean => { - let coda = char >= '가' && char <= '힣' ? (char.charCodeAt(0) - 0xac00) % 28 : foreignCharsRecord[char] || 0 - return !coda || (ignoreRieul && coda == 8) +const hasNoCoda = (char: string, ignore_ㄹ: boolean): boolean => { + let coda = char >= '가' && char <= '힣' ? (char.charCodeAt(0) - 16) % 28 : foreignCharsRecord[char] + return !coda || (ignore_ㄹ && coda == 8) } /** - * Resolve a postposition token into a fitting postposition, based on the word specified. + * Returns a proper postposition from given `tokenString` based on the `testString`. + * + * Note that `resolve()` does not support decomposed (NFD) Hangul characters. + * Use [`testString.normalize('NFC')`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize) if you need to. * * @param tokenString A string starting with one of supported Korean postposition tokens. `(은)는`, `(이)가`, `(을)를`, `(과)와`, `(으)로`, `(이)여`, `(이)`, and `(아)야` are available. * @param testString A string to resolve the token against.