-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82faa2c
commit 3b03ec6
Showing
11 changed files
with
156 additions
and
40 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
export * from './convert' | ||
|
||
export { | ||
customS2TPhrases, | ||
customT2SPhrases, | ||
} from './phrases' |
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,51 @@ | ||
import type { PhrasesMap } from './cache' | ||
import { simplifiedPhrasesMap, traditionalPhrasesMap } from './cache' | ||
|
||
export type PhrasesConfig = (readonly [string, string])[] | ||
|
||
/** | ||
* 添加 自定义 简体转繁体 短语集合 | ||
* @param config - 短语集合 [['简体短语', '繁体短语'], ...] | ||
* | ||
* @example | ||
* ```ts | ||
* customS2TPhrases([ | ||
* ['友谊万岁', '友誼萬歲'], | ||
* ['王后', '王后'], | ||
* ]) | ||
* ``` | ||
*/ | ||
export function customS2TPhrases(config: PhrasesConfig): void { | ||
config.forEach(([source, target]) => addPhrases(simplifiedPhrasesMap, source, target)) | ||
} | ||
|
||
/** | ||
* 添加 自定义 繁体转简体 短语集合 | ||
* @param config - 短语集合 [['繁体短语', '简体短语'], ...] | ||
* | ||
* @example | ||
* ```ts | ||
* customT2SPhrases([ | ||
* ['酒逢知己千鍾少', '酒逢知己千锺少'], | ||
* ['雖覆能復', '虽覆能复'], | ||
* ]) | ||
* ``` | ||
*/ | ||
export function customT2SPhrases(config: PhrasesConfig): void { | ||
config.forEach(([source, target]) => addPhrases(traditionalPhrasesMap, source, target)) | ||
} | ||
|
||
function addPhrases(map: PhrasesMap, source: string, target: string): void { | ||
const key = source[0] | ||
let tuple = map.get(key) | ||
!tuple && map.set(key, (tuple = [[], []])) | ||
|
||
tuple[0].push(source) | ||
tuple[1].push(target === '_' ? source : target) | ||
} | ||
|
||
export function transformPhrases(data: readonly [string, string], map: PhrasesMap): void { | ||
const sources = data[0].split(' ') | ||
const targets = data[1].split(' ') | ||
sources.forEach((source, index) => addPhrases(map, source, targets[index])) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { beforeEach, it } from 'vitest' | ||
import { toSimplified, toTraditional, customS2TPhrases, customT2SPhrases } from '../src/index' | ||
import { simplifiedPhrasesMap, traditionalPhrasesMap } from '../src/cache' | ||
import { afterEach } from 'node:test' | ||
|
||
beforeEach(() => { | ||
customS2TPhrases([ | ||
['双台子区', '雙臺子區'], | ||
]) | ||
customT2SPhrases([ | ||
['雖覆能復', '虽覆能复'] | ||
]) | ||
}) | ||
|
||
afterEach(() => { | ||
simplifiedPhrasesMap.clear() | ||
traditionalPhrasesMap.clear() | ||
}) | ||
|
||
it('custom simplified to traditional phrases', ({ expect }) => { | ||
expect(toTraditional('双台子区')).toBe('雙台子區') | ||
expect(toTraditional('双台子区', true)).toBe('雙臺子區') | ||
}) | ||
|
||
it('custom traditional to simplified phrases', ({ expect }) => { | ||
expect(toSimplified('雖覆能復')).toBe('虽复能复') | ||
expect(toSimplified('雖覆能復', true)).toBe('虽覆能复') | ||
}) |
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