-
-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move tools to package (#50)
- Loading branch information
Showing
44 changed files
with
4,405 additions
and
688 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { defineBuildConfig } from 'unbuild' | ||
|
||
export default defineBuildConfig({ | ||
entries: [ | ||
'src/index', | ||
], | ||
clean: true, | ||
declaration: true, | ||
rollup: { | ||
emitCJS: true, | ||
inlineDependencies: true, | ||
}, | ||
}) |
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,45 @@ | ||
{ | ||
"name": "@hankit/tools", | ||
"version": "0.0.1", | ||
"description": "Toolkits for Hanzi and Pinyin", | ||
"keywords": [ | ||
"hanzi", | ||
"chinese", | ||
"chinese-character", | ||
"pinyin" | ||
], | ||
"homepage": "https://github.com/antfu/handle/tree/main/packages/tools#readme", | ||
"bugs": { | ||
"url": "https://github.com/antfu/handle/issues" | ||
}, | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/antfu/handle.git", | ||
"directory": "packages/tools" | ||
}, | ||
"funding": "https://github.com/sponsors/antfu", | ||
"author": "Anthony Fu <[email protected]>", | ||
"sideEffects": false, | ||
"files": [ | ||
"dist" | ||
], | ||
"exports": { | ||
".": { | ||
"require": "./dist/index.cjs", | ||
"import": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts" | ||
} | ||
}, | ||
"main": "dist/index.cjs", | ||
"module": "dist/index.mjs", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"build": "unbuild", | ||
"stub": "unbuild --stub" | ||
}, | ||
"devDependencies": { | ||
"pinyin": "^2.11.0", | ||
"@types/pinyin": "^2.10.0" | ||
} | ||
} |
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,22 @@ | ||
import toSimplifiedMap from './map/toSimplified.json' | ||
import { reverseMap } from './utils' | ||
|
||
export function toSimplified<T extends string | number | undefined>(text: T): T { | ||
if (!text || typeof text !== 'string') | ||
return text | ||
// @ts-expect-error ignore | ||
// eslint-disable-next-line no-control-regex | ||
return text.replace(/[^\x00-\xFF]/g, s => ((s in toSimplifiedMap) ? toSimplifiedMap[s] : s)) | ||
} | ||
|
||
let toTraditionalMap: Record<string, string> | undefined | ||
|
||
export function toTraditional<T extends string | number | undefined>(text: T): T { | ||
if (!text || typeof text !== 'string') | ||
return text | ||
if (!toTraditionalMap) | ||
toTraditionalMap = reverseMap(toSimplifiedMap) | ||
// @ts-expect-error ignore | ||
// eslint-disable-next-line no-control-regex | ||
return text.replace(/[^\x00-\xFF]/g, s => ((s in toTraditionalMap) ? toTraditionalMap[s] : s)) | ||
} |
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,16 @@ | ||
export function filterNonChineseChars(input: string) { | ||
return Array.from(input) | ||
.filter(i => /\p{Script=Han}/u.test(i)) | ||
.join('') | ||
} | ||
|
||
if (import.meta.vitest) { | ||
const { it, expect } = import.meta.vitest | ||
it('filterNonChineseChars', () => { | ||
expect(filterNonChineseChars('Hello World!')).toBe('') | ||
expect(filterNonChineseChars('こんにちは')).toBe('') | ||
expect(filterNonChineseChars('안녕하세요')).toBe('') | ||
expect(filterNonChineseChars('你好啊')).toMatchInlineSnapshot('"你好啊"') | ||
expect(filterNonChineseChars('Hello,你好!')).toMatchInlineSnapshot('"你好"') | ||
}) | ||
} |
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 @@ | ||
export * from './filter' |
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,4 +1,6 @@ | ||
export * from './t2s' | ||
export * from './zhuyin' | ||
export * from './convert' | ||
export * from './types' | ||
export * from './pinyin' | ||
export * from './zhuyin' | ||
export * from './hanzi' | ||
export * from './shuangpin' |
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,29 @@ | ||
{ | ||
"ā": "a1", | ||
"á": "a2", | ||
"ǎ": "a3", | ||
"à": "a4", | ||
"ē": "e1", | ||
"é": "e2", | ||
"ě": "e3", | ||
"è": "e4", | ||
"ō": "o1", | ||
"ó": "o2", | ||
"ǒ": "o3", | ||
"ò": "o4", | ||
"ī": "i1", | ||
"í": "i2", | ||
"ǐ": "i3", | ||
"ì": "i4", | ||
"ū": "u1", | ||
"ú": "u2", | ||
"ǔ": "u3", | ||
"ù": "u4", | ||
"ü": "v0", | ||
"ǘ": "v2", | ||
"ǚ": "v3", | ||
"ǜ": "v4", | ||
"ń": "n2", | ||
"ň": "n3", | ||
"": "m2" | ||
} |
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,49 @@ | ||
{ | ||
"a": [ | ||
"ā", | ||
"á", | ||
"ǎ", | ||
"à" | ||
], | ||
"e": [ | ||
"ē", | ||
"é", | ||
"ě", | ||
"è" | ||
], | ||
"o": [ | ||
"ō", | ||
"ó", | ||
"ǒ", | ||
"ò" | ||
], | ||
"i": [ | ||
"ī", | ||
"í", | ||
"ǐ", | ||
"ì" | ||
], | ||
"u": [ | ||
"ū", | ||
"ú", | ||
"ǔ", | ||
"ù" | ||
], | ||
"v": [ | ||
"ü", | ||
"ǘ", | ||
"ǚ", | ||
"ǜ" | ||
], | ||
"ü": [ | ||
"ü", | ||
"ǘ", | ||
"ǚ", | ||
"ǜ" | ||
], | ||
"n": [ | ||
"n", | ||
"ń", | ||
"ň" | ||
] | ||
} |
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,44 @@ | ||
{ | ||
"initals": { | ||
"sh": "u", | ||
"ch": "i", | ||
"zh": "v", | ||
"": "o" | ||
}, | ||
"finals": { | ||
"a": "a", | ||
"ai": "l", | ||
"an": "j", | ||
"ang": "h", | ||
"ao": "k", | ||
"e": "e", | ||
"ei": "z", | ||
"en": "f", | ||
"eng": "g", | ||
"i": "i", | ||
"ia": "w", | ||
"ian": "m", | ||
"iang": "d", | ||
"iao": "c", | ||
"ie": "x", | ||
"iong": "s", | ||
"in": "n", | ||
"ing": ";", | ||
"iu": "q", | ||
"o": "o", | ||
"ong": "s", | ||
"ou": "b", | ||
"u": "u", | ||
"er": "r", | ||
"ua": "w", | ||
"uai": "y", | ||
"uan": "r", | ||
"uang": "d", | ||
"ue": "t", | ||
"ui": "v", | ||
"un": "p", | ||
"uo": "o", | ||
"v": "y", | ||
"ve": "t" | ||
} | ||
} |
Oops, something went wrong.