Technical skill practice
Reverse a String in JavaScript: Preserve the Intended Text Units
Before reversing a string, decide what counts as one unit. Splitting into empty-string pieces works on UTF-16 code units; spreading the string works on code points. Neither automatically preserves every user-perceived character cluster.
The task
Reverse a decomposed accented letter followed by an emoji, keeping the accent attached to its letter. Verify Intl.Segmenter behavior on the actual Node22 runtime.
Runnable Node 22 example
const assert = require('node:assert/strict');
const text = 'a\u0301🙂';
const codeUnits = text.split('').reverse().join('');
const codePoints = [...text].reverse().join('');
const clusters = [...new Intl.Segmenter('en', {granularity: 'grapheme'}).segment(text)]
.map(part => part.segment);
const reversed = clusters.reverse().join('');
assert.notEqual(codeUnits, '🙂a\u0301');
assert.equal(codePoints, '🙂\u0301a');
assert.equal(reversed, '🙂a\u0301');
console.log(reversed);
Expected output and explanation
The code-unit reversal fails the desired output and can separate the emoji’s surrogate pair. The code-point reversal preserves the emoji but moves the combining accent ahead of its letter, producing a different cluster arrangement.
The segmenter groups the original accented letter before reversing the segments, so the target output retains that relationship. This is a tested result for the explicit fixture and runtime, not a claim that reversing text preserves every language’s display or reading semantics.
Output from the example
🙂á
Pitfall to check
Reversal can be inappropriate for bidirectional writing or complex presentation requirements even when clusters remain intact. Define the product’s text operation first. Do not call a code-unit algorithm Unicode-safe merely because it works for a short ASCII sample.
Questions, answered
Can I use split("").reverse() for ASCII-only identifiers?
It can be suitable when the input contract truly guarantees that limited character set. Keep that restriction explicit and test rejected nonconforming input.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.