Skip to content

taisukef/grapheme-breaker-mjs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

grapheme-breaker-mjs

A JavaScript (ES Module) implementation for web apps, Deno and Node.js of the Unicode 13.0.0 grapheme cluster breaking algorithm (UAX #29)

This is a fork of grapheme-breaker-u10-0. Support Unicode 10.0 and emoji v5 by @vaskevich(publishd by @yumetodo).
The base project is grapheme-breaker by @devongovett

for Web and Deno

test page
https://taisukef.github.io/grapheme-breaker-mjs/

import GraphemeBreaker from 'https://taisukef.github.io/grapheme-breaker-mjs/src/GraphemeBreaker.mjs'

console.log(GraphemeBreaker.break('😜🇺🇸👍')) // => [ '😜', '🇺🇸', '👍' ]

Installation

You can install via npm

npm i grapheme-breaker-mjs

Example

import GraphemeBreaker from 'grapheme-breaker-mjs'

// break a string into an array of grapheme clusters


GraphemeBreaker.break('Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞') // => ['Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍', 'A̴̵̜̰͔ͫ͗͢', 'L̠ͨͧͩ͘', 'G̴̻͈͍͔̹̑͗̎̅͛́', 'Ǫ̵̹̻̝̳͂̌̌͘', '!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞']


// or just count the number of grapheme clusters in a string


GraphemeBreaker.countBreaks('Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞') // => 6


// use nextBreak and previousBreak to get break points starting
// from anywhere in the string
GraphemeBreaker.nextBreak('😜🇺🇸👍', 3) // => 6
GraphemeBreaker.previousBreak('😜🇺🇸👍', 3) // => 2

Development Notes

In order to use the library, you shouldn't need to know this, but if you're interested in contributing or fixing bugs, these things might be of interest.

  • The src/classes.mjs file is generated from GraphemeBreakProperty.txt in the Unicode database by src/generate_data.mjs. It should be rare that you need to run this, but you may if, for instance, you want to change the Unicode version.
  • You can run the tests using npm test. They are written using mocha, and generated from GraphemeBreakTest.txt and emoji-test.txt from the Unicode database, which is included in the repository for performance reasons while running them.

License

MIT

About

A JS implementation of the Unicode 13.0.0 grapheme cluster breaking algorithm (UAX #29)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 99.2%
  • HTML 0.8%