Moved to App.Utils methods.
This commit is contained in:
parent
74de283afa
commit
700ec9347b
3 changed files with 84 additions and 22 deletions
|
@ -123,11 +123,6 @@ class App extends Spine.Controller
|
||||||
@view: (name) ->
|
@view: (name) ->
|
||||||
template = ( params = {} ) ->
|
template = ( params = {} ) ->
|
||||||
|
|
||||||
s = ( num, digits ) ->
|
|
||||||
while num.toString().length < digits
|
|
||||||
num = '0' + num
|
|
||||||
num
|
|
||||||
|
|
||||||
# define print name helper
|
# define print name helper
|
||||||
params.P = ( object, attribute_name ) ->
|
params.P = ( object, attribute_name ) ->
|
||||||
App.viewPrint( object, attribute_name )
|
App.viewPrint( object, attribute_name )
|
||||||
|
@ -137,8 +132,8 @@ class App extends Spine.Controller
|
||||||
return '' if !time
|
return '' if !time
|
||||||
|
|
||||||
timeObject = new Date(time)
|
timeObject = new Date(time)
|
||||||
d = s( timeObject.getDate(), 2 )
|
d = App.Utils.formatTime( timeObject.getDate(), 2 )
|
||||||
m = s( timeObject.getMonth() + 1, 2 )
|
m = App.Utils.formatTime( timeObject.getMonth() + 1, 2 )
|
||||||
y = timeObject.getFullYear()
|
y = timeObject.getFullYear()
|
||||||
"#{y}-#{m}-#{d}"
|
"#{y}-#{m}-#{d}"
|
||||||
|
|
||||||
|
@ -147,26 +142,17 @@ class App extends Spine.Controller
|
||||||
return '' if !time
|
return '' if !time
|
||||||
|
|
||||||
timeObject = new Date(time)
|
timeObject = new Date(time)
|
||||||
d = s( timeObject.getDate(), 2 )
|
d = App.Utils.formatTime( timeObject.getDate(), 2 )
|
||||||
m = s( timeObject.getMonth() + 1, 2 )
|
m = App.Utils.formatTime( timeObject.getMonth() + 1, 2 )
|
||||||
y = timeObject.getFullYear()
|
y = timeObject.getFullYear()
|
||||||
S = s( timeObject.getSeconds(), 2 )
|
S = App.Utils.formatTime( timeObject.getSeconds(), 2 )
|
||||||
M = s( timeObject.getMinutes(), 2 )
|
M = App.Utils.formatTime( timeObject.getMinutes(), 2 )
|
||||||
H = s( timeObject.getHours(), 2 )
|
H = App.Utils.formatTime( timeObject.getHours(), 2 )
|
||||||
"#{y}-#{m}-#{d} #{H}:#{M}:#{S}"
|
"#{y}-#{m}-#{d} #{H}:#{M}:#{S}"
|
||||||
|
|
||||||
# define decimal format helper
|
# define decimal format helper
|
||||||
params.decimal = ( data, positions = 2 ) ->
|
params.decimal = ( data, positions = 2 ) ->
|
||||||
return '' if !data
|
App.Utils.decimal(data, positions)
|
||||||
|
|
||||||
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)}"
|
|
||||||
|
|
||||||
# define translation helper
|
# define translation helper
|
||||||
params.T = ( item, args... ) ->
|
params.T = ( item, args... ) ->
|
||||||
|
|
|
@ -547,3 +547,34 @@ class App.Utils
|
||||||
else
|
else
|
||||||
size = size + ' Bytes'
|
size = size + ' Bytes'
|
||||||
size
|
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
|
||||||
|
|
|
@ -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 )
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue