Skip to content

Commit b923c57

Browse files
Moshood AlabidunMoshood Alabidun
authored andcommitted
Fix ESLint errors and remove comments from grapheme count implementation
1 parent 27cf1c1 commit b923c57

File tree

5 files changed

+31
-31
lines changed

5 files changed

+31
-31
lines changed

packages/nhsuk-frontend/src/nhsuk/common/grapheme-count.jsdom.test.mjs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('graphemeCount', () => {
5252
})
5353

5454
it('handles mixed content', () => {
55-
expect(graphemeCount('Hello 👋 World')).toBe(12)
55+
expect(graphemeCount('Hello 👋 World')).toBe(13)
5656
expect(graphemeCount('café 👋🏼')).toBe(6)
5757
})
5858

@@ -64,19 +64,19 @@ describe('graphemeCount', () => {
6464
it('throws for invalid input', () => {
6565
expect(() => graphemeCount(null)).toThrow(TypeError)
6666
expect(() => graphemeCount(undefined)).toThrow(TypeError)
67-
// @ts-expect-error
67+
// @ts-expect-error - Testing runtime type validation with invalid number input
6868
expect(() => graphemeCount(123)).toThrow(TypeError)
69-
// @ts-expect-error
69+
// @ts-expect-error - Testing runtime type validation with invalid object input
7070
expect(() => graphemeCount({})).toThrow(TypeError)
71-
// @ts-expect-error
71+
// @ts-expect-error - Testing runtime type validation with invalid array input
7272
expect(() => graphemeCount([])).toThrow(TypeError)
7373
})
7474

7575
it('works with real examples', () => {
7676
expect(graphemeCount('NHS 👨‍⚕️')).toBe(5)
77-
expect(graphemeCount('Call 111 🏥')).toBe(8)
78-
expect(graphemeCount('Please describe your symptoms')).toBe(33)
79-
expect(graphemeCount('Feeling unwell 😷')).toBe(15)
77+
expect(graphemeCount('Call 111 🏥')).toBe(10)
78+
expect(graphemeCount('Please describe your symptoms')).toBe(29)
79+
expect(graphemeCount('Feeling unwell 😷')).toBe(16)
8080
expect(graphemeCount('José García')).toBe(11)
8181
})
8282
})

packages/nhsuk-frontend/src/nhsuk/common/grapheme-count.mjs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@ export function graphemeCount(text) {
77
throw new TypeError('graphemeCount expects a string argument')
88
}
99

10-
if (typeof Intl !== 'undefined' && Intl.Segmenter) {
10+
if ('Segmenter' in Intl) {
1111
try {
1212
const segmenter = new Intl.Segmenter('en', { granularity: 'grapheme' })
1313
return [...segmenter.segment(text)].length
14-
} catch {
15-
// fallback for older browsers
14+
} catch (_) {
15+
void _
1616
}
1717
}
1818

19-
let result = 0
20-
let index = 0
19+
let count = 0
20+
let i = 0
2121

22-
while (index < text.length) {
23-
const point = text.codePointAt(index)
24-
if (point === undefined) {
25-
index++
26-
continue
22+
while (i < text.length) {
23+
const codePoint = text.codePointAt(i)
24+
if (codePoint !== undefined) {
25+
count++
26+
i += codePoint > 0xffff ? 2 : 1
27+
} else {
28+
i++
2729
}
28-
result++
29-
index += point > 0xffff ? 2 : 1
3030
}
3131

32-
return result
32+
return count
3333
}

packages/nhsuk-frontend/src/nhsuk/components/character-count/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ This counting method matches Python's `len()` function for Unicode strings, ensu
2323

2424
```javascript
2525
// Simple ASCII
26-
"Hello" 5 characters
26+
"Hello" // 5 characters
2727

2828
// Emoji
29-
"👋" 1 character (not 2 code points)
30-
"👋🏼" 1 character (emoji with skin tone modifier)
31-
"👨‍👩‍👧‍👦" 1 character (family emoji sequence)
29+
"👋" // 1 character (not 2 code points)
30+
"👋🏼" // 1 character (emoji with skin tone modifier)
31+
"👨‍👩‍👧‍👦" // 1 character (family emoji sequence)
3232

3333
// Accented characters
34-
"café" 4 characters
35-
"naïve" 5 characters
34+
"café" // 4 characters
35+
"naïve" // 5 characters
3636

3737
// Mixed content
38-
"Hello 👋" 6 characters (5 letters + 1 space + 1 emoji)
38+
"Hello 👋" // 6 characters (5 letters + 1 space + 1 emoji)
3939
```
4040

4141
### Browser support

packages/nhsuk-frontend/src/nhsuk/components/character-count/character-count.jsdom.test.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ describe('Character count: Format count message', () => {
356356
$textarea.value = '👋👋👋👋👋👋👋👋👋👋'
357357
component.updateCountMessage()
358358
const $status = document.querySelector('.nhsuk-character-count__status')
359-
expect($status.textContent).toBe('You have 0 characters remaining')
359+
expect($status).toHaveTextContent('You have 0 characters remaining')
360360
})
361361

362362
it('shows error when over limit with emoji', () => {
@@ -366,8 +366,8 @@ describe('Character count: Format count message', () => {
366366
$textarea.value = '👋👋👋👋👋👋👋👋👋👋👋'
367367
component.updateCountMessage()
368368
const $status = document.querySelector('.nhsuk-character-count__status')
369-
expect($status.textContent).toBe('You have 1 character too many')
370-
expect($status.classList.contains('nhsuk-error-message')).toBe(true)
369+
expect($status).toHaveTextContent('You have 1 character too many')
370+
expect($status).toHaveClass('nhsuk-error-message')
371371
})
372372
})
373373
})

packages/nhsuk-frontend/src/nhsuk/components/character-count/character-count.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import {
33
normaliseOptions,
44
validateConfig
55
} from '../../common/configuration/index.mjs'
6-
import { formatErrorMessage } from '../../common/index.mjs'
76
import { graphemeCount } from '../../common/grapheme-count.mjs'
7+
import { formatErrorMessage } from '../../common/index.mjs'
88
import { ConfigurableComponent } from '../../configurable-component.mjs'
99
import { ConfigError, ElementError } from '../../errors/index.mjs'
1010
import { I18n } from '../../i18n.mjs'

0 commit comments

Comments
 (0)