@@ -74,6 +74,44 @@ describe('filters/array', function () {
74
74
return test ( tpl , { arr : [ a , b , c ] } , 'Alice Bob Carol' )
75
75
} )
76
76
} )
77
+ describe ( 'sum' , ( ) => {
78
+ it ( 'should support sum with no args' , function ( ) {
79
+ const ages = [ 21 , null , - 4 , '4.5' , 13.25 , undefined , 0 ]
80
+ return test ( '{{ages | sum}}' , { ages } , '34.75' )
81
+ } )
82
+ it ( 'should support sum with property' , function ( ) {
83
+ const ages = [ 21 , null , - 4 , '4.5' , 13.25 , undefined , 0 ] . map ( x => ( { age : x } ) )
84
+ return test ( '{{ages | sum: "age"}}' , { ages } , '34.75' )
85
+ } )
86
+ it ( 'should support sum with nested property' , function ( ) {
87
+ const ages = [ 21 , null , - 4 , '4.5' , 13.25 , undefined , 0 ] . map ( x => ( { age : { first : x } } ) )
88
+ return test ( '{{ages | sum: "age.first"}}' , { ages } , '34.75' )
89
+ } )
90
+ it ( 'should support non-array input' , function ( ) {
91
+ const age = 21.5
92
+ return test ( '{{age | sum}}' , { age } , '21.5' )
93
+ } )
94
+ it ( 'should coerce missing property to zero' , function ( ) {
95
+ const ages = [ { qty : 1 } , { qty : 2 , cnt : 3 } , { cnt : 4 } ]
96
+ return test ( '{{ages | sum}} {{ages | sum: "cnt"}} {{ages | sum: "other"}}' , { ages } , '0 7 0' )
97
+ } )
98
+ it ( 'should coerce indexable non-map values to zero' , function ( ) {
99
+ const input = [ 1 , 'foo' , { quantity : 3 } ]
100
+ return test ( '{{input | sum}}' , { input } , '1' )
101
+ } )
102
+ it ( 'should coerce unindexable values to zero' , function ( ) {
103
+ const input = [ 1 , null , { quantity : 2 } ]
104
+ return test ( '{{input | sum}}' , { input } , '1' )
105
+ } )
106
+ it ( 'should coerce true to 1' , function ( ) {
107
+ const input = [ 1 , true , null , { quantity : 2 } ]
108
+ return test ( '{{input | sum}}' , { input } , '2' )
109
+ } )
110
+ it ( 'should not support nested arrays' , function ( ) {
111
+ const ages = [ 1 , [ 2 , [ 3 , 4 ] ] ]
112
+ return test ( '{{ages | sum}}' , { ages } , '1' )
113
+ } )
114
+ } )
77
115
describe ( 'compact' , ( ) => {
78
116
it ( 'should compact array' , function ( ) {
79
117
const posts = [ { category : 'foo' } , { category : 'bar' } , { foo : 'bar' } ]
0 commit comments