1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use liquid_core::Result;
use liquid_core::Runtime;
use liquid_core::{Display_filter, Filter, FilterReflection, ParseFilter};
use liquid_core::{Value, ValueView};

/// Removes all whitespace (tabs, spaces, and newlines) from both the left and right side of a
/// string.
///
/// It does not affect spaces between words.  Note that while this works for the case of tabs,
/// spaces, and newlines, it also removes any other codepoints defined by the Unicode Derived Core
/// Property `White_Space` (per [rust
/// documentation](https://doc.rust-lang.org/std/primitive.str.html#method.trim_start).
#[derive(Clone, ParseFilter, FilterReflection)]
#[filter(
    name = "strip",
    description = "Removes all whitespace (tabs, spaces, and newlines) from both the left and right side of a string.",
    parsed(StripFilter)
)]
pub struct Strip;

#[derive(Debug, Default, Display_filter)]
#[name = "strip"]
struct StripFilter;

impl Filter for StripFilter {
    fn evaluate(&self, input: &dyn ValueView, _runtime: &Runtime<'_>) -> Result<Value> {
        let input = input.to_kstr();
        Ok(Value::scalar(input.trim().to_owned()))
    }
}

/// Removes all whitespaces (tabs, spaces, and newlines) from the beginning of a string.
///
/// The filter does not affect spaces between words.  Note that while this works for the case of
/// tabs, spaces, and newlines, it also removes any other codepoints defined by the Unicode Derived
/// Core Property `White_Space` (per [rust
/// documentation](https://doc.rust-lang.org/std/primitive.str.html#method.trim_start).
#[derive(Clone, ParseFilter, FilterReflection)]
#[filter(
    name = "lstrip",
    description = "Removes all whitespaces (tabs, spaces, and newlines) from the beginning of a string.",
    parsed(LstripFilter)
)]
pub struct Lstrip;

#[derive(Debug, Default, Display_filter)]
#[name = "lstrip"]
struct LstripFilter;

impl Filter for LstripFilter {
    fn evaluate(&self, input: &dyn ValueView, _runtime: &Runtime<'_>) -> Result<Value> {
        let input = input.to_kstr();
        Ok(Value::scalar(input.trim_start().to_owned()))
    }
}

/// Removes all whitespace (tabs, spaces, and newlines) from the right side of a string.
///
/// The filter does not affect spaces between words.  Note that while this works for the case of
/// tabs, spaces, and newlines, it also removes any other codepoints defined by the Unicode Derived
/// Core Property `White_Space` (per [rust
/// documentation](https://doc.rust-lang.org/std/primitive.str.html#method.trim_start).
#[derive(Clone, ParseFilter, FilterReflection)]
#[filter(
    name = "rstrip",
    description = "Removes all whitespace (tabs, spaces, and newlines) from the right side of a string.",
    parsed(RstripFilter)
)]
pub struct Rstrip;

#[derive(Debug, Default, Display_filter)]
#[name = "rstrip"]
struct RstripFilter;

impl Filter for RstripFilter {
    fn evaluate(&self, input: &dyn ValueView, _runtime: &Runtime<'_>) -> Result<Value> {
        let input = input.to_kstr();
        Ok(Value::scalar(input.trim_end().to_owned()))
    }
}

#[derive(Clone, ParseFilter, FilterReflection)]
#[filter(
    name = "strip_newlines",
    description = "Removes any newline characters (line breaks) from a string.",
    parsed(StripNewlinesFilter)
)]
pub struct StripNewlines;

#[derive(Debug, Default, Display_filter)]
#[name = "strip_newlines"]
struct StripNewlinesFilter;

