@@ -7,45 +7,45 @@ use crate::filter_utils::{get_sort_strategy_for_type, get_unique_strategy_for_ty
7
7
use serde_json:: value:: { to_value, Map , Value } ;
8
8
9
9
/// Returns the nth value of an array
10
- /// If the array is empty, returns empty string
10
+ /// If the array is empty, returns null
11
11
pub fn nth ( value : & Value , args : & HashMap < String , Value > ) -> Result < Value > {
12
12
let arr = try_get_value ! ( "nth" , "value" , Vec <Value >, value) ;
13
13
14
14
if arr. is_empty ( ) {
15
- return Ok ( to_value ( "" ) . unwrap ( ) ) ;
15
+ return Ok ( Value :: Null ) ;
16
16
}
17
17
18
18
let index = match args. get ( "n" ) {
19
19
Some ( val) => try_get_value ! ( "nth" , "n" , usize , val) ,
20
20
None => return Err ( Error :: msg ( "The `nth` filter has to have an `n` argument" ) ) ,
21
21
} ;
22
22
23
- Ok ( arr. get ( index) . unwrap_or ( & to_value ( "" ) . unwrap ( ) ) . to_owned ( ) )
23
+ Ok ( arr. get ( index) . map ( |x| x . to_owned ( ) ) . unwrap_or ( Value :: Null ) )
24
24
}
25
25
26
26
/// Returns the first value of an array
27
- /// If the array is empty, returns empty string
27
+ /// If the array is empty, returns null
28
28
pub fn first ( value : & Value , _: & HashMap < String , Value > ) -> Result < Value > {
29
29
let mut arr = try_get_value ! ( "first" , "value" , Vec <Value >, value) ;
30
30
31
31
if arr. is_empty ( ) {
32
- Ok ( to_value ( "" ) . unwrap ( ) )
32
+ Ok ( Value :: Null )
33
33
} else {
34
34
Ok ( arr. swap_remove ( 0 ) )
35
35
}
36
36
}
37
37
38
38
/// Returns the last value of an array
39
- /// If the array is empty, returns empty string
39
+ /// If the array is empty, returns null
40
40
pub fn last ( value : & Value , _: & HashMap < String , Value > ) -> Result < Value > {
41
41
let mut arr = try_get_value ! ( "last" , "value" , Vec <Value >, value) ;
42
42
43
- Ok ( arr. pop ( ) . unwrap_or_else ( || to_value ( "" ) . unwrap ( ) ) )
43
+ Ok ( arr. pop ( ) . unwrap_or ( Value :: Null ) )
44
44
}
45
45
46
46
/// Joins all values in the array by the `sep` argument given
47
47
/// If no separator is given, it will use `""` (empty string) as separator
48
- /// If the array is empty, returns empty string
48
+ /// If the array is empty, returns null
49
49
pub fn join ( value : & Value , args : & HashMap < String , Value > ) -> Result < Value > {
50
50
let arr = try_get_value ! ( "join" , "value" , Vec <Value >, value) ;
51
51
let sep = match args. get ( "sep" ) {
@@ -319,7 +319,7 @@ mod tests {
319
319
args. insert ( "n" . to_string ( ) , to_value ( 1 ) . unwrap ( ) ) ;
320
320
let result = nth ( & to_value ( & v) . unwrap ( ) , & args) ;
321
321
assert ! ( result. is_ok( ) ) ;
322
- assert_eq ! ( result. unwrap( ) , to_value ( "" ) . unwrap ( ) ) ;
322
+ assert_eq ! ( result. unwrap( ) , Value :: Null ) ;
323
323
}
324
324
325
325
#[ test]
@@ -335,7 +335,7 @@ mod tests {
335
335
336
336
let result = first ( & to_value ( & v) . unwrap ( ) , & HashMap :: new ( ) ) ;
337
337
assert ! ( result. is_ok( ) ) ;
338
- assert_eq ! ( result. ok( ) . unwrap( ) , to_value ( "" ) . unwrap ( ) ) ;
338
+ assert_eq ! ( result. ok( ) . unwrap( ) , Value :: Null ) ;
339
339
}
340
340
341
341
#[ test]
@@ -351,7 +351,7 @@ mod tests {
351
351
352
352
let result = last ( & to_value ( & v) . unwrap ( ) , & HashMap :: new ( ) ) ;
353
353
assert ! ( result. is_ok( ) ) ;
354
- assert_eq ! ( result. ok( ) . unwrap( ) , to_value ( "" ) . unwrap ( ) ) ;
354
+ assert_eq ! ( result. ok( ) . unwrap( ) , Value :: Null ) ;
355
355
}
356
356
357
357
#[ test]
0 commit comments