Skip to content

Commit

Permalink
refactor: move tools to package (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Mar 7, 2022
1 parent 1ba5f2c commit 2447c5e
Show file tree
Hide file tree
Showing 44 changed files with 4,405 additions and 688 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"picocolors": "^1.0.0",
"pnpm": "^6.32.3",
"typescript": "^4.6.2",
"unbuild": "^0.6.9",
"unocss": "^0.27.2",
"unplugin-auto-import": "^0.6.1",
"unplugin-vue-components": "^0.17.21",
Expand Down
13 changes: 13 additions & 0 deletions packages/tools/build.config.ts
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,
},
})
45 changes: 45 additions & 0 deletions packages/tools/package.json
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"
}
}
22 changes: 22 additions & 0 deletions packages/tools/src/convert.ts
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))
}
16 changes: 16 additions & 0 deletions packages/tools/src/hanzi/filter.ts
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('"你好"')
})
}
1 change: 1 addition & 0 deletions packages/tools/src/hanzi/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './filter'
6 changes: 4 additions & 2 deletions src/logic/lang/index.ts → packages/tools/src/index.ts
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'
29 changes: 29 additions & 0 deletions packages/tools/src/map/phonetics.json
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"
}
49 changes: 49 additions & 0 deletions packages/tools/src/map/toPhonetics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"a": [
"ā",
"á",
"ǎ",
"à"
],
"e": [
"ē",
"é",
"ě",
"è"
],
"o": [
"ō",
"ó",
"ǒ",
"ò"
],
"i": [
"ī",
"í",
"ǐ",
"ì"
],
"u": [
"ū",
"ú",
"ǔ",
"ù"
],
"v": [
"ü",
"ǘ",
"ǚ",
"ǜ"
],
"ü": [
"ü",
"ǘ",
"ǚ",
"ǜ"
],
"n": [
"n",
"ń",
"ň"
]
}
44 changes: 44 additions & 0 deletions packages/tools/src/map/toShuangpin.json
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"
}
}
Loading

0 comments on commit 2447c5e

Please sign in to comment.