11import add from '../run/add' ;
22import emptyRun from '../run/empty' ;
33import prependToRun from '../run/prepend' ;
4- import { AttributedString , Glyph } from '../types' ;
4+ import { AttributedString , Glyph , Run } from '../types' ;
55import stringFromCodePoints from '../utils/stringFromCodePoints' ;
66
77/**
8- * prepend glyph into last run of attributed string
8+ * Prepend glyph into first run of attributed string
99 *
10- * @param glyph
10+ * @param glyph - Glyph to prepend
1111 * @param attributedString - Attributed string
1212 * @returns Attributed string with new glyph
1313 */
@@ -19,13 +19,16 @@ const prepend = (
1919 const string = stringFromCodePoints ( codePoints ) + attributedString . string ;
2020
2121 const offset = codePoints . length ;
22- const firstRun = attributedString . runs [ 0 ] || emptyRun ( ) ;
23- const lastRuns = attributedString . runs
24- . slice ( 1 )
25- . map ( ( run ) => add ( offset , run ) ) ;
26- const runs = [ prependToRun ( glyph , firstRun ) ] . concat ( lastRuns ) ;
22+ const { runs : existingRuns } = attributedString ;
23+ const firstRun = existingRuns [ 0 ] || emptyRun ( ) ;
2724
28- return Object . assign ( { } , attributedString , { runs, string } ) ;
25+ // Build new runs array: prepend to first run, offset remaining runs
26+ const runs : Run [ ] = [ prependToRun ( glyph , firstRun ) ] ;
27+ for ( let i = 1 ; i < existingRuns . length ; i += 1 ) {
28+ runs . push ( add ( offset , existingRuns [ i ] ) ) ;
29+ }
30+
31+ return { ...attributedString , runs, string } ;
2932} ;
3033
3134export default prepend ;
0 commit comments