source: imaps-frontend/node_modules/graphemer/README.md

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 5.9 KB
Line 
1# Graphemer: Unicode Character Splitter 🪓
2
3## Introduction
4
5This library continues the work of [Grapheme Splitter](https://github.com/orling/grapheme-splitter) and supports the following unicode versions:
6
7- Unicode 15 and below `[v1.4.0]`
8- Unicode 14 and below `[v1.3.0]`
9- Unicode 13 and below `[v1.1.0]`
10- Unicode 11 and below `[v1.0.0]` (Unicode 10 supported by `grapheme-splitter`)
11
12In JavaScript there is not always a one-to-one relationship between string characters and what a user would call a separate visual "letter". Some symbols are represented by several characters. This can cause issues when splitting strings and inadvertently cutting a multi-char letter in half, or when you need the actual number of letters in a string.
13
14For example, emoji characters like "🌷","🎁","💩","😜" and "👍" are represented by two JavaScript characters each (high surrogate and low surrogate). That is,
15
16```javascript
17'🌷'.length == 2;
18```
19
20The combined emoji are even longer:
21
22```javascript
23'🏳️‍🌈'.length == 6;
24```
25
26What's more, some languages often include combining marks - characters that are used to modify the letters before them. Common examples are the German letter ü and the Spanish letter ñ. Sometimes they can be represented alternatively both as a single character and as a letter + combining mark, with both forms equally valid:
27
28```javascript
29var two = 'ñ'; // unnormalized two-char n+◌̃, i.e. "\u006E\u0303";
30var one = 'ñ'; // normalized single-char, i.e. "\u00F1"
31
32console.log(one != two); // prints 'true'
33```
34
35Unicode normalization, as performed by the popular punycode.js library or ECMAScript 6's String.normalize, can **sometimes** fix those differences and turn two-char sequences into single characters. But it is **not** enough in all cases. Some languages like Hindi make extensive use of combining marks on their letters, that have no dedicated single-codepoint Unicode sequences, due to the sheer number of possible combinations.
36For example, the Hindi word "अनुच्छेद" is comprised of 5 letters and 3 combining marks:
37
38अ + न + ु + च + ् + छ + े + द
39
40which is in fact just 5 user-perceived letters:
41
42अ + नु + च् + छे + द
43
44and which Unicode normalization would not combine properly.
45There are also the unusual letter+combining mark combinations which have no dedicated Unicode codepoint. The string Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘ obviously has 5 separate letters, but is in fact comprised of 58 JavaScript characters, most of which are combining marks.
46
47Enter the `graphemer` library. It can be used to properly split JavaScript strings into what a human user would call separate letters (or "extended grapheme clusters" in Unicode terminology), no matter what their internal representation is. It is an implementation on the [Default Grapheme Cluster Boundary](http://unicode.org/reports/tr29/#Default_Grapheme_Cluster_Table) of [UAX #29](http://www.unicode.org/reports/tr29/).
48
49## Installation
50
51Install `graphemer` using the NPM command below:
52
53```
54$ npm i graphemer
55```
56
57## Usage
58
59If you're using [Typescript](https://www.typescriptlang.org/) or a compiler like [Babel](https://babeljs.io/) (or something like Create React App) things are pretty simple; just import, initialize and use!
60
61```javascript
62import Graphemer from 'graphemer';
63
64const splitter = new Graphemer();
65
66// split the string to an array of grapheme clusters (one string each)
67const graphemes = splitter.splitGraphemes(string);
68
69// iterate the string to an iterable iterator of grapheme clusters (one string each)
70const graphemeIterator = splitter.iterateGraphemes(string);
71
72// or do this if you just need their number
73const graphemeCount = splitter.countGraphemes(string);
74```
75
76If you're using vanilla Node you can use the `require()` method.
77
78```javascript
79const Graphemer = require('graphemer').default;
80
81const splitter = new Graphemer();
82
83const graphemes = splitter.splitGraphemes(string);
84```
85
86## Examples
87
88```javascript
89import Graphemer from 'graphemer';
90
91const splitter = new Graphemer();
92
93// plain latin alphabet - nothing spectacular
94splitter.splitGraphemes('abcd'); // returns ["a", "b", "c", "d"]
95
96// two-char emojis and six-char combined emoji
97splitter.splitGraphemes('🌷🎁💩😜👍🏳️‍🌈'); // returns ["🌷","🎁","💩","😜","👍","🏳️‍🌈"]
98
99// diacritics as combining marks, 10 JavaScript chars
100splitter.splitGraphemes('Ĺo͂řȩm̅'); // returns ["Ĺ","o͂","ř","ȩ","m̅"]
101
102// individual Korean characters (Jamo), 4 JavaScript chars
103splitter.splitGraphemes('뎌쉐'); // returns ["뎌","쉐"]
104
105// Hindi text with combining marks, 8 JavaScript chars
106splitter.splitGraphemes('अनुच्छेद'); // returns ["अ","नु","च्","छे","द"]
107
108// demonic multiple combining marks, 75 JavaScript chars
109splitter.splitGraphemes('Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞'); // returns ["Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍","A̴̵̜̰͔ͫ͗͢","L̠ͨͧͩ͘","G̴̻͈͍͔̹̑͗̎̅͛́","Ǫ̵̹̻̝̳͂̌̌͘","!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞"]
110```
111
112## TypeScript
113
114Graphemer is built with TypeScript and, of course, includes type declarations.
115
116```javascript
117import Graphemer from 'graphemer';
118
119const splitter = new Graphemer();
120
121const split: string[] = splitter.splitGraphemes('Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞');
122```
123
124## Contributing
125
126See [Contribution Guide](./CONTRIBUTING.md).
127
128## Acknowledgements
129
130This library is a fork of the incredible work done by Orlin Georgiev and Huáng Jùnliàng at https://github.com/orling/grapheme-splitter.
131
132The original library was heavily influenced by Devon Govett's excellent [grapheme-breaker](https://github.com/devongovett/grapheme-breaker) CoffeeScript library.
Note: See TracBrowser for help on using the repository browser.