Skip to content

Commit

Permalink
Shave a few more bytes off
Browse files Browse the repository at this point in the history
  • Loading branch information
alattalatta committed Aug 25, 2024
1 parent a356c15 commit 4dd1812
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-ducks-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'k-popo': patch
---

Shave a few more bytes off
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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('디자이너로서 좌시할 수 없다.')
Expand All @@ -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은 미지의 수입니다.')
Expand Down
6 changes: 2 additions & 4 deletions src/getLastChar.ts
Original file line number Diff line number Diff line change
@@ -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 }
5 changes: 4 additions & 1 deletion src/ko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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=재민: 그러자 재민을 찾으며 말씀하시었으니, 재민아, 어디 있느냐?
Expand Down
11 changes: 7 additions & 4 deletions src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ const foreignCharsRecord: Readonly<Record<string, number>> = {
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.
Expand Down

0 comments on commit 4dd1812

Please sign in to comment.