diff --git a/types/transliteration/index.d.ts b/types/transliteration/index.d.ts new file mode 100644 index 0000000000..6e74e8d1aa --- /dev/null +++ b/types/transliteration/index.d.ts @@ -0,0 +1,68 @@ +// Type definitions for transliteration 1.6 +// Project: https://github.com/andyhu/transliteration#readme +// Definitions by: Anthony Trinh +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export function transliterate(str: string, options?: transliterate.Options): string; + +export namespace transliterate { + /** + * Bind options globally so any following calls will be using + * optionsObj by default. If optionsObj argument is omitted, + * it will return current default option object. + */ + function config(optionsObj?: Options): Options; + + interface Options { + /** + * Unicode characters that are not in the database will be replaced with `unknown` + * default: "[?]" + */ + unknown?: string; + + /** + * Custom replacement of the strings before transliteration + */ + replace?: string[][] | {[source: string]: string}; + + /** + * Strings in the ignore list will be bypassed from transliteration + */ + ignore?: string[]; + } +} + +export function slugify(str: string, options?: slugify.Options): string; + +export namespace slugify { + /** + * Bind options globally so any following calls will be using + * optionsObj by default. If optionsObj argument is omitted, + * it will return current default option object. + */ + function config(optionsObj?: Options): Options; + + interface Options { + /** + * Whether to force slugs to be lowercased + * default: true + */ + lowercase?: boolean; + + /** + * Separator of the slug + * default: "-" + */ + separator?: string; + + /** + * Custom replacement of the strings before transliteration + */ + replace?: string[][] | {[source: string]: string}; + + /** + * Strings in the ignore list will be bypassed from transliteration + */ + ignore?: string[]; + } +} diff --git a/types/transliteration/transliteration-tests.ts b/types/transliteration/transliteration-tests.ts new file mode 100644 index 0000000000..fd9bef63c1 --- /dev/null +++ b/types/transliteration/transliteration-tests.ts @@ -0,0 +1,40 @@ +import { transliterate as tr, slugify } from 'transliteration'; + +tr('你好,世界'); // Ni Hao , Shi Jie +tr('Γεια σας, τον κόσμο'); // Geia sas, ton kosmo +tr('안녕하세요, 세계'); // annyeonghaseyo, segye +tr('你好,世界', { replace: {你: 'You'}, ignore: ['好'] }); // You 好, Shi Jie +tr('你好,世界', { replace: [['你', 'You']], ignore: ['好'] }); // You 好, Shi Jie (option in array form) +// or use configurations +tr.config({ replace: [['你', 'You']], ignore: ['好'] }); +tr('你好,世界'); // You 好, Shi Jie +// get configurations +const opt1 = tr.config(); +opt1.ignore = ['foo', 'bar', 'baz']; +opt1.ignore = undefined; +opt1.replace = [['a', 'foo'], ['b', 'bar']]; +opt1.replace = {a: 'foo', b: 'bar', c: 'baz'}; +opt1.replace = undefined; +opt1.unknown = '***?***'; +opt1.unknown = undefined; + +slugify('你好,世界'); // ni-hao-shi-jie +slugify('你好,世界', { lowercase: false, separator: '_' }); // Ni_Hao_Shi_Jie +slugify('你好,世界', { replace: {你好: 'Hello', 世界: 'world'}, separator: '_' }); // hello_world +slugify('你好,世界', { replace: [['你好', 'Hello'], ['世界', 'world']], separator: '_' }); // hello_world (option in array form) +slugify('你好,世界', { ignore: ['你好'] }); // 你好shi-jie +// or use configurations +slugify.config({ lowercase: false, separator: '_' }); +slugify('你好,世界'); // Ni_Hao_Shi_Jie +// get configurations +const opt2 = slugify.config(); +opt2.ignore = ['foo', 'bar', 'baz']; +opt2.ignore = undefined; +opt2.replace = [['a', 'foo'], ['b', 'bar']]; +opt2.replace = {a: 'foo', b: 'bar', c: 'baz'}; +opt2.replace = undefined; +opt2.lowercase = false; +opt2.lowercase = true; +opt2.lowercase = undefined; +opt2.separator = ';'; +opt2.separator = undefined; diff --git a/types/transliteration/tsconfig.json b/types/transliteration/tsconfig.json new file mode 100644 index 0000000000..152b73f04a --- /dev/null +++ b/types/transliteration/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es5" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "transliteration-tests.ts" + ] +} diff --git a/types/transliteration/tslint.json b/types/transliteration/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/transliteration/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }