File tree Expand file tree Collapse file tree 3 files changed +31
-2
lines changed Expand file tree Collapse file tree 3 files changed +31
-2
lines changed Original file line number Diff line number Diff line change @@ -27,11 +27,16 @@ impl<'s> Chunk<'s> {
27
27
intro_iter. chain ( Some ( source_frag) ) . chain ( outro_iter)
28
28
}
29
29
30
- pub fn append_outro ( & mut self , frag : CowStr < ' s > ) {
30
+ pub fn append ( & mut self , frag : CowStr < ' s > ) {
31
31
self . len += frag. len ( ) ;
32
32
self . outro . push ( frag. into ( ) )
33
33
}
34
34
35
+ pub fn prepend ( & mut self , frag : CowStr < ' s > ) {
36
+ self . len += frag. len ( ) ;
37
+ self . intro . push ( frag. into ( ) )
38
+ }
39
+
35
40
pub fn len ( & self ) -> usize {
36
41
self . len
37
42
}
Original file line number Diff line number Diff line change @@ -42,7 +42,12 @@ impl<'s> MagicString<'s> {
42
42
}
43
43
44
44
pub fn append ( & mut self , source : impl Into < CowStr < ' s > > ) -> & mut Self {
45
- self . last_chunk_mut ( ) . append_outro ( source. into ( ) ) ;
45
+ self . last_chunk_mut ( ) . append ( source. into ( ) ) ;
46
+ self
47
+ }
48
+
49
+ pub fn prepend ( & mut self , source : impl Into < CowStr < ' s > > ) -> & mut Self {
50
+ self . first_chunk_mut ( ) . prepend ( source. into ( ) ) ;
46
51
self
47
52
}
48
53
@@ -99,6 +104,11 @@ impl<'s> MagicString<'s> {
99
104
let idx = self . chunk_by_end . get ( & ( self . source_len ) ) . unwrap ( ) ;
100
105
& mut self . chunks [ * idx]
101
106
}
107
+
108
+ fn first_chunk_mut ( & mut self ) -> & mut Chunk < ' s > {
109
+ let idx = self . chunk_by_start . get ( & 0 ) . unwrap ( ) ;
110
+ & mut self . chunks [ * idx]
111
+ }
102
112
}
103
113
104
114
struct ChunkIter < ' a > {
Original file line number Diff line number Diff line change @@ -28,3 +28,17 @@ mod append {
28
28
assert_eq ! ( s. to_string( ) , "abcdefghijklxyzxyz" ) ;
29
29
}
30
30
}
31
+
32
+ mod prepend {
33
+ use super :: * ;
34
+
35
+ #[ test]
36
+ fn should_append_content ( ) {
37
+ // should append content
38
+ let mut s = MagicString :: new ( "abcdefghijkl" ) ;
39
+ s. prepend ( "xyz" ) ;
40
+ assert_eq ! ( s. to_string( ) , "xyzabcdefghijkl" ) ;
41
+ s. prepend ( "xyz" ) ;
42
+ assert_eq ! ( s. to_string( ) , "xyzxyzabcdefghijkl" ) ;
43
+ }
44
+ }
You can’t perform that action at this time.
0 commit comments