UNPKG

3.66 kBTypeScriptView Raw
1import { Charmap } from '../../data/charmap';
2export declare type OptionReplaceArrayItem = [string | RegExp, string];
3export declare type OptionReplaceArray = OptionReplaceArrayItem[];
4export interface OptionReplaceObject {
5 [from: string]: string;
6}
7export declare type OptionReplaceCombined = OptionReplaceArray | OptionReplaceObject;
8export interface OptionsTransliterate {
9 /**
10 * Ignore a list of strings untouched
11 * @example tr('你好,世界', { ignore: ['你'] }) // 你 Hao , Shi Jie
12 */
13 ignore?: string[];
14 /**
15 * Replace a list of string / regex in the source string into the provided target string before transliteration
16 * The option can either be an array or an object
17 * @example tr('你好,世界', { replace: {你: 'You'} }) // You Hao , Shi Jie
18 * @example tr('你好,世界', { replace: [['你', 'You']] }) // You Hao , Shi Jie
19 * @example tr('你好,世界', { replace: [[/你/g, 'You']] }) // You Hao , Shi Jie
20 */
21 replace?: OptionReplaceCombined;
22 /**
23 * Same as `replace` but after transliteration
24 */
25 replaceAfter?: OptionReplaceCombined;
26 /**
27 * Decides whether or not to trim the result string after transliteration
28 * @default false
29 */
30 trim?: boolean;
31 /**
32 * Any characters not known by this library will be replaced by a specific string `unknown`
33 * @default ''
34 */
35 unknown?: string;
36 /**
37 * Fix Chinese spacing. For example, `你好` is transliterated to `Ni Hao` instead of `NiHao`. If you don't need to transliterate Chinese characters, set it to false to false to improve performance.
38 * @default true
39 */
40 fixChineseSpacing?: boolean;
41}
42export interface OptionsSlugify extends OptionsTransliterate {
43 /**
44 * Whether the result need to be converted into lowercase
45 * @default true
46 */
47 lowercase?: boolean;
48 /**
49 * Whether the result need to be converted into uppercase
50 * @default false
51 */
52 uppercase?: boolean;
53 /**
54 * Custom separator string
55 * @default '-'
56 */
57 separator?: string;
58 /**
59 * Allowed characters.
60 * When `allowedChars` is set to `'abc'`, then only characters match `/[abc]/g` will be preserved.
61 * Other characters will all be converted to `separator`
62 * @default 'a-zA-Z0-9-_.~''
63 */
64 allowedChars?: string;
65 /**
66 * Fix Chinese spacing. For example, `你好` is transliterated to `Ni Hao` instead of `NiHao`. If you don't need to transliterate Chinese characters, set it to false to false to improve performance.
67 */
68 fixChineseSpacing?: boolean;
69}
70export declare type Options = OptionsTransliterate | OptionsSlugify;
71export declare type IntervalArray = [number, number][];
72interface TransliterationFunction<T> {
73 (source: string, options?: T): string;
74 /**
75 * Set default config
76 * @param options
77 * @param reset
78 */
79 config: (options?: T, reset?: boolean) => T;
80 /**
81 * Set charmap data
82 * @param data
83 * @param reset
84 * @memberof Transliterate
85 */
86 setData: (data?: Charmap, reset?: boolean) => Charmap;
87 /**
88 * Used by browser
89 */
90 noConflict?: () => TransliterationFunction<T>;
91}
92export declare type TransliterateFunction = TransliterationFunction<OptionsTransliterate>;
93export declare type SlugifyFunction = TransliterationFunction<OptionsSlugify>;
94export interface BrowserGlobalObject {
95 transl: TransliterateFunction;
96 transliterate: TransliterateFunction;
97 slugify: SlugifyFunction;
98}
99export {};