convert v3.17.0+
Using convert
to convert the format of pinyin.
Example
Different formats
Support numToSymbol
、symbolToNum
、toneNone
and so on:
js
import { convert } from 'pinyin-pro';
// numToSymbol
convert('pin1 yin1'); // 'pīn yīn'
// symbolToNum
convert('pīn yīn', { format: 'symbolToNum' }); // 'pin1 yin1'
// toneNone
convert('pīn yīn', { format: 'toneNone' }); // 'pin yin'
convert('pin1 yin1', { format: 'toneNone' }); // 'pin yin'
import { convert } from 'pinyin-pro';
// numToSymbol
convert('pin1 yin1'); // 'pīn yīn'
// symbolToNum
convert('pīn yīn', { format: 'symbolToNum' }); // 'pin1 yin1'
// toneNone
convert('pīn yīn', { format: 'toneNone' }); // 'pin yin'
convert('pin1 yin1', { format: 'toneNone' }); // 'pin yin'
Array
Supports input in array format, and returns array format after conversion:
js
import { convert } from 'pinyin-pro';
convert(['pin1', 'yin1']); // ['pīn', 'yīn']
import { convert } from 'pinyin-pro';
convert(['pin1', 'yin1']); // ['pīn', 'yīn']
Specify separator
When the input is in string
format, the separator can be specified through the separator
parameter:
js
import { convert } from 'pinyin-pro';
convert('pin1-yin1', { separator: '-' }); // 'pīn-yīn'
import { convert } from 'pinyin-pro';
convert('pin1-yin1', { separator: '-' }); // 'pīn-yīn'
API
Function
js
import { convert } from 'pinyin-pro';
interface ConvertOptions {
/**
* @description specify the separator
*/
separator?: string;
/**
* @description format of conversation, default value is numToSymbol
* @example numToSymbol: pin yin -> pīn yīn
* @example symbolToNum: pīn yīn -> pin yin
* @example toneNone: pīn yīn -> pin yin
*/
format?: ConvertFormat;
}
function convert(pinyin: string | string[], options?: ConvertOptions): string | string[];
import { convert } from 'pinyin-pro';
interface ConvertOptions {
/**
* @description specify the separator
*/
separator?: string;
/**
* @description format of conversation, default value is numToSymbol
* @example numToSymbol: pin yin -> pīn yīn
* @example symbolToNum: pīn yīn -> pin yin
* @example toneNone: pīn yīn -> pin yin
*/
format?: ConvertFormat;
}
function convert(pinyin: string | string[], options?: ConvertOptions): string | string[];
Parameters
pinyin
(required):string | string[], the pinyin string or array of pinyin strings to convertoptions
(optional): object, the configuration of conversion rules:
Attribute | Type | Description | Optional Values | Detail | Default Value |
---|---|---|---|---|---|
separator | string | the separator specified | - | convert function will split pinyin with this separator for conversion | ' ' |
format | string | format of conversion | numToSymbol | convert number to symbol | numToSymbol |
SymbolToNum | convert symbol to number | ||||
toneNone | remove symbol in pinyin |