mirror of
https://github.com/zh-lx/pinyin-pro.git
synced 2026-03-13 09:51:38 +08:00
feat: extract common types
This commit is contained in:
@@ -12,3 +12,57 @@ export type ToneType = "symbol" | "num" | "none";
|
||||
|
||||
export type PinyinMode = "normal" | "surname";
|
||||
export type SurnameMode = "all" | "head" | "off";
|
||||
|
||||
export type CommonOptions = {
|
||||
/**
|
||||
* @description 返回的拼音音调类型
|
||||
* @value symbol:在字母上加音调 (默认值)
|
||||
* @value num:以数字格式展示音调,并跟在拼音后面
|
||||
* @value none:不展示音调
|
||||
*/
|
||||
toneType?: "symbol" | "num" | "none";
|
||||
/**
|
||||
* @description 返回的拼音格式类型
|
||||
* @value pinyin:返回完整拼音 (默认值)
|
||||
* @value initial:返回声母
|
||||
* @value final:返回韵母
|
||||
* @value num:返回音调对应的数字
|
||||
* @value first:返回首字母
|
||||
* @value finalHead:返回韵头(介音)
|
||||
* @value finalBody:返回韵腹
|
||||
* @value finalTail:返回韵尾
|
||||
*/
|
||||
pattern?:
|
||||
| "pinyin"
|
||||
| "initial"
|
||||
| "final"
|
||||
| "num"
|
||||
| "first"
|
||||
| "finalHead"
|
||||
| "finalBody"
|
||||
| "finalTail";
|
||||
/**
|
||||
* @description 是否移除非汉字字符(推荐使用 removeNonZh: removed 代替)
|
||||
* @value false:返回结果保留非汉字字符 (默认值)
|
||||
* @value true:返回结果移除非汉字字符
|
||||
*/
|
||||
removeNonZh?: boolean;
|
||||
/**
|
||||
* @description 非汉字字符的间距格式
|
||||
* @value spaced:连续非汉字字符之间用空格隔开 (默认值)
|
||||
* @value consecutive:连续非汉字字符无间距
|
||||
* @value removed:返回结果移除非汉字字符
|
||||
*/
|
||||
nonZh?: "spaced" | "consecutive" | "removed";
|
||||
/**
|
||||
* @description nonZh 生效范围的正则表达式
|
||||
*/
|
||||
nonZhScope?: RegExp;
|
||||
/**
|
||||
* @description 对于 ü 的返回是否转换成 v(仅在 toneType: none 启用时生效)
|
||||
* @value false:返回值中保留 ü (默认值)
|
||||
* @value true:返回值中 ü 转换成 v
|
||||
* @value string:返回值中 ü 转换成指定字符
|
||||
*/
|
||||
v?: boolean | string;
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { pinyin } from '@/core/pinyin';
|
||||
import { pinyin } from "@/core/pinyin";
|
||||
|
||||
interface HtmlOptions {
|
||||
/**
|
||||
@@ -24,7 +24,7 @@ interface HtmlOptions {
|
||||
/**
|
||||
* @description 拼音上是否标注音调
|
||||
*/
|
||||
toneType?: 'symbol' | 'num' | 'none';
|
||||
toneType?: "symbol" | "num" | "none";
|
||||
/**
|
||||
* @description 对于指定的汉字及字符,在 result 上额外补充的拼音
|
||||
*/
|
||||
@@ -43,18 +43,26 @@ interface HtmlOptions {
|
||||
* @value false:移除 <rp>(</rp>
|
||||
*/
|
||||
rp?: boolean;
|
||||
/**
|
||||
* @description 对于 ü 的返回是否转换成 v(仅在 toneType: none 启用时生效)
|
||||
* @value false:返回值中保留 ü (默认值)
|
||||
* @value true:返回值中 ü 转换成 v
|
||||
* @value string:返回值中 ü 转换成指定字符
|
||||
*/
|
||||
v?: boolean | string;
|
||||
}
|
||||
|
||||
const DefaultHtmlOptions: HtmlOptions = {
|
||||
resultClass: 'py-result-item',
|
||||
chineseClass: 'py-chinese-item',
|
||||
pinyinClass: 'py-pinyin-item',
|
||||
nonChineseClass: 'py-non-chinese-item',
|
||||
resultClass: "py-result-item",
|
||||
chineseClass: "py-chinese-item",
|
||||
pinyinClass: "py-pinyin-item",
|
||||
nonChineseClass: "py-non-chinese-item",
|
||||
wrapNonChinese: false,
|
||||
toneType: 'symbol',
|
||||
toneType: "symbol",
|
||||
customClassMap: {},
|
||||
toneSandhi: true,
|
||||
rp: true,
|
||||
v: false,
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -69,12 +77,13 @@ export const html = (text: string, options?: HtmlOptions) => {
|
||||
...(options || {}),
|
||||
} as Required<HtmlOptions>;
|
||||
const pinyinArray = pinyin(text, {
|
||||
type: 'all',
|
||||
type: "all",
|
||||
toneType: completeOptions.toneType,
|
||||
toneSandhi: options?.toneSandhi,
|
||||
v: completeOptions.v,
|
||||
});
|
||||
const result = pinyinArray.map((item) => {
|
||||
let additionalClass = '';
|
||||
let additionalClass = "";
|
||||
for (const classname in completeOptions.customClassMap) {
|
||||
const dict = completeOptions.customClassMap[classname];
|
||||
if (dict.includes(item.origin)) {
|
||||
@@ -83,19 +92,29 @@ export const html = (text: string, options?: HtmlOptions) => {
|
||||
}
|
||||
if (item.isZh) {
|
||||
// 汉字字符处理
|
||||
const resultClass = completeOptions.resultClass || DefaultHtmlOptions.resultClass;
|
||||
const chineseClass = completeOptions.chineseClass || DefaultHtmlOptions.chineseClass;
|
||||
const pinyinClass = completeOptions.pinyinClass || DefaultHtmlOptions.pinyinClass;
|
||||
return `<span class="${resultClass}${additionalClass}"><ruby><span class="${chineseClass}">${item.origin}</span>${completeOptions.rp ? '<rp>(</rp>' : ''}<rt class="${pinyinClass}">${item.pinyin}</rt>${completeOptions.rp ? '<rp>)</rp>' : ''}</ruby></span>`;
|
||||
const resultClass =
|
||||
completeOptions.resultClass || DefaultHtmlOptions.resultClass;
|
||||
const chineseClass =
|
||||
completeOptions.chineseClass || DefaultHtmlOptions.chineseClass;
|
||||
const pinyinClass =
|
||||
completeOptions.pinyinClass || DefaultHtmlOptions.pinyinClass;
|
||||
return `<span class="${resultClass}${additionalClass}"><ruby><span class="${chineseClass}">${
|
||||
item.origin
|
||||
}</span>${
|
||||
completeOptions.rp ? "<rp>(</rp>" : ""
|
||||
}<rt class="${pinyinClass}">${item.pinyin}</rt>${
|
||||
completeOptions.rp ? "<rp>)</rp>" : ""
|
||||
}</ruby></span>`;
|
||||
} else {
|
||||
// 非汉字字符处理
|
||||
if (completeOptions.wrapNonChinese) {
|
||||
const nonChineseClass = completeOptions.nonChineseClass || DefaultHtmlOptions.nonChineseClass;
|
||||
const nonChineseClass =
|
||||
completeOptions.nonChineseClass || DefaultHtmlOptions.nonChineseClass;
|
||||
return `<span class="${nonChineseClass}${additionalClass}">${item.origin}</span>`;
|
||||
} else {
|
||||
return item.origin;
|
||||
}
|
||||
}
|
||||
});
|
||||
return result.join('');
|
||||
return result.join("");
|
||||
};
|
||||
|
||||
@@ -4,6 +4,7 @@ import type {
|
||||
SingleWordResult,
|
||||
PinyinMode,
|
||||
SurnameMode,
|
||||
CommonOptions,
|
||||
} from "../../common/type";
|
||||
import { getPinyin } from "./handle";
|
||||
import {
|
||||
@@ -17,34 +18,7 @@ import {
|
||||
middlewareToneSandhi,
|
||||
} from "./middlewares";
|
||||
|
||||
export interface BasicOptions {
|
||||
/**
|
||||
* @description 返回的拼音音调类型
|
||||
* @value symbol:在字母上加音调 (默认值)
|
||||
* @value num:以数字格式展示音调,并跟在拼音后面
|
||||
* @value none:不展示音调
|
||||
*/
|
||||
toneType?: "symbol" | "num" | "none";
|
||||
/**
|
||||
* @description 返回的拼音格式类型
|
||||
* @value pinyin:返回完整拼音 (默认值)
|
||||
* @value initial:返回声母
|
||||
* @value final:返回韵母
|
||||
* @value num:返回音调对应的数字
|
||||
* @value first:返回首字母
|
||||
* @value finalHead:返回韵头(介音)
|
||||
* @value finalBody:返回韵腹
|
||||
* @value finalTail:返回韵尾
|
||||
*/
|
||||
pattern?:
|
||||
| "pinyin"
|
||||
| "initial"
|
||||
| "final"
|
||||
| "num"
|
||||
| "first"
|
||||
| "finalHead"
|
||||
| "finalBody"
|
||||
| "finalTail";
|
||||
export interface BasicOptions extends CommonOptions {
|
||||
/**
|
||||
* @description 是否返回单个汉字的所有多音,仅针对输入的 word 为单个汉字生效
|
||||
* @value false:返回最常用的一个拼音 (默认值)
|
||||
@@ -64,30 +38,6 @@ export interface BasicOptions {
|
||||
* @value head:作用于汉语字符串开头
|
||||
*/
|
||||
surname?: SurnameMode;
|
||||
/**
|
||||
* @description 是否移除非汉字字符(推荐使用 removeNonZh: removed 代替)
|
||||
* @value false:返回结果保留非汉字字符 (默认值)
|
||||
* @value true:返回结果移除非汉字字符
|
||||
*/
|
||||
removeNonZh?: boolean;
|
||||
/**
|
||||
* @description 非汉字字符的间距格式
|
||||
* @value spaced:连续非汉字字符之间用空格隔开 (默认值)
|
||||
* @value consecutive:连续非汉字字符无间距
|
||||
* @value removed:返回结果移除非汉字字符
|
||||
*/
|
||||
nonZh?: "spaced" | "consecutive" | "removed";
|
||||
/**
|
||||
* @description nonZh 生效范围的正则表达式
|
||||
*/
|
||||
nonZhScope?: RegExp;
|
||||
/**
|
||||
* @description 对于 ü 的返回是否转换成 v(仅在 toneType: none 启用时生效)
|
||||
* @value false:返回值中保留 ü (默认值)
|
||||
* @value true:返回值中 ü 转换成 v
|
||||
* @value string:返回值中 ü 转换成指定字符
|
||||
*/
|
||||
v?: boolean | string;
|
||||
/**
|
||||
* @description 是否开启「一」和 「不」字的变调。默认开启。参考:https://zh.wiktionary.org/wiki/Appendix:%E2%80%9C%E4%B8%80%E2%80%9D%E5%8F%8A%E2%80%9C%E4%B8%8D%E2%80%9D%E7%9A%84%E5%8F%98%E8%B0%83
|
||||
* @value true:开启
|
||||
|
||||
@@ -1,64 +1,22 @@
|
||||
import type { SingleWordResult } from '../../common/type';
|
||||
import type { SingleWordResult, CommonOptions } from "../../common/type";
|
||||
import {
|
||||
validateType,
|
||||
middleWareNonZh,
|
||||
middlewarePattern,
|
||||
middlewareToneType,
|
||||
middlewareV,
|
||||
} from '@/core/pinyin/middlewares';
|
||||
import DICT1 from '@/data/dict1';
|
||||
} from "@/core/pinyin/middlewares";
|
||||
import DICT1 from "@/data/dict1";
|
||||
import {
|
||||
getNumOfTone,
|
||||
getInitialAndFinal,
|
||||
getFirstLetter,
|
||||
getFinalParts,
|
||||
} from '@/core/pinyin/handle';
|
||||
import { getCustomPolyphonicDict } from '../custom';
|
||||
import { splitString } from '@/common/utils';
|
||||
|
||||
interface BasicOptions {
|
||||
/**
|
||||
* @description 返回的拼音音调类型
|
||||
* @value symbol:在字母上加音调 (默认值)
|
||||
* @value num:以数字格式展示音调,并跟在拼音后面
|
||||
* @value none:不展示音调
|
||||
*/
|
||||
toneType?: 'symbol' | 'num' | 'none';
|
||||
/**
|
||||
* @description 返回的拼音格式类型
|
||||
* @value pinyin:返回完整拼音 (默认值)
|
||||
* @value initial:返回声母
|
||||
* @value final:返回韵母
|
||||
* @value num:返回音调对应的数字
|
||||
* @value first:返回首字母
|
||||
* @value finalHead:返回韵头(介音)
|
||||
* @value finalBody:返回韵腹
|
||||
* @value finalTail:返回韵尾
|
||||
*/
|
||||
pattern?:
|
||||
| 'pinyin'
|
||||
| 'initial'
|
||||
| 'final'
|
||||
| 'num'
|
||||
| 'first'
|
||||
| 'finalHead'
|
||||
| 'finalBody'
|
||||
| 'finalTail';
|
||||
/**
|
||||
* @description 对于 ü 的返回是否转换成 v(仅在 toneType: none 启用时生效)
|
||||
* @value false:返回值中保留 ü (默认值)
|
||||
* @value true:返回值中 ü 转换成 v
|
||||
*/
|
||||
v?: boolean;
|
||||
/**
|
||||
* @description 非汉字字符的间距格式
|
||||
* @value spaced:连续非汉字字符之间用空格隔开 (默认值)
|
||||
* @value consecutive:连续非汉字字符无间距
|
||||
* @value removed:返回结果移除非汉字字符
|
||||
*/
|
||||
nonZh?: 'spaced' | 'consecutive' | 'removed';
|
||||
}
|
||||
} from "@/core/pinyin/handle";
|
||||
import { getCustomPolyphonicDict } from "../custom";
|
||||
import { splitString } from "@/common/utils";
|
||||
|
||||
interface BasicOptions extends CommonOptions {}
|
||||
interface AllData {
|
||||
origin: string;
|
||||
pinyin: string;
|
||||
@@ -80,7 +38,7 @@ interface OptionsReturnString extends BasicOptions {
|
||||
* @value array:以数组格式返回
|
||||
* @value array: 返回全部信息数组
|
||||
*/
|
||||
type?: 'string';
|
||||
type?: "string";
|
||||
}
|
||||
|
||||
interface OptionsReturnArray extends BasicOptions {
|
||||
@@ -90,7 +48,7 @@ interface OptionsReturnArray extends BasicOptions {
|
||||
* @value array:以数组格式返回
|
||||
* @value array: 返回全部信息数组
|
||||
*/
|
||||
type: 'array';
|
||||
type: "array";
|
||||
}
|
||||
|
||||
interface OptionsReturnAll extends BasicOptions {
|
||||
@@ -100,7 +58,7 @@ interface OptionsReturnAll extends BasicOptions {
|
||||
* @value array:以数组格式返回
|
||||
* @value array: 返回全部信息数组
|
||||
*/
|
||||
type: 'all';
|
||||
type: "all";
|
||||
}
|
||||
|
||||
export interface CompleteOptions extends BasicOptions {
|
||||
@@ -110,15 +68,15 @@ export interface CompleteOptions extends BasicOptions {
|
||||
* @value array:以数组格式返回
|
||||
* @value array: 返回全部信息数组
|
||||
*/
|
||||
type?: 'string' | 'array' | 'all';
|
||||
type?: "string" | "array" | "all";
|
||||
}
|
||||
|
||||
const DEFAULT_OPTIONS: CompleteOptions = {
|
||||
pattern: 'pinyin',
|
||||
toneType: 'symbol',
|
||||
type: 'string',
|
||||
pattern: "pinyin",
|
||||
toneType: "symbol",
|
||||
type: "string",
|
||||
v: false,
|
||||
nonZh: 'spaced',
|
||||
nonZh: "spaced",
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -162,16 +120,20 @@ function polyphonic(
|
||||
}
|
||||
|
||||
// 传入空字符串
|
||||
if (text === '') {
|
||||
if (text === "") {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (options.type === 'all') {
|
||||
options.pattern = 'pinyin';
|
||||
if (options.type === "all") {
|
||||
options.pattern = "pinyin";
|
||||
}
|
||||
|
||||
if (options.pattern === 'num') {
|
||||
options.toneType = 'none';
|
||||
if (options.pattern === "num") {
|
||||
options.toneType = "none";
|
||||
}
|
||||
|
||||
if (options.removeNonZh) {
|
||||
options.nonZh = "removed";
|
||||
}
|
||||
|
||||
let list = getPolyphonicList(text);
|
||||
@@ -221,7 +183,7 @@ const getSplittedPolyphonicList = (
|
||||
): SingleWordResult[][] => {
|
||||
return list.map((item) => {
|
||||
return item.isZh
|
||||
? item.result.split(' ').map((pinyin) => ({
|
||||
? item.result.split(" ").map((pinyin) => ({
|
||||
origin: item.origin,
|
||||
result: pinyin,
|
||||
isZh: true,
|
||||
@@ -236,12 +198,12 @@ export const handleType = (
|
||||
list: SingleWordResult[],
|
||||
options: CompleteOptions
|
||||
) => {
|
||||
if (options.type === 'array') {
|
||||
if (options.type === "array") {
|
||||
return Array.from(new Set(list.map((item) => item.result)));
|
||||
}
|
||||
if (options.type === 'all') {
|
||||
if (options.type === "all") {
|
||||
return list.map((item) => {
|
||||
const pinyin = item.isZh ? item.result : '';
|
||||
const pinyin = item.isZh ? item.result : "";
|
||||
const { initial, final } = getInitialAndFinal(pinyin);
|
||||
const { head, body, tail } = getFinalParts(pinyin);
|
||||
return {
|
||||
@@ -259,7 +221,7 @@ export const handleType = (
|
||||
};
|
||||
});
|
||||
}
|
||||
return Array.from(new Set(list.map((item) => item.result))).join(' ');
|
||||
return Array.from(new Set(list.map((item) => item.result))).join(" ");
|
||||
};
|
||||
|
||||
export { polyphonic };
|
||||
|
||||
@@ -1,249 +1,254 @@
|
||||
import { polyphonic } from '../lib/index';
|
||||
import { expect, describe, it } from 'vitest';
|
||||
import { polyphonic } from "../lib/index";
|
||||
import { expect, describe, it } from "vitest";
|
||||
|
||||
describe('polyphonic', () => {
|
||||
it('[polyphonic]normal', () => {
|
||||
const result = polyphonic('好好学习');
|
||||
expect(result).to.deep.equal(['hǎo hào', 'hǎo hào', 'xué', 'xí']);
|
||||
describe("polyphonic", () => {
|
||||
it("[polyphonic]normal", () => {
|
||||
const result = polyphonic("好好学习");
|
||||
expect(result).to.deep.equal(["hǎo hào", "hǎo hào", "xué", "xí"]);
|
||||
});
|
||||
|
||||
it('[polyphonic]array', () => {
|
||||
const result = polyphonic('好好学习', { type: 'array' });
|
||||
it("[polyphonic]array", () => {
|
||||
const result = polyphonic("好好学习", { type: "array" });
|
||||
expect(result).to.deep.equal([
|
||||
['hǎo', 'hào'],
|
||||
['hǎo', 'hào'],
|
||||
['xué'],
|
||||
['xí'],
|
||||
["hǎo", "hào"],
|
||||
["hǎo", "hào"],
|
||||
["xué"],
|
||||
["xí"],
|
||||
]);
|
||||
});
|
||||
|
||||
it('[polyphonic]all', () => {
|
||||
const result = polyphonic('好好学习', { type: 'all' });
|
||||
it("[polyphonic]all", () => {
|
||||
const result = polyphonic("好好学习", { type: "all" });
|
||||
expect(result).to.deep.equal([
|
||||
[
|
||||
{
|
||||
final: 'ǎo',
|
||||
finalBody: 'ǎ',
|
||||
finalHead: '',
|
||||
finalTail: 'o',
|
||||
first: 'h',
|
||||
initial: 'h',
|
||||
final: "ǎo",
|
||||
finalBody: "ǎ",
|
||||
finalHead: "",
|
||||
finalTail: "o",
|
||||
first: "h",
|
||||
initial: "h",
|
||||
isZh: true,
|
||||
num: 3,
|
||||
origin: '好',
|
||||
pinyin: 'hǎo',
|
||||
origin: "好",
|
||||
pinyin: "hǎo",
|
||||
inZhRange: true,
|
||||
},
|
||||
{
|
||||
final: 'ào',
|
||||
finalBody: 'à',
|
||||
finalHead: '',
|
||||
finalTail: 'o',
|
||||
first: 'h',
|
||||
initial: 'h',
|
||||
final: "ào",
|
||||
finalBody: "à",
|
||||
finalHead: "",
|
||||
finalTail: "o",
|
||||
first: "h",
|
||||
initial: "h",
|
||||
isZh: true,
|
||||
num: 4,
|
||||
origin: '好',
|
||||
pinyin: 'hào',
|
||||
origin: "好",
|
||||
pinyin: "hào",
|
||||
inZhRange: true,
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
final: 'ǎo',
|
||||
finalBody: 'ǎ',
|
||||
finalHead: '',
|
||||
finalTail: 'o',
|
||||
first: 'h',
|
||||
initial: 'h',
|
||||
final: "ǎo",
|
||||
finalBody: "ǎ",
|
||||
finalHead: "",
|
||||
finalTail: "o",
|
||||
first: "h",
|
||||
initial: "h",
|
||||
isZh: true,
|
||||
num: 3,
|
||||
origin: '好',
|
||||
pinyin: 'hǎo',
|
||||
origin: "好",
|
||||
pinyin: "hǎo",
|
||||
inZhRange: true,
|
||||
},
|
||||
{
|
||||
final: 'ào',
|
||||
finalBody: 'à',
|
||||
finalHead: '',
|
||||
finalTail: 'o',
|
||||
first: 'h',
|
||||
initial: 'h',
|
||||
final: "ào",
|
||||
finalBody: "à",
|
||||
finalHead: "",
|
||||
finalTail: "o",
|
||||
first: "h",
|
||||
initial: "h",
|
||||
isZh: true,
|
||||
num: 4,
|
||||
origin: '好',
|
||||
pinyin: 'hào',
|
||||
origin: "好",
|
||||
pinyin: "hào",
|
||||
inZhRange: true,
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
final: 'üé',
|
||||
finalBody: 'é',
|
||||
finalHead: 'ü',
|
||||
finalTail: '',
|
||||
first: 'x',
|
||||
initial: 'x',
|
||||
final: "üé",
|
||||
finalBody: "é",
|
||||
finalHead: "ü",
|
||||
finalTail: "",
|
||||
first: "x",
|
||||
initial: "x",
|
||||
isZh: true,
|
||||
num: 2,
|
||||
origin: '学',
|
||||
pinyin: 'xué',
|
||||
origin: "学",
|
||||
pinyin: "xué",
|
||||
inZhRange: true,
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
final: 'í',
|
||||
finalBody: 'í',
|
||||
finalHead: '',
|
||||
finalTail: '',
|
||||
first: 'x',
|
||||
initial: 'x',
|
||||
final: "í",
|
||||
finalBody: "í",
|
||||
finalHead: "",
|
||||
finalTail: "",
|
||||
first: "x",
|
||||
initial: "x",
|
||||
isZh: true,
|
||||
num: 2,
|
||||
origin: '习',
|
||||
pinyin: 'xí',
|
||||
origin: "习",
|
||||
pinyin: "xí",
|
||||
inZhRange: true,
|
||||
},
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
it('[polyphonic]type error', () => {
|
||||
it("[polyphonic]type error", () => {
|
||||
// @ts-ignore
|
||||
const result = polyphonic(11);
|
||||
expect(result).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it('[polyphonic]empty', () => {
|
||||
const result = polyphonic('');
|
||||
it("[polyphonic]empty", () => {
|
||||
const result = polyphonic("");
|
||||
expect(result).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it('[polyphonic]nonzh', () => {
|
||||
const result = polyphonic('好好学习s');
|
||||
expect(result).to.deep.equal(['hǎo hào', 'hǎo hào', 'xué', 'xí', 's']);
|
||||
it("[polyphonic]nonzh", () => {
|
||||
const result = polyphonic("好好学习s");
|
||||
expect(result).to.deep.equal(["hǎo hào", "hǎo hào", "xué", "xí", "s"]);
|
||||
});
|
||||
|
||||
it('[polyphonic]all&nonZh', () => {
|
||||
const result = polyphonic('好好学习s', { type: 'all' });
|
||||
it("[polyphonic]removeNonZh", () => {
|
||||
const result = polyphonic("好好学习s", { removeNonZh: true });
|
||||
expect(result).to.deep.equal(["hǎo hào", "hǎo hào", "xué", "xí"]);
|
||||
});
|
||||
|
||||
it("[polyphonic]all&nonZh", () => {
|
||||
const result = polyphonic("好好学习s", { type: "all" });
|
||||
expect(result).to.deep.equal([
|
||||
[
|
||||
{
|
||||
final: 'ǎo',
|
||||
finalBody: 'ǎ',
|
||||
finalHead: '',
|
||||
finalTail: 'o',
|
||||
first: 'h',
|
||||
initial: 'h',
|
||||
final: "ǎo",
|
||||
finalBody: "ǎ",
|
||||
finalHead: "",
|
||||
finalTail: "o",
|
||||
first: "h",
|
||||
initial: "h",
|
||||
isZh: true,
|
||||
num: 3,
|
||||
origin: '好',
|
||||
pinyin: 'hǎo',
|
||||
origin: "好",
|
||||
pinyin: "hǎo",
|
||||
inZhRange: true,
|
||||
},
|
||||
{
|
||||
final: 'ào',
|
||||
finalBody: 'à',
|
||||
finalHead: '',
|
||||
finalTail: 'o',
|
||||
first: 'h',
|
||||
initial: 'h',
|
||||
final: "ào",
|
||||
finalBody: "à",
|
||||
finalHead: "",
|
||||
finalTail: "o",
|
||||
first: "h",
|
||||
initial: "h",
|
||||
isZh: true,
|
||||
num: 4,
|
||||
origin: '好',
|
||||
pinyin: 'hào',
|
||||
origin: "好",
|
||||
pinyin: "hào",
|
||||
inZhRange: true,
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
final: 'ǎo',
|
||||
finalBody: 'ǎ',
|
||||
finalHead: '',
|
||||
finalTail: 'o',
|
||||
first: 'h',
|
||||
initial: 'h',
|
||||
final: "ǎo",
|
||||
finalBody: "ǎ",
|
||||
finalHead: "",
|
||||
finalTail: "o",
|
||||
first: "h",
|
||||
initial: "h",
|
||||
isZh: true,
|
||||
num: 3,
|
||||
origin: '好',
|
||||
pinyin: 'hǎo',
|
||||
origin: "好",
|
||||
pinyin: "hǎo",
|
||||
inZhRange: true,
|
||||
},
|
||||
{
|
||||
final: 'ào',
|
||||
finalBody: 'à',
|
||||
finalHead: '',
|
||||
finalTail: 'o',
|
||||
first: 'h',
|
||||
initial: 'h',
|
||||
final: "ào",
|
||||
finalBody: "à",
|
||||
finalHead: "",
|
||||
finalTail: "o",
|
||||
first: "h",
|
||||
initial: "h",
|
||||
isZh: true,
|
||||
num: 4,
|
||||
origin: '好',
|
||||
pinyin: 'hào',
|
||||
origin: "好",
|
||||
pinyin: "hào",
|
||||
inZhRange: true,
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
final: 'üé',
|
||||
finalBody: 'é',
|
||||
finalHead: 'ü',
|
||||
finalTail: '',
|
||||
first: 'x',
|
||||
initial: 'x',
|
||||
final: "üé",
|
||||
finalBody: "é",
|
||||
finalHead: "ü",
|
||||
finalTail: "",
|
||||
first: "x",
|
||||
initial: "x",
|
||||
isZh: true,
|
||||
num: 2,
|
||||
origin: '学',
|
||||
pinyin: 'xué',
|
||||
origin: "学",
|
||||
pinyin: "xué",
|
||||
inZhRange: true,
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
final: 'í',
|
||||
finalBody: 'í',
|
||||
finalHead: '',
|
||||
finalTail: '',
|
||||
first: 'x',
|
||||
initial: 'x',
|
||||
final: "í",
|
||||
finalBody: "í",
|
||||
finalHead: "",
|
||||
finalTail: "",
|
||||
first: "x",
|
||||
initial: "x",
|
||||
isZh: true,
|
||||
num: 2,
|
||||
origin: '习',
|
||||
pinyin: 'xí',
|
||||
origin: "习",
|
||||
pinyin: "xí",
|
||||
inZhRange: true,
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
final: '',
|
||||
finalBody: '',
|
||||
finalHead: '',
|
||||
finalTail: '',
|
||||
first: 's',
|
||||
initial: '',
|
||||
final: "",
|
||||
finalBody: "",
|
||||
finalHead: "",
|
||||
finalTail: "",
|
||||
first: "s",
|
||||
initial: "",
|
||||
isZh: false,
|
||||
num: 0,
|
||||
origin: 's',
|
||||
pinyin: '',
|
||||
origin: "s",
|
||||
pinyin: "",
|
||||
inZhRange: false,
|
||||
},
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
it('[polyphonic]num', () => {
|
||||
const result = polyphonic('好好学习', { pattern: 'num' });
|
||||
expect(result).to.deep.equal(['3 4', '3 4', '2', '2']);
|
||||
it("[polyphonic]num", () => {
|
||||
const result = polyphonic("好好学习", { pattern: "num" });
|
||||
expect(result).to.deep.equal(["3 4", "3 4", "2", "2"]);
|
||||
});
|
||||
|
||||
it('[polyphonic]toneType', () => {
|
||||
const result = polyphonic('好好学习', { toneType: 'none' });
|
||||
expect(result).to.deep.equal(['hao', 'hao', 'xue', 'xi']);
|
||||
it("[polyphonic]toneType", () => {
|
||||
const result = polyphonic("好好学习", { toneType: "none" });
|
||||
expect(result).to.deep.equal(["hao", "hao", "xue", "xi"]);
|
||||
});
|
||||
|
||||
it('[polyphonic]toneType&num', () => {
|
||||
const result = polyphonic('好好学习', { toneType: 'num' });
|
||||
expect(result).to.deep.equal(['hao3 hao4', 'hao3 hao4', 'xue2', 'xi2']);
|
||||
it("[polyphonic]toneType&num", () => {
|
||||
const result = polyphonic("好好学习", { toneType: "num" });
|
||||
expect(result).to.deep.equal(["hao3 hao4", "hao3 hao4", "xue2", "xi2"]);
|
||||
});
|
||||
});
|
||||
|
||||
45
types/common/type.d.ts
vendored
45
types/common/type.d.ts
vendored
@@ -8,3 +8,48 @@ export interface SingleWordResult {
|
||||
export type ToneType = "symbol" | "num" | "none";
|
||||
export type PinyinMode = "normal" | "surname";
|
||||
export type SurnameMode = "all" | "head" | "off";
|
||||
export type CommonOptions = {
|
||||
/**
|
||||
* @description 返回的拼音音调类型
|
||||
* @value symbol:在字母上加音调 (默认值)
|
||||
* @value num:以数字格式展示音调,并跟在拼音后面
|
||||
* @value none:不展示音调
|
||||
*/
|
||||
toneType?: "symbol" | "num" | "none";
|
||||
/**
|
||||
* @description 返回的拼音格式类型
|
||||
* @value pinyin:返回完整拼音 (默认值)
|
||||
* @value initial:返回声母
|
||||
* @value final:返回韵母
|
||||
* @value num:返回音调对应的数字
|
||||
* @value first:返回首字母
|
||||
* @value finalHead:返回韵头(介音)
|
||||
* @value finalBody:返回韵腹
|
||||
* @value finalTail:返回韵尾
|
||||
*/
|
||||
pattern?: "pinyin" | "initial" | "final" | "num" | "first" | "finalHead" | "finalBody" | "finalTail";
|
||||
/**
|
||||
* @description 是否移除非汉字字符(推荐使用 removeNonZh: removed 代替)
|
||||
* @value false:返回结果保留非汉字字符 (默认值)
|
||||
* @value true:返回结果移除非汉字字符
|
||||
*/
|
||||
removeNonZh?: boolean;
|
||||
/**
|
||||
* @description 非汉字字符的间距格式
|
||||
* @value spaced:连续非汉字字符之间用空格隔开 (默认值)
|
||||
* @value consecutive:连续非汉字字符无间距
|
||||
* @value removed:返回结果移除非汉字字符
|
||||
*/
|
||||
nonZh?: "spaced" | "consecutive" | "removed";
|
||||
/**
|
||||
* @description nonZh 生效范围的正则表达式
|
||||
*/
|
||||
nonZhScope?: RegExp;
|
||||
/**
|
||||
* @description 对于 ü 的返回是否转换成 v(仅在 toneType: none 启用时生效)
|
||||
* @value false:返回值中保留 ü (默认值)
|
||||
* @value true:返回值中 ü 转换成 v
|
||||
* @value string:返回值中 ü 转换成指定字符
|
||||
*/
|
||||
v?: boolean | string;
|
||||
};
|
||||
|
||||
9
types/core/html/index.d.ts
vendored
9
types/core/html/index.d.ts
vendored
@@ -22,7 +22,7 @@ interface HtmlOptions {
|
||||
/**
|
||||
* @description 拼音上是否标注音调
|
||||
*/
|
||||
toneType?: 'symbol' | 'num' | 'none';
|
||||
toneType?: "symbol" | "num" | "none";
|
||||
/**
|
||||
* @description 对于指定的汉字及字符,在 result 上额外补充的拼音
|
||||
*/
|
||||
@@ -41,6 +41,13 @@ interface HtmlOptions {
|
||||
* @value false:移除 <rp>(</rp>
|
||||
*/
|
||||
rp?: boolean;
|
||||
/**
|
||||
* @description 对于 ü 的返回是否转换成 v(仅在 toneType: none 启用时生效)
|
||||
* @value false:返回值中保留 ü (默认值)
|
||||
* @value true:返回值中 ü 转换成 v
|
||||
* @value string:返回值中 ü 转换成指定字符
|
||||
*/
|
||||
v?: boolean | string;
|
||||
}
|
||||
/**
|
||||
* @description: 获取带拼音汉字的 html 字符串
|
||||
|
||||
47
types/core/pinyin/index.d.ts
vendored
47
types/core/pinyin/index.d.ts
vendored
@@ -1,25 +1,6 @@
|
||||
import { TokenizationAlgorithm } from "../../common/segmentit";
|
||||
import type { PinyinMode, SurnameMode } from "../../common/type";
|
||||
export interface BasicOptions {
|
||||
/**
|
||||
* @description 返回的拼音音调类型
|
||||
* @value symbol:在字母上加音调 (默认值)
|
||||
* @value num:以数字格式展示音调,并跟在拼音后面
|
||||
* @value none:不展示音调
|
||||
*/
|
||||
toneType?: "symbol" | "num" | "none";
|
||||
/**
|
||||
* @description 返回的拼音格式类型
|
||||
* @value pinyin:返回完整拼音 (默认值)
|
||||
* @value initial:返回声母
|
||||
* @value final:返回韵母
|
||||
* @value num:返回音调对应的数字
|
||||
* @value first:返回首字母
|
||||
* @value finalHead:返回韵头(介音)
|
||||
* @value finalBody:返回韵腹
|
||||
* @value finalTail:返回韵尾
|
||||
*/
|
||||
pattern?: "pinyin" | "initial" | "final" | "num" | "first" | "finalHead" | "finalBody" | "finalTail";
|
||||
import type { PinyinMode, SurnameMode, CommonOptions } from "../../common/type";
|
||||
export interface BasicOptions extends CommonOptions {
|
||||
/**
|
||||
* @description 是否返回单个汉字的所有多音,仅针对输入的 word 为单个汉字生效
|
||||
* @value false:返回最常用的一个拼音 (默认值)
|
||||
@@ -39,30 +20,6 @@ export interface BasicOptions {
|
||||
* @value head:作用于汉语字符串开头
|
||||
*/
|
||||
surname?: SurnameMode;
|
||||
/**
|
||||
* @description 是否移除非汉字字符(推荐使用 removeNonZh: removed 代替)
|
||||
* @value false:返回结果保留非汉字字符 (默认值)
|
||||
* @value true:返回结果移除非汉字字符
|
||||
*/
|
||||
removeNonZh?: boolean;
|
||||
/**
|
||||
* @description 非汉字字符的间距格式
|
||||
* @value spaced:连续非汉字字符之间用空格隔开 (默认值)
|
||||
* @value consecutive:连续非汉字字符无间距
|
||||
* @value removed:返回结果移除非汉字字符
|
||||
*/
|
||||
nonZh?: "spaced" | "consecutive" | "removed";
|
||||
/**
|
||||
* @description nonZh 生效范围的正则表达式
|
||||
*/
|
||||
nonZhScope?: RegExp;
|
||||
/**
|
||||
* @description 对于 ü 的返回是否转换成 v(仅在 toneType: none 启用时生效)
|
||||
* @value false:返回值中保留 ü (默认值)
|
||||
* @value true:返回值中 ü 转换成 v
|
||||
* @value string:返回值中 ü 转换成指定字符
|
||||
*/
|
||||
v?: boolean | string;
|
||||
/**
|
||||
* @description 是否开启「一」和 「不」字的变调。默认开启。参考:https://zh.wiktionary.org/wiki/Appendix:%E2%80%9C%E4%B8%80%E2%80%9D%E5%8F%8A%E2%80%9C%E4%B8%8D%E2%80%9D%E7%9A%84%E5%8F%98%E8%B0%83
|
||||
* @value true:开启
|
||||
|
||||
44
types/core/polyphonic/index.d.ts
vendored
44
types/core/polyphonic/index.d.ts
vendored
@@ -1,37 +1,5 @@
|
||||
import type { SingleWordResult } from '../../common/type';
|
||||
interface BasicOptions {
|
||||
/**
|
||||
* @description 返回的拼音音调类型
|
||||
* @value symbol:在字母上加音调 (默认值)
|
||||
* @value num:以数字格式展示音调,并跟在拼音后面
|
||||
* @value none:不展示音调
|
||||
*/
|
||||
toneType?: 'symbol' | 'num' | 'none';
|
||||
/**
|
||||
* @description 返回的拼音格式类型
|
||||
* @value pinyin:返回完整拼音 (默认值)
|
||||
* @value initial:返回声母
|
||||
* @value final:返回韵母
|
||||
* @value num:返回音调对应的数字
|
||||
* @value first:返回首字母
|
||||
* @value finalHead:返回韵头(介音)
|
||||
* @value finalBody:返回韵腹
|
||||
* @value finalTail:返回韵尾
|
||||
*/
|
||||
pattern?: 'pinyin' | 'initial' | 'final' | 'num' | 'first' | 'finalHead' | 'finalBody' | 'finalTail';
|
||||
/**
|
||||
* @description 对于 ü 的返回是否转换成 v(仅在 toneType: none 启用时生效)
|
||||
* @value false:返回值中保留 ü (默认值)
|
||||
* @value true:返回值中 ü 转换成 v
|
||||
*/
|
||||
v?: boolean;
|
||||
/**
|
||||
* @description 非汉字字符的间距格式
|
||||
* @value spaced:连续非汉字字符之间用空格隔开 (默认值)
|
||||
* @value consecutive:连续非汉字字符无间距
|
||||
* @value removed:返回结果移除非汉字字符
|
||||
*/
|
||||
nonZh?: 'spaced' | 'consecutive' | 'removed';
|
||||
import type { SingleWordResult, CommonOptions } from "../../common/type";
|
||||
interface BasicOptions extends CommonOptions {
|
||||
}
|
||||
interface AllData {
|
||||
origin: string;
|
||||
@@ -53,7 +21,7 @@ interface OptionsReturnString extends BasicOptions {
|
||||
* @value array:以数组格式返回
|
||||
* @value array: 返回全部信息数组
|
||||
*/
|
||||
type?: 'string';
|
||||
type?: "string";
|
||||
}
|
||||
interface OptionsReturnArray extends BasicOptions {
|
||||
/**
|
||||
@@ -62,7 +30,7 @@ interface OptionsReturnArray extends BasicOptions {
|
||||
* @value array:以数组格式返回
|
||||
* @value array: 返回全部信息数组
|
||||
*/
|
||||
type: 'array';
|
||||
type: "array";
|
||||
}
|
||||
interface OptionsReturnAll extends BasicOptions {
|
||||
/**
|
||||
@@ -71,7 +39,7 @@ interface OptionsReturnAll extends BasicOptions {
|
||||
* @value array:以数组格式返回
|
||||
* @value array: 返回全部信息数组
|
||||
*/
|
||||
type: 'all';
|
||||
type: "all";
|
||||
}
|
||||
export interface CompleteOptions extends BasicOptions {
|
||||
/**
|
||||
@@ -80,7 +48,7 @@ export interface CompleteOptions extends BasicOptions {
|
||||
* @value array:以数组格式返回
|
||||
* @value array: 返回全部信息数组
|
||||
*/
|
||||
type?: 'string' | 'array' | 'all';
|
||||
type?: "string" | "array" | "all";
|
||||
}
|
||||
/**
|
||||
* @description: 获取每个汉字的所有读音
|
||||
|
||||
Reference in New Issue
Block a user