@marijn/find-cluster-break 1.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/README.md +28 -0
- package/package.json +24 -0
- package/src/index.d.ts +15 -0
- package/src/index.js +87 -0
- package/test/test-cluster.js +30 -0
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# @marijn/find-cluster-break
|
|
2
|
+
|
|
3
|
+
Small JavaScript module for finding grapheme cluster breaks in
|
|
4
|
+
strings, scanning from a given position.
|
|
5
|
+
|
|
6
|
+
```javascript
|
|
7
|
+
import {findClusterBreak} from "@marijn/find-cluster-break"
|
|
8
|
+
console.log(findClusterBreak("๐ช๐ฝ๐ฆ", 0))
|
|
9
|
+
// โ 4
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
This code is open source, released under an MIT license.
|
|
13
|
+
|
|
14
|
+
## Documentation
|
|
15
|
+
|
|
16
|
+
**`findClusterBreak`**`(str: string, pos: number, forward = true, includeExtending = true): number`
|
|
17
|
+
|
|
18
|
+
Returns a next grapheme cluster break _after_ (not equal to) `pos`,
|
|
19
|
+
if `forward` is true, or before otherwise. Returns `pos` itself if no
|
|
20
|
+
further cluster break is available in the string. Moves across
|
|
21
|
+
surrogate pairs, extending characters (when `includeExtending` is
|
|
22
|
+
true, which is the default), characters joined with zero-width joiners,
|
|
23
|
+
and flag emoji.
|
|
24
|
+
|
|
25
|
+
**`isExtendingChar`**`(code: number): boolean`
|
|
26
|
+
|
|
27
|
+
Query whether the given character has a `Grapheme_Cluster_Break` value
|
|
28
|
+
of `Extend` in Unicode.
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@marijn/find-cluster-break",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Find the position of grapheme cluster breaks in a string",
|
|
6
|
+
"main": "src/index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "mocha test/*.js"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/marijnh/find-cluster-break.git"
|
|
13
|
+
},
|
|
14
|
+
"keywords": ["unicode", "grapheme", "cluster", "break"],
|
|
15
|
+
"author": "Marijn Haverbeke <marijn@haverbeke.berlin>",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/marijnh/find-cluster-break/issues"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/marijnh/find-cluster-break#readme",
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"mocha": "^10.7.3"
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Query whether the given character has a `Grapheme_Cluster_Break`
|
|
3
|
+
value of `Extend` in Unicode.
|
|
4
|
+
*/
|
|
5
|
+
export declare function isExtendingChar(code: number): boolean
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
Returns a next grapheme cluster break _after_ (not equal to) `pos`,
|
|
9
|
+
if `forward` is true, or before otherwise. Returns `pos` itself if no
|
|
10
|
+
further cluster break is available in the string. Moves across
|
|
11
|
+
surrogate pairs, extending characters (when `includeExtending` is
|
|
12
|
+
true, which is the default), characters joined with zero-width joiners,
|
|
13
|
+
and flag emoji.
|
|
14
|
+
*/
|
|
15
|
+
export declare function findClusterBreak(str: string, pos: number, forward?: boolean, includeExtending?: boolean): number
|
package/src/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// These are filled with ranges (rangeFrom[i] up to but not including
|
|
2
|
+
// rangeTo[i]) of code points that count as extending characters.
|
|
3
|
+
let rangeFrom = [], rangeTo = []
|
|
4
|
+
|
|
5
|
+
;(() => {
|
|
6
|
+
// Compressed representation of the Grapheme_Cluster_Break=Extend
|
|
7
|
+
// information from
|
|
8
|
+
// http://www.unicode.org/Public/16.0.0/ucd/auxiliary/GraphemeBreakProperty.txt.
|
|
9
|
+
// Each pair of elements represents a range, as an offet from the
|
|
10
|
+
// previous range and a length. Numbers are in base-36, with the empty
|
|
11
|
+
// string being a shorthand for 1.
|
|
12
|
+
let numbers = "lc,34,7n,7,7b,19,,,,2,,2,,,20,b,1c,l,g,,2t,7,2,6,2,2,,4,z,,u,r,2j,b,1m,9,9,,o,4,,9,,3,,5,17,3,3b,f,,w,1j,,,,4,8,4,,3,7,a,2,t,,1m,,,,2,4,8,,9,,a,2,q,,2,2,1l,,4,2,4,2,2,3,3,,u,2,3,,b,2,1l,,4,5,,2,4,,k,2,m,6,,,1m,,,2,,4,8,,7,3,a,2,u,,1n,,,,c,,9,,14,,3,,1l,3,5,3,,4,7,2,b,2,t,,1m,,2,,2,,3,,5,2,7,2,b,2,s,2,1l,2,,,2,4,8,,9,,a,2,t,,20,,4,,2,3,,,8,,29,,2,7,c,8,2q,,2,9,b,6,22,2,r,,,,,,1j,e,,5,,2,5,b,,10,9,,2u,4,,6,,2,2,2,p,2,4,3,g,4,d,,2,2,6,,f,,jj,3,qa,3,t,3,t,2,u,2,1s,2,,7,8,,2,b,9,,19,3,3b,2,y,,3a,3,4,2,9,,6,3,63,2,2,,1m,,,7,,,,,2,8,6,a,2,,1c,h,1r,4,1c,7,,,5,,14,9,c,2,w,4,2,2,,3,1k,,,2,3,,,3,1m,8,2,2,48,3,,d,,7,4,,6,,3,2,5i,1m,,5,ek,,5f,x,2da,3,3x,,2o,w,fe,6,2x,2,n9w,4,,a,w,2,28,2,7k,,3,,4,,p,2,5,,47,2,q,i,d,,12,8,p,b,1a,3,1c,,2,4,2,2,13,,1v,6,2,2,2,2,c,,8,,1b,,1f,,,3,2,2,5,2,,,16,2,8,,6m,,2,,4,,fn4,,kh,g,g,g,a6,2,gt,,6a,,45,5,1ae,3,,2,5,4,14,3,4,,4l,2,fx,4,ar,2,49,b,4w,,1i,f,1k,3,1d,4,2,2,1x,3,10,5,,8,1q,,c,2,1g,9,a,4,2,,2n,3,2,,,2,6,,4g,,3,8,l,2,1l,2,,,,,m,,e,7,3,5,5f,8,2,3,,,n,,29,,2,6,,,2,,,2,,2,6j,,2,4,6,2,,2,r,2,2d,8,2,,,2,2y,,,,2,6,,,2t,3,2,4,,5,77,9,,2,6t,,a,2,,,4,,40,4,2,2,4,,w,a,14,6,2,4,8,,9,6,2,3,1a,d,,2,ba,7,,6,,,2a,m,2,7,,2,,2,3e,6,3,,,2,,7,,,20,2,3,,,,9n,2,f0b,5,1n,7,t4,,1r,4,29,,f5k,2,43q,,,3,4,5,8,8,2,7,u,4,44,3,1iz,1j,4,1e,8,,e,,m,5,,f,11s,7,,h,2,7,,2,,5,79,7,c5,4,15s,7,31,7,240,5,gx7k,2o,3k,6o".split(",").map(s => s ? parseInt(s, 36) : 1)
|
|
13
|
+
for (let i = 0, n = 0; i < numbers.length; i++)
|
|
14
|
+
(i % 2 ? rangeTo : rangeFrom).push(n = n + numbers[i])
|
|
15
|
+
})()
|
|
16
|
+
|
|
17
|
+
export function isExtendingChar(code) {
|
|
18
|
+
if (code < 768) return false
|
|
19
|
+
for (let from = 0, to = rangeFrom.length;;) {
|
|
20
|
+
let mid = (from + to) >> 1
|
|
21
|
+
if (code < rangeFrom[mid]) to = mid
|
|
22
|
+
else if (code >= rangeTo[mid]) from = mid + 1
|
|
23
|
+
else return true
|
|
24
|
+
if (from == to) return false
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isRegionalIndicator(code) {
|
|
29
|
+
return code >= 0x1F1E6 && code <= 0x1F1FF
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function check(code) {
|
|
33
|
+
for (let i = 0; i < rangeFrom.length; i++) {
|
|
34
|
+
if (rangeTo[i] > code) return rangeFrom[i] <= code
|
|
35
|
+
}
|
|
36
|
+
return false
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const ZWJ = 0x200d
|
|
40
|
+
|
|
41
|
+
export function findClusterBreak(str, pos, forward = true, includeExtending = true) {
|
|
42
|
+
return (forward ? nextClusterBreak : prevClusterBreak)(str, pos, includeExtending)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function nextClusterBreak(str, pos, includeExtending) {
|
|
46
|
+
if (pos == str.length) return pos
|
|
47
|
+
// If pos is in the middle of a surrogate pair, move to its start
|
|
48
|
+
if (pos && surrogateLow(str.charCodeAt(pos)) && surrogateHigh(str.charCodeAt(pos - 1))) pos--
|
|
49
|
+
let prev = codePointAt(str, pos)
|
|
50
|
+
pos += codePointSize(prev)
|
|
51
|
+
while (pos < str.length) {
|
|
52
|
+
let next = codePointAt(str, pos)
|
|
53
|
+
if (prev == ZWJ || next == ZWJ || includeExtending && isExtendingChar(next)) {
|
|
54
|
+
pos += codePointSize(next)
|
|
55
|
+
prev = next
|
|
56
|
+
} else if (isRegionalIndicator(next)) {
|
|
57
|
+
let countBefore = 0, i = pos - 2
|
|
58
|
+
while (i >= 0 && isRegionalIndicator(codePointAt(str, i))) { countBefore++; i -= 2 }
|
|
59
|
+
if (countBefore % 2 == 0) break
|
|
60
|
+
else pos += 2
|
|
61
|
+
} else {
|
|
62
|
+
break
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return pos
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function prevClusterBreak(str, pos, includeExtending) {
|
|
69
|
+
while (pos > 0) {
|
|
70
|
+
let found = nextClusterBreak(str, pos - 2, includeExtending)
|
|
71
|
+
if (found < pos) return found
|
|
72
|
+
pos--
|
|
73
|
+
}
|
|
74
|
+
return 0
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function codePointAt(str, pos) {
|
|
78
|
+
let code0 = str.charCodeAt(pos)
|
|
79
|
+
if (!surrogateHigh(code0) || pos + 1 == str.length) return code0
|
|
80
|
+
let code1 = str.charCodeAt(pos + 1)
|
|
81
|
+
if (!surrogateLow(code1)) return code0
|
|
82
|
+
return ((code0 - 0xd800) << 10) + (code1 - 0xdc00) + 0x10000
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function surrogateLow(ch) { return ch >= 0xDC00 && ch < 0xE000 }
|
|
86
|
+
function surrogateHigh(ch) { return ch >= 0xD800 && ch < 0xDC00 }
|
|
87
|
+
function codePointSize(code) { return code < 0x10000 ? 1 : 2 }
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {findClusterBreak} from "../src/index.js"
|
|
2
|
+
|
|
3
|
+
function assertEq(a, b) {
|
|
4
|
+
if (a !== b) throw new Error(`${a} !== ${b}`)
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
describe("findClusterBreak", () => {
|
|
8
|
+
function test(spec) {
|
|
9
|
+
it(spec, () => {
|
|
10
|
+
let breaks = [], next
|
|
11
|
+
while ((next = spec.indexOf("|")) > -1) {
|
|
12
|
+
breaks.push(next)
|
|
13
|
+
spec = spec.slice(0, next) + spec.slice(next + 1)
|
|
14
|
+
}
|
|
15
|
+
let found = []
|
|
16
|
+
for (let i = 0;;) {
|
|
17
|
+
let next = findClusterBreak(spec, i)
|
|
18
|
+
if (next == spec.length) break
|
|
19
|
+
found.push(i = next)
|
|
20
|
+
}
|
|
21
|
+
assertEq(found.join(","), breaks.join(","))
|
|
22
|
+
})
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
test("a|b|c|d")
|
|
26
|
+
test("a|eฬฬ |oฬ|x")
|
|
27
|
+
test("๐|๐")
|
|
28
|
+
test("๐จโ๐ค|๐ช๐ฝ|๐ฉโ๐ฉโ๐งโ๐ฆ|โค")
|
|
29
|
+
test("๐ฉ๐ช|๐ซ๐ท|๐ช๐ธ|x|๐ฎ๐น")
|
|
30
|
+
})
|