@storyteller-platform/transliteration 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright http://github.com/yf-hk/transliteration
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ Software), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, andor sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,309 @@
1
+ <p align="center"><img src="https://yf-hk.github.io/transliteration/transliteration.png" alt="Transliteration"></p>
2
+
3
+ [![Build and Test](https://img.shields.io/github/actions/workflow/status/yf-hk/transliteration/build.yml?branch=main)](https://github.com/yf-hk/transliteration/actions/workflows/build.yml)
4
+ [![Coverage](https://codecov.io/gh/yf-hk/transliteration/graph/badge.svg)](https://codecov.io/gh/yf-hk/transliteration)
5
+ [![NPM Version](https://img.shields.io/npm/v/transliteration.svg)](https://www.npmjs.com/package/transliteration)
6
+ [![NPM Downloads](https://img.shields.io/npm/dm/transliteration.svg)](https://www.npmjs.com/package/transliteration)
7
+ [![jsDelivr](https://data.jsdelivr.com/v1/package/npm/transliteration/badge)](https://www.jsdelivr.com/package/npm/transliteration)
8
+ [![License](https://img.shields.io/npm/l/transliteration.svg)](https://github.com/yf-hk/transliteration/blob/main/LICENSE.txt)
9
+
10
+ # Transliteration
11
+
12
+ A universal Unicode to Latin transliteration and slug generation library. Designed for cross-platform compatibility with comprehensive language support.
13
+
14
+ **[Live Demo →](https://yf-hk.github.io/transliteration)**
15
+
16
+ ## Table of Contents
17
+
18
+ - [Features](#features)
19
+ - [Latin-Only Mode](#latin-only-mode)
20
+ - [Compatibility](#compatibility)
21
+ - [Installation](#installation)
22
+ - [Usage](#usage)
23
+ - [transliterate()](#transliteratestr-options)
24
+ - [slugify()](#slugifystr-options)
25
+ - [CLI](#cli-usage)
26
+ - [Known Issues](#known-issues)
27
+ - [License](#license)
28
+
29
+ ## Features
30
+
31
+ - **Unicode Transliteration** — Convert characters from any writing system to Latin equivalents
32
+ - **Slug Generation** — Create URL-safe and filename-safe strings from Unicode text
33
+ - **Highly Configurable** — Extensive options for custom replacements, ignored characters, and output formatting
34
+ - **Cross-Platform** — Runs seamlessly in Node.js, browsers, and command-line environments
35
+ - **TypeScript Ready** — Full type definitions included out of the box
36
+ - **Zero Dependencies** — Lightweight with no external runtime dependencies
37
+
38
+ ## Latin-Only Mode
39
+
40
+ For applications that only need to transliterate Latin-based scripts (Western European languages), you can use the lightweight Latin-only build for significantly smaller bundle size:
41
+
42
+ ```javascript
43
+ // Full build: ~186 KB (all scripts including CJK, Korean, etc.)
44
+ import { transliterate, slugify } from 'transliteration';
45
+
46
+ // Latin-only build: ~5 KB (Basic Latin + Latin Extended only)
47
+ import { transliterate, slugify } from 'transliteration/latin';
48
+ ```
49
+
50
+ The Latin-only build supports:
51
+
52
+ - Basic ASCII (U+0000-U+007F)
53
+ - Latin-1 Supplement (U+0080-U+00FF)
54
+ - Latin Extended-A (U+0100-U+017F)
55
+ - Latin Extended-B (U+0180-U+024F)
56
+
57
+ This is ideal for applications that only handle Western European languages and want to minimize bundle size.
58
+
59
+ ## Compatibility
60
+
61
+ - **Node.js**: v20.0.0 or higher
62
+ - **Browsers**: All modern browsers (Chrome, Firefox, Safari, Edge)
63
+ - **Mobile**: React Native
64
+ - **Other**: Web Workers, CLI
65
+
66
+ ## Installation
67
+
68
+ ### Node.js / React Native
69
+
70
+ ```bash
71
+ npm install transliteration
72
+ ```
73
+
74
+ > **TypeScript:** Type definitions are bundled with this package. Do not install `@types/transliteration`.
75
+
76
+ **Quick Start:**
77
+
78
+ ```javascript
79
+ import { transliterate as tr, slugify } from 'transliteration';
80
+
81
+ // Transliteration
82
+ tr('你好, world!'); // => 'Ni Hao , world!'
83
+
84
+ // Slugify
85
+ slugify('你好, world!'); // => 'ni-hao-world'
86
+ ```
87
+
88
+ ### Browser (CDN)
89
+
90
+ #### UMD Build (Global Variables)
91
+
92
+ ```html
93
+ <script src="https://cdn.jsdelivr.net/npm/transliteration@2/dist/browser/bundle.umd.min.js"></script>
94
+ <script>
95
+ // Available as global variables
96
+ transliterate('你好, World'); // => 'Ni Hao , World'
97
+ slugify('Hello, 世界'); // => 'hello-shi-jie'
98
+
99
+ // Legacy method (will be removed in next major version)
100
+ transl('Hola, mundo'); // => 'hola-mundo'
101
+ </script>
102
+ ```
103
+
104
+ #### ES Module
105
+
106
+ ```html
107
+ <script type="module">
108
+ import { transliterate } from 'https://cdn.jsdelivr.net/npm/transliteration@2/dist/browser/bundle.esm.min.js';
109
+ console.log(transliterate('你好')); // => 'Ni Hao'
110
+ </script>
111
+ ```
112
+
113
+ ### CLI
114
+
115
+ ```bash
116
+ # Global installation
117
+ npm install transliteration -g
118
+
119
+ # Basic usage
120
+ transliterate 你好 # => Ni Hao
121
+ slugify 你好 # => ni-hao
122
+
123
+ # Using stdin
124
+ echo 你好 | slugify -S # => ni-hao
125
+ ```
126
+
127
+ ## Usage
128
+
129
+ ### transliterate(str, [options])
130
+
131
+ Converts Unicode characters in the input string to their Latin equivalents. Unrecognized characters are replaced with the `unknown` placeholder or removed if no placeholder is specified.
132
+
133
+ #### Options
134
+
135
+ | Option | Type | Default | Description |
136
+ |--------|------|---------|-------------|
137
+ | `ignore` | `string[]` | `[]` | Strings to preserve without transliteration |
138
+ | `replace` | `object` or `array` | `{}` | Custom replacements applied before transliteration |
139
+ | `replaceAfter` | `object` or `array` | `{}` | Custom replacements applied after transliteration |
140
+ | `trim` | `boolean` | `false` | Trim leading and trailing whitespace from result |
141
+ | `unknown` | `string` | `''` | Replacement character for unrecognized Unicode |
142
+ | `fixChineseSpacing` | `boolean` | `true` | Insert spaces between transliterated Chinese characters |
143
+
144
+ #### Example
145
+
146
+ ```javascript
147
+ import { transliterate as tr } from 'transliteration';
148
+
149
+ // Basic usage
150
+ tr('你好,世界'); // => 'Ni Hao , Shi Jie'
151
+ tr('Γεια σας, τον κόσμο'); // => 'Geia sas, ton kosmo'
152
+ tr('안녕하세요, 세계'); // => 'annyeonghaseyo, segye'
153
+
154
+ // With options
155
+ tr('你好,世界', {
156
+ replace: { 你: 'You' },
157
+ ignore: ['好']
158
+ }); // => 'You 好, Shi Jie'
159
+
160
+ // Array form of replace option
161
+ tr('你好,世界', {
162
+ replace: [['你', 'You']],
163
+ ignore: ['好']
164
+ }); // => 'You 好, Shi Jie'
165
+ ```
166
+
167
+ ### transliterate.config([optionsObj], [reset = false])
168
+
169
+ Sets global default options for all subsequent `transliterate()` calls. When called without arguments, returns the current configuration. Pass `reset = true` to restore factory defaults.
170
+
171
+ ```javascript
172
+ // Set global configuration
173
+ tr.config({ replace: [['你', 'You']], ignore: ['好'] });
174
+
175
+ // All calls will use this configuration
176
+ tr('你好,世界'); // => 'You 好, Shi Jie'
177
+
178
+ // View current configuration
179
+ console.log(tr.config()); // => { replace: [['你', 'You']], ignore: ['好'] }
180
+
181
+ // Reset to defaults
182
+ tr.config(undefined, true);
183
+ console.log(tr.config()); // => {}
184
+ ```
185
+
186
+ ### slugify(str, [options])
187
+
188
+ Transforms Unicode text into a URL-safe and filename-safe slug string.
189
+
190
+ #### Options
191
+
192
+ | Option | Type | Default | Description |
193
+ |--------|------|---------|-------------|
194
+ | `ignore` | `string[]` | `[]` | Strings to preserve without transliteration |
195
+ | `replace` | `object` or `array` | `{}` | Custom replacements applied before transliteration |
196
+ | `replaceAfter` | `object` or `array` | `{}` | Custom replacements applied after transliteration |
197
+ | `trim` | `boolean` | `false` | Trim leading and trailing whitespace from result |
198
+ | `unknown` | `string` | `''` | Replacement character for unrecognized Unicode |
199
+ | `lowercase` | `boolean` | `true` | Convert output to lowercase |
200
+ | `uppercase` | `boolean` | `false` | Convert output to uppercase |
201
+ | `separator` | `string` | `-` | Word separator character |
202
+ | `allowedChars` | `string` | `a-zA-Z0-9-_.~'` | Regex character class for permitted characters |
203
+ | `fixChineseSpacing` | `boolean` | `true` | Insert spaces between transliterated Chinese characters |
204
+
205
+ #### Example
206
+
207
+ ```javascript
208
+ // Basic usage
209
+ slugify('你好,世界'); // => 'ni-hao-shi-jie'
210
+
211
+ // With options
212
+ slugify('你好,世界', {
213
+ lowercase: false,
214
+ separator: '_'
215
+ }); // => 'Ni_Hao_Shi_Jie'
216
+
217
+ // Using replace option
218
+ slugify('你好,世界', {
219
+ replace: {
220
+ 你好: 'Hello',
221
+ 世界: 'world'
222
+ },
223
+ separator: '_'
224
+ }); // => 'hello_world'
225
+
226
+ // Using ignore option
227
+ slugify('你好,世界', {
228
+ ignore: ['你好']
229
+ }); // => '你好shi-jie'
230
+ ```
231
+
232
+ ### slugify.config([optionsObj], [reset = false])
233
+
234
+ Sets global default options for all subsequent `slugify()` calls. When called without arguments, returns the current configuration. Pass `reset = true` to restore factory defaults.
235
+
236
+ ```javascript
237
+ // Set global configuration
238
+ slugify.config({ lowercase: false, separator: '_' });
239
+
240
+ // All calls will use this configuration
241
+ slugify('你好,世界'); // => 'Ni_Hao_Shi_Jie'
242
+
243
+ // View current configuration
244
+ console.log(slugify.config()); // => { lowercase: false, separator: "_" }
245
+
246
+ // Reset to defaults
247
+ slugify.config(undefined, true);
248
+ console.log(slugify.config()); // => {}
249
+ ```
250
+
251
+ ### CLI Usage
252
+
253
+ #### Transliterate Command
254
+
255
+ ```
256
+ transliterate <unicode> [options]
257
+
258
+ Options:
259
+ --version Show version number [boolean]
260
+ -u, --unknown Placeholder for unknown characters [string] [default: ""]
261
+ -r, --replace Custom string replacement [array] [default: []]
262
+ -i, --ignore String list to ignore [array] [default: []]
263
+ -S, --stdin Use stdin as input [boolean] [default: false]
264
+ -h, --help Show help information [boolean]
265
+
266
+ Examples:
267
+ transliterate "你好, world!" -r 好=good -r "world=Shi Jie"
268
+ transliterate "你好,世界!" -i 你好 -i ,
269
+ ```
270
+
271
+ #### Slugify Command
272
+
273
+ ```
274
+ slugify <unicode> [options]
275
+
276
+ Options:
277
+ --version Show version number [boolean]
278
+ -U, --unknown Placeholder for unknown characters [string] [default: ""]
279
+ -l, --lowercase Returns result in lowercase [boolean] [default: true]
280
+ -u, --uppercase Returns result in uppercase [boolean] [default: false]
281
+ -s, --separator Separator of the slug [string] [default: "-"]
282
+ -r, --replace Custom string replacement [array] [default: []]
283
+ -i, --ignore String list to ignore [array] [default: []]
284
+ -S, --stdin Use stdin as input [boolean] [default: false]
285
+ -h, --help Show help information [boolean]
286
+
287
+ Examples:
288
+ slugify "你好, world!" -r 好=good -r "world=Shi Jie"
289
+ slugify "你好,世界!" -i 你好 -i ,
290
+ ```
291
+
292
+ ## Known Issues
293
+
294
+ This library uses 1-to-1 Unicode code point mapping, which provides broad language coverage but has inherent limitations with context-dependent characters (e.g., polyphonic characters where pronunciation varies by context). Thorough testing with your target languages is recommended before production use.
295
+
296
+ **Language-Specific Limitations:**
297
+
298
+ | Language | Issue | Alternative |
299
+ |----------|-------|-------------|
300
+ | **Chinese** | Polyphonic characters may not transliterate correctly | [`pinyin`](https://www.npmjs.com/package/pinyin) |
301
+ | **Japanese** | Kanji characters often convert to Chinese Pinyin due to Unicode overlap | [`kuroshiro`](https://www.npmjs.com/package/kuroshiro) |
302
+ | **Thai** | Not working properly | [Issue #67](https://github.com/yf-hk/transliteration/issues/67) |
303
+ | **Cyrillic** | May be inaccurate for specific languages like Bulgarian | - |
304
+
305
+ For other issues or feature requests, please [open an issue](https://github.com/yf-hk/transliteration/issues).
306
+
307
+ ## License
308
+
309
+ MIT