fix(docs): count Greek, Cyrillic, Hebrew, and Arabic words (#311)
This commit is contained in:
@@ -13,7 +13,10 @@
|
||||
const ASIAN_RE =
|
||||
/[ᄀ-ᇿ⺀-、-〿-ヿ-ㄯ-㇀-ㇿ㐀-䶿一-鿿가-豈-!-⦆¢-₩]|[\uD840-\uD87F][\uDC00-\uDFFF]/g
|
||||
|
||||
const NON_ASIAN_WORD_RE = /[A-Za-z0-9À-ɏ]+(?:['-][A-Za-z0-9À-ɏ]+)*/g
|
||||
// Latin Extended Additional (Vietnamese, …) plus the space-delimited
|
||||
// non-Latin scripts Word counts as words: Greek, Cyrillic, Hebrew, Arabic.
|
||||
// Deliberately excludes the ASIAN_RE ranges above (counted char-by-char).
|
||||
const NON_ASIAN_WORD_RE = /[A-Za-z0-9À-ɏͰ-ϿЀ-ӿ--ۿḀ-ỿ]+(?:['-][A-Za-z0-9À-ɏͰ-ϿЀ-ӿ--ۿḀ-ỿ]+)*/g
|
||||
|
||||
export function asianCharCount(text: string): number {
|
||||
return (text.match(ASIAN_RE) ?? []).length
|
||||
|
||||
@@ -29,4 +29,13 @@ describe('word count CJK rule', () => {
|
||||
expect(countWords('第1章:GenOffice 使用指南')).toBe(9)
|
||||
// 7 asian chars (incl. the fullwidth colon) + "1" and "GenOffice" as 2 words
|
||||
})
|
||||
|
||||
it('counts space-delimited non-Latin scripts as words', () => {
|
||||
expect(nonAsianWordCount('Привет мир')).toBe(2)
|
||||
expect(nonAsianWordCount('Γεια σου')).toBe(2)
|
||||
expect(nonAsianWordCount('שלום עולם')).toBe(2)
|
||||
expect(nonAsianWordCount('مرحبا بالعالم')).toBe(2)
|
||||
expect(countWords('Привет мир')).toBe(2)
|
||||
expect(countWords('Hello Привет')).toBe(2)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user