Skip to content

Commit b8182e8

Browse files
committed
Support separator
1 parent 7712156 commit b8182e8

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "string_wizard"
3-
version = "0.0.2"
3+
version = "0.0.3"
44
edition = "2021"
55
license = "MIT"
66
description = "manipulate string like wizards"

src/joiner.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
use crate::{MagicString, CowStr};
22

3+
pub struct JoinerOptions {
4+
pub separator: Option<String>,
5+
}
6+
37
#[derive(Default)]
48
pub struct Joiner<'s> {
59
sources: Vec<MagicString<'s>>,
10+
separator: Option<String>,
611
}
712

813
impl<'s> Joiner<'s> {
914
// --- public
15+
pub fn with_options(options: JoinerOptions) -> Self {
16+
Self {
17+
separator: options.separator,
18+
..Default::default()
19+
}
20+
}
21+
1022
pub fn append(&mut self, source: MagicString<'s>) -> &mut Self {
1123
self.sources.push(source);
1224
self
@@ -32,7 +44,15 @@ impl<'s> Joiner<'s> {
3244
// --- private
3345

3446
fn fragments(&'s self) -> impl Iterator<Item = &'s str> {
35-
self.sources.iter().flat_map(|c| c.fragments())
47+
let mut iter = self
48+
.sources
49+
.iter()
50+
.flat_map(|c| self.separator.as_ref().map(|s| s.as_str()).into_iter().chain(c.fragments()));
51+
// Drop the first separator
52+
if self.separator.is_some() {
53+
iter.next();
54+
}
55+
iter
3656
}
3757

3858

src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type CowStr<'s> = Cow<'s, str>;
1010
use chunk::Chunk;
1111
use index_vec::IndexVec;
1212

13-
pub use crate::{magic_string::{MagicString, MagicStringOptions}, joiner::Joiner};
13+
pub use crate::{magic_string::{MagicString, MagicStringOptions}, joiner::{Joiner, JoinerOptions}};
1414

1515

1616
index_vec::define_index_type! {
@@ -22,7 +22,3 @@ type ChunkVec<'s> = IndexVec<ChunkIdx, Chunk<'s>>;
2222
index_vec::define_index_type! {
2323
struct SourceIdx = u32;
2424
}
25-
26-
27-
28-
pub struct Bundle {}

tests/joiner.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use string_wizard::{Joiner, MagicString};
1+
use string_wizard::{Joiner, MagicString, JoinerOptions};
22
mod append {
33
use super::*;
44

@@ -9,4 +9,15 @@ mod append {
99
j.append_raw("123").append_raw("456");
1010
assert_eq!(j.join(), "*123456");
1111
}
12+
}
13+
14+
#[test]
15+
fn separator() {
16+
let mut j = Joiner::with_options(JoinerOptions { separator: Some(",".to_string()) });
17+
j.append_raw("123");
18+
assert_eq!(j.join(), "123");
19+
j.append_raw("123");
20+
assert_eq!(j.join(), "123,123");
21+
j.append_raw("123");
22+
assert_eq!(j.join(), "123,123,123");
1223
}

0 commit comments

Comments
 (0)