Skip to content

convert v3.17.0+

Using convert to convert the format of pinyin.

Example

Different formats

Support numToSymbolsymbolToNumtoneNone 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 convert
  • options (optional): object, the configuration of conversion rules:
AttributeTypeDescriptionOptional ValuesDetailDefault Value
separatorstringthe separator specified-convert function will split pinyin with this separator for conversion' '
formatstringformat of conversionnumToSymbolconvert number to symbolnumToSymbol
SymbolToNumconvert symbol to number
toneNoneremove symbol in pinyin