impl Filter for StripNewlinesFilter {
    fn evaluate(&self, input: &dyn ValueView, _runtime: &Runtime<'_>) -> Result<Value> {
        let input = input.to_kstr();
        Ok(Value::scalar(
            input
                .chars()
                .filter(|c| *c != '\n' && *c != '\r')
                .collect::<String>(),
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn unit_lstrip() {
        assert_eq!(
            liquid_core::call_filter!(Lstrip, " 	 \n \r test").unwrap(),
            liquid_core::value!("test")
        );
    }

    #[test]
    fn unit_lstrip_non_string() {
        assert_eq!(
            liquid_core::call_filter!(Lstrip, 0f64).unwrap(),
            liquid_core::value!("0")
        );
    }

    #[test]
    fn unit_lstrip_one_argument() {
        liquid_core::call_filter!(Lstrip, " 	 \n \r test", 0f64).unwrap_err();
    }

    #[test]
    fn unit_lstrip_shopify_liquid() {
        // One test from https://shopify.github.io/liquid/filters/lstrip/
        assert_eq!(
            liquid_core::call_filter!(Lstrip, "          So much room for activities!          ")
                .unwrap(),
            liquid_core::value!("So much room for activities!          ")
        );
    }

    #[test]
    fn unit_lstrip_trailing_sequence() {
        assert_eq!(
            liquid_core::call_filter!(Lstrip, " 	 \n \r test 	 \n \r ").unwrap(),
            liquid_core::value!("test 	 \n \r ")
        );
    }

    #[test]
    fn unit_lstrip_trailing_sequence_only() {
        assert_eq!(
            liquid_core::call_filter!(Lstrip, "test 	 \n \r ").unwrap(),
            liquid_core::value!("test 	 \n \r ")
        );
    }

    #[test]
    fn unit_rstrip() {
        assert_eq!(
            liquid_core::call_filter!(Rstrip, "test 	 \n \r ").unwrap(),
            liquid_core::value!("test")
        );
    }

    #[test]
    fn unit_rstrip_leading_sequence() {
        assert_eq!(
            liquid_core::call_filter!(Rstrip, " 	 \n \r test 	 \n \r ").unwrap(),
            liquid_core::value!(" 	 \n \r test")
        );
    }

    #[test]
    fn unit_rstrip_leading_sequence_only() {
        assert_eq!(
            liquid_core::call_filter!(Rstrip, " 	 \n \r test").unwrap(),
            liquid_core::value!(" 	 \n \r test")
        );
    }

    #[test]
    fn unit_rstrip_non_string() {
        assert_eq!(
            liquid_core::call_filter!(Rstrip, 0f64).unwrap(),
            liquid_core::value!("0")
        );
    }

    #[test]
    fn unit_rstrip_one_argument() {
        liquid_core::call_filter!(Rstrip, " 	 \n \r test", 0f64).unwrap_err();
    }

    #[test]
    fn unit_rstrip_shopify_liquid() {
        // One test from https://shopify.github.io/liquid/filters/rstrip/
        assert_eq!(
            liquid_core::call_filter!(Rstrip, "          So much room for activities!          ")
                .unwrap(),
            liquid_core::value!("          So much room for activities!")
        );
    }

    #[test]
    fn unit_strip() {
        assert_eq!(
            liquid_core::call_filter!(Strip, " 	 \n \r test 	 \n \r ").unwrap(),
            liquid_core::value!("test")
        );
    }

    #[test]
    fn unit_strip_leading_sequence_only() {
        assert_eq!(
            liquid_core::call_filter!(Strip, " 	 \n \r test").unwrap(),
            liquid_core::value!("test")
        );
    }

    #[test]
    fn unit_strip_non_string() {
        assert_eq!(
            liquid_core::call_filter!(Strip, 0f64).unwrap(),
            liquid_core::value!("0")
        );
    }

    #[test]
    fn unit_strip_one_argument() {
        liquid_core::call_filter!(Strip, " 	 \n \r test 	 \n \r ", 0f64).unwrap_err();
    }

    #[test]
    fn unit_strip_shopify_liquid() {
        // One test from https://shopify.github.io/liquid/filters/strip/
        assert_eq!(
            liquid_core::call_filter!(Strip, "          So much room for activities!          ")
                .unwrap(),
            liquid_core::value!("So much room for activities!")
        );
    }

    #[test]
    fn unit_strip_trailing_sequence_only() {
        assert_eq!(
            liquid_core::call_filter!(Strip, "test 	 \n \r ").unwrap(),
            liquid_core::value!("test")
        );
    }

    #[test]
    fn unit_strip_newlines() {
        assert_eq!(
            liquid_core::call_filter!(StripNewlines, "a\nb\n").unwrap(),
            liquid_core::value!("ab")
        );
    }

    #[test]
    fn unit_strip_newlines_between_only() {
        assert_eq!(
            liquid_core::call_filter!(StripNewlines, "a\nb").unwrap(),
            liquid_core::value!("ab")
        );
    }

    #[test]
    fn unit_strip_newlines_leading_only() {
        assert_eq!(
            liquid_core::call_filter!(StripNewlines, "\nab").unwrap(),
            liquid_core::value!("ab")
        );
    }

    #[test]
    fn unit_strip_newlines_non_string() {
        assert_eq!(
            liquid_core::call_filter!(StripNewlines, 0f64).unwrap(),
            liquid_core::value!("0")
        );
    }

    #[test]
    fn unit_strip_newlines_one_argument() {
        liquid_core::call_filter!(StripNewlines, "ab\n", 0f64).unwrap_err();
    }

    #[test]
    fn unit_strip_newlines_shopify_liquid() {
        // Test from https://shopify.github.io/liquid/filters/strip_newlines/
        assert_eq!(
            liquid_core::call_filter!(StripNewlines, "\nHello\nthere\n").unwrap(),
            liquid_core::value!("Hellothere")
        );
    }

    #[test]
    fn unit_strip_newlines_trailing_only() {
        assert_eq!(
            liquid_core::call_filter!(StripNewlines, "ab\n").unwrap(),
            liquid_core::value!("ab")
        );
    }
}