mirror of
https://github.com/dhruvasagar/vim-table-mode.git
synced 2025-11-08 11:03:47 -05:00
fixed the Average function and test case when spanning rows/columns
fixed a Vim error(E907) in Min/Max when comparing v:null to a float fixed CountE/CountNE/PercentE/PercentNE to support spanning rows/columns added test cases for all the new formula functions
This commit is contained in:
@@ -1,68 +1,104 @@
|
||||
" Private Functions {{{1
|
||||
function! s:TotalCells(list) "{{{2
|
||||
let result = 0
|
||||
for item in a:list
|
||||
if type(item) == type([])
|
||||
let result += s:TotalCells(item)
|
||||
else
|
||||
let result += 1
|
||||
endif
|
||||
endfor
|
||||
return result
|
||||
endfunction
|
||||
|
||||
function! s:Min(list) "{{{2
|
||||
let result = v:null
|
||||
let found = v:false
|
||||
let result = 0
|
||||
for item in a:list
|
||||
if empty(item)
|
||||
continue
|
||||
endif
|
||||
if type(item) == type(1) || type(item) == type(1.0)
|
||||
if result == v:null || item < result
|
||||
if found == v:false || item < result
|
||||
let found = v:true
|
||||
let result = item
|
||||
endif
|
||||
elseif type(item) == type('')
|
||||
let val = str2float(item)
|
||||
if result == v:null || val < result
|
||||
if found == v:false || val < result
|
||||
let found = v:true
|
||||
let result = val
|
||||
endif
|
||||
elseif type(item) == type([])
|
||||
let val = s:Sum(item)
|
||||
if result == v:null || val < result
|
||||
let val = s:Min(item)
|
||||
if found == v:false || val < result
|
||||
let found = v:true
|
||||
let result = val
|
||||
endif
|
||||
endif
|
||||
endfor
|
||||
return result == v:null ? 0 : result
|
||||
return result
|
||||
endfunction
|
||||
|
||||
function! s:Max(list) "{{{2
|
||||
let result = v:null
|
||||
let found = v:false
|
||||
let result = 0
|
||||
for item in a:list
|
||||
if empty(item)
|
||||
continue
|
||||
endif
|
||||
if type(item) == type(1) || type(item) == type(1.0)
|
||||
if result == v:null || item > result
|
||||
if found == v:false || item > result
|
||||
let found = v:true
|
||||
let result = item
|
||||
endif
|
||||
elseif type(item) == type('')
|
||||
let val = str2float(item)
|
||||
if result == v:null || val > result
|
||||
if found == v:false || val > result
|
||||
let found = v:true
|
||||
let result = val
|
||||
endif
|
||||
elseif type(item) == type([])
|
||||
let val = s:Sum(item)
|
||||
if result == v:null || val > result
|
||||
let val = s:Max(item)
|
||||
if found == v:false || val > result
|
||||
let found = v:true
|
||||
let result = val
|
||||
endif
|
||||
endif
|
||||
endfor
|
||||
return result == v:null ? 0 : result
|
||||
return result
|
||||
endfunction
|
||||
|
||||
function! s:CountE(list) "{{{2
|
||||
return len(filter(copy(a:list), {_,l -> empty(l)}))
|
||||
let result = 0
|
||||
for item in a:list
|
||||
if empty(item)
|
||||
let result += 1
|
||||
elseif type(item) == type([])
|
||||
let result += s:CountE(item)
|
||||
endif
|
||||
endfor
|
||||
return result
|
||||
endfunction
|
||||
|
||||
function! s:CountNE(list) "{{{2
|
||||
return len(a:list)-s:CountE(a:list)
|
||||
let result = 0
|
||||
for item in a:list
|
||||
if type(item) == type([])
|
||||
let result += s:CountNE(item)
|
||||
elseif !empty(item)
|
||||
let result += 1
|
||||
endif
|
||||
endfor
|
||||
return result
|
||||
endfunction
|
||||
|
||||
function! s:PercentE(list) "{{{2
|
||||
return (s:CountE(a:list)*100)/len(a:list)
|
||||
return (s:CountE(a:list)*100)/s:TotalCells(a:list)
|
||||
endfunction
|
||||
|
||||
function! s:PercentNE(list) "{{{2
|
||||
return (s:CountNE(a:list)*100)/len(a:list)
|
||||
return (s:CountNE(a:list)*100)/s:TotalCells(a:list)
|
||||
endfunction
|
||||
|
||||
function! s:Sum(list) "{{{2
|
||||
@@ -80,7 +116,7 @@ function! s:Sum(list) "{{{2
|
||||
endfunction
|
||||
|
||||
function! s:Average(list) "{{{2
|
||||
return s:Sum(a:list)/len(a:list)
|
||||
return s:Sum(a:list)/s:TotalCells(a:list)
|
||||
endfunction
|
||||
|
||||
function! s:AverageNE(list) "{{{2
|
||||
|
||||
3
t/fixtures/cell/counts.txt
Normal file
3
t/fixtures/cell/counts.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
| 1 | 2 | 3 |
|
||||
| | 0 | |
|
||||
| 4 | 5 | 6 |
|
||||
@@ -74,11 +74,93 @@ describe 'spreadsheet'
|
||||
call cursor(1,3)
|
||||
Expect tablemode#spreadsheet#Average('1:2') == 2.0
|
||||
Expect tablemode#spreadsheet#Average('1,1:1,2') == 1.5
|
||||
Expect tablemode#spreadsheet#Average('1,1:2,2') == 5.0
|
||||
Expect tablemode#spreadsheet#Average('1,1:2,2') == 2.5
|
||||
call cursor(2,7)
|
||||
Expect tablemode#spreadsheet#Average('1:2') == 3.0
|
||||
Expect tablemode#spreadsheet#Average('2,1:2,2') == 3.5
|
||||
end
|
||||
|
||||
it 'should return the min of cell range'
|
||||
call cursor(1,3)
|
||||
Expect tablemode#spreadsheet#Min('1:2') == 1.0
|
||||
Expect tablemode#spreadsheet#Min('1,1:1,2') == 1.0
|
||||
Expect tablemode#spreadsheet#Min('1,1:2,2') == 1.0
|
||||
call cursor(2,7)
|
||||
Expect tablemode#spreadsheet#Min('1:2') == 2.0
|
||||
Expect tablemode#spreadsheet#Min('2,1:2,2') == 3.0
|
||||
end
|
||||
|
||||
it 'should return the max of cell range'
|
||||
call cursor(1,3)
|
||||
Expect tablemode#spreadsheet#Max('1:2') == 3.0
|
||||
Expect tablemode#spreadsheet#Max('1,1:1,2') == 2.0
|
||||
Expect tablemode#spreadsheet#Max('1,1:2,2') == 4.0
|
||||
call cursor(2,7)
|
||||
Expect tablemode#spreadsheet#Max('1:2') == 4.0
|
||||
Expect tablemode#spreadsheet#Max('2,1:2,2') == 4.0
|
||||
end
|
||||
end
|
||||
|
||||
describe 'Count'
|
||||
before
|
||||
new
|
||||
read t/fixtures/cell/counts.txt
|
||||
end
|
||||
|
||||
it 'should return the count of empty cell range'
|
||||
call cursor(1,3)
|
||||
Expect tablemode#spreadsheet#CountE('1:3') == 1
|
||||
Expect tablemode#spreadsheet#CountE('1,1:1,3') == 0
|
||||
Expect tablemode#spreadsheet#CountE('2,1:2,3') == 2
|
||||
Expect tablemode#spreadsheet#CountE('1,1:3,3') == 2
|
||||
call cursor(3,11)
|
||||
Expect tablemode#spreadsheet#CountE('1:3') == 1
|
||||
Expect tablemode#spreadsheet#CountE('3,1:3,3') == 0
|
||||
end
|
||||
|
||||
it 'should return the count of not-empty cell range'
|
||||
call cursor(1,3)
|
||||
Expect tablemode#spreadsheet#CountNE('1:3') == 2
|
||||
Expect tablemode#spreadsheet#CountNE('1,1:1,3') == 3
|
||||
Expect tablemode#spreadsheet#CountNE('2,1:2,3') == 1
|
||||
Expect tablemode#spreadsheet#CountNE('1,1:3,3') == 7
|
||||
call cursor(3,11)
|
||||
Expect tablemode#spreadsheet#CountNE('1:3') == 2
|
||||
Expect tablemode#spreadsheet#CountNE('3,1:3,3') == 3
|
||||
end
|
||||
|
||||
it 'should return the percent count of empty cell range'
|
||||
call cursor(1,3)
|
||||
Expect tablemode#spreadsheet#PercentE('1:3') == 33
|
||||
Expect tablemode#spreadsheet#PercentE('1,1:1,3') == 0
|
||||
Expect tablemode#spreadsheet#PercentE('2,1:2,3') == 66
|
||||
Expect tablemode#spreadsheet#PercentE('1,1:3,3') == 22
|
||||
call cursor(3,11)
|
||||
Expect tablemode#spreadsheet#PercentE('1:3') == 33
|
||||
Expect tablemode#spreadsheet#PercentE('3,1:3,3') == 0
|
||||
end
|
||||
|
||||
it 'should return the percent count of not-empty cell range'
|
||||
call cursor(1,3)
|
||||
Expect tablemode#spreadsheet#PercentNE('1:3') == 66
|
||||
Expect tablemode#spreadsheet#PercentNE('1,1:1,3') == 100
|
||||
Expect tablemode#spreadsheet#PercentNE('2,1:2,3') == 33
|
||||
Expect tablemode#spreadsheet#PercentNE('1,1:3,3') == 77
|
||||
call cursor(3,11)
|
||||
Expect tablemode#spreadsheet#PercentNE('1:3') == 66
|
||||
Expect tablemode#spreadsheet#PercentNE('3,1:3,3') == 100
|
||||
end
|
||||
|
||||
it 'should return the average of not-empty cell range'
|
||||
call cursor(1,3)
|
||||
Expect tablemode#spreadsheet#AverageNE('1:3') == 2.5
|
||||
Expect tablemode#spreadsheet#AverageNE('1,1:1,3') == 2.0
|
||||
Expect tablemode#spreadsheet#AverageNE('2,1:2,3') == 0.0
|
||||
Expect tablemode#spreadsheet#AverageNE('1,1:3,3') == 3.0
|
||||
call cursor(3,11)
|
||||
Expect tablemode#spreadsheet#AverageNE('1:3') == 4.5
|
||||
Expect tablemode#spreadsheet#AverageNE('3,1:3,3') == 5.0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user