From 700ec9347b46a75f569c405565f021469c1225cf Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 2 Oct 2015 23:41:41 +0200 Subject: [PATCH] Moved to App.Utils methods. --- app/assets/javascripts/app/index.coffee | 30 ++++--------- .../javascripts/app/lib/app_post/utils.coffee | 31 +++++++++++++ public/assets/tests/html-utils.js | 45 +++++++++++++++++++ 3 files changed, 84 insertions(+), 22 deletions(-) diff --git a/app/assets/javascripts/app/index.coffee b/app/assets/javascripts/app/index.coffee index 415898cba..39e35c252 100644 --- a/app/assets/javascripts/app/index.coffee +++ b/app/assets/javascripts/app/index.coffee @@ -123,11 +123,6 @@ class App extends Spine.Controller @view: (name) -> template = ( params = {} ) -> - s = ( num, digits ) -> - while num.toString().length < digits - num = '0' + num - num - # define print name helper params.P = ( object, attribute_name ) -> App.viewPrint( object, attribute_name ) @@ -137,8 +132,8 @@ class App extends Spine.Controller return '' if !time timeObject = new Date(time) - d = s( timeObject.getDate(), 2 ) - m = s( timeObject.getMonth() + 1, 2 ) + d = App.Utils.formatTime( timeObject.getDate(), 2 ) + m = App.Utils.formatTime( timeObject.getMonth() + 1, 2 ) y = timeObject.getFullYear() "#{y}-#{m}-#{d}" @@ -147,26 +142,17 @@ class App extends Spine.Controller return '' if !time timeObject = new Date(time) - d = s( timeObject.getDate(), 2 ) - m = s( timeObject.getMonth() + 1, 2 ) + d = App.Utils.formatTime( timeObject.getDate(), 2 ) + m = App.Utils.formatTime( timeObject.getMonth() + 1, 2 ) y = timeObject.getFullYear() - S = s( timeObject.getSeconds(), 2 ) - M = s( timeObject.getMinutes(), 2 ) - H = s( timeObject.getHours(), 2 ) + S = App.Utils.formatTime( timeObject.getSeconds(), 2 ) + M = App.Utils.formatTime( timeObject.getMinutes(), 2 ) + H = App.Utils.formatTime( timeObject.getHours(), 2 ) "#{y}-#{m}-#{d} #{H}:#{M}:#{S}" # define decimal format helper params.decimal = ( data, positions = 2 ) -> - return '' if !data - - result = data.toString().match(/^(.+?)\.(.+?)$/) - if !result || !result[2] - return "#{data}." + s( 0, positions ).toString() - length = result[2].toString().length - diff = positions - length - if diff > 0 - return "#{result[1]}." + s( result[2], positions ).toString() - "#{result[1]}.#{result[2].substr(0,positions)}" + App.Utils.decimal(data, positions) # define translation helper params.T = ( item, args... ) -> diff --git a/app/assets/javascripts/app/lib/app_post/utils.coffee b/app/assets/javascripts/app/lib/app_post/utils.coffee index 724571ede..0c4ad90e0 100644 --- a/app/assets/javascripts/app/lib/app_post/utils.coffee +++ b/app/assets/javascripts/app/lib/app_post/utils.coffee @@ -547,3 +547,34 @@ class App.Utils else size = size + ' Bytes' size + + # format decimal + @decimal: (data, positions = 2) -> + + # input validation + return data if data is '' + return data if data.match(/[A-z]/) + + format = ( num, digits ) -> + while num.toString().length < digits + num = num + '0' + num + + result = data.toString().match(/^(.+?)\.(.+?)$/) + + # add .00 + if !result || !result[2] + return "#{data}." + format( 0, positions ).toString() + length = result[2].toString().length + diff = positions - length + + # check length, add .00 + return "#{result[1]}." + format( result[2], positions ).toString() if diff > 0 + + # check length, remove longer positions + "#{result[1]}.#{result[2].substr(0,positions)}" + + @formatTime: (num, digits) -> + while num.toString().length < digits + num = '0' + num + num diff --git a/public/assets/tests/html-utils.js b/public/assets/tests/html-utils.js index c9b055084..52d1932e6 100644 --- a/public/assets/tests/html-utils.js +++ b/public/assets/tests/html-utils.js @@ -1208,4 +1208,49 @@ test( "check form diff", function() { }); +// check decimal format +test( "check decimal format", function() { + + var string = '123' + var result = '123.00' + var verify = App.Utils.decimal( string ) + equal( verify, result, string ) + + string = '0.6' + result = '0.60' + verify = App.Utils.decimal( string ) + equal( verify, result, string ) + + string = '111111.6' + result = '111111.60' + verify = App.Utils.decimal( string ) + equal( verify, result, string ) + + string = '111111.622' + result = '111111.62' + verify = App.Utils.decimal( string ) + equal( verify, result, string ) + + string = 'abc.6' + result = 'abc.6' + verify = App.Utils.decimal( string ) + equal( verify, result, string ) + +}); + +// check formatTime format +test( "check formatTime format", function() { + + var string = '123' + var result = '123' + var verify = App.Utils.formatTime( string, 0 ) + equal( verify, result, string ) + + string = '6' + result = '06' + verify = App.Utils.formatTime( string, 2 ) + equal( verify, result, string ) + +}); + } \ No newline at end of file