Skip to content
This repository was archived by the owner on Apr 2, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions odometer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ FORMAT_MARK_HTML = '<span class="odometer-formatting-mark"></span>'
# This is just the default, it can also be set as options.format.
DIGIT_FORMAT = '(,ddd).dd'

FORMAT_PARSER = /^\(?([^)]*)\)?(?:(.)(d+))?$/
FORMAT_PARSER = /^\(?([^)]*)\)?(?:(.)(D*)(d*))?$/

# What is our target framerate?
FRAMERATE = 30
Expand Down Expand Up @@ -88,9 +88,6 @@ truncate = (val) ->
else
Math.floor(val)

fractionalPart = (val) ->
val - round(val)

_jQueryWrapped = false
do wrapJQuery = ->
return if _jQueryWrapped
Expand Down Expand Up @@ -223,11 +220,13 @@ class Odometer
if not parsed
throw new Error "Odometer: Unparsable digit format"

[repeating, radix, fractional] = parsed[1..3]
[repeating, radix, fractional1, fractional2] = parsed[1..4]

fractional = fractional1?.length or 0

precision = fractional?.length or 0
precision = fractional + fractional2?.length or 0

@format = {repeating, radix, precision}
@format = {repeating, radix, precision, fractional}

render: (value=@value) ->
@stopWatchingMutations()
Expand Down Expand Up @@ -283,12 +282,21 @@ class Odometer
else
@addSpacer valueDigit
else
wholePart = not @format.precision or not fractionalPart(value) or false
for digit in value.toString().split('').reverse()
if digit is '.'
wholePart = true

@addDigit digit, wholePart
v = Math.abs value
fractionalCount = Math.max @format.fractional, @getFractionalDigitCount v
if fractionalCount
v = v * Math.pow(10, fractionalCount)

i = 0
while v > 0
@addDigit (v % 10).toString(), i >= fractionalCount
v = Math.floor(v / 10);
i += 1
if i == fractionalCount
@addDigit '.', true

if value < 0
@addDigit '-', true

return

Expand Down Expand Up @@ -432,7 +440,7 @@ class Odometer
animateSlide: (newValue) ->
oldValue = @value

fractionalCount = @getFractionalDigitCount oldValue, newValue
fractionalCount = Math.max @format.fractional, @getFractionalDigitCount oldValue, newValue

if fractionalCount
newValue = newValue * Math.pow(10, fractionalCount)
Expand Down
44 changes: 25 additions & 19 deletions odometer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading