<commit_before> # Place all the behaviors and hooks related to the matching controller here.
 # All this logic will automatically be available in application.js.
 
$ ->
  'use strict';
  class Calculator 
    constructor: (@exchange_rate) ->
      # accept @exchange_rate argument and set it as an instance var
      
      # event handler for 2 selectors and 2 events
      $('#btc_calculator_input, #usd_calculator_input').on 'change keyup', (evt) =>
        
        # determine which type of input has fired
        type = if evt.currentTarget.id is 'btc_calculator_input' then 'btc' else 'usd'
        
        # calculate exchange rate
        @.calculate_exchange evt.currentTarget.value, type
        
      # event handler for radio btns
      $("input[name=bitcoin_calculator]:radio").change (evt) =>
        
        # determine which radio btn is selected
        type = evt.currentTarget.value
        
        # update the DOM accordingly
        if type is 'btc'
          $('#usd_calculator_input').closest('.form-group').addClass 'hide' unless $('#usd_calculator_input').closest('.form-group').hasClass 'hide' ;
          $('#usd_value').closest('p').removeClass 'hide' if $('#usd_value').closest('p').hasClass 'hide';
          
          $('#btc_calculator_input').closest('.form-group').removeClass 'hide' if $('#btc_calculator_input').closest('.form-group').hasClass 'hide' ;
          $('#btc_value').closest('p').addClass 'hide' unless $('#btc_value').closest('p').hasClass 'hide';
        else if type is 'usd'
          $('#usd_calculator_input').closest('.form-group').removeClass 'hide' if $('#usd_calculator_input').closest('.form-group').hasClass 'hide' ;
          $('#usd_value').closest('p').addClass 'hide' unless $('#usd_value').closest('p').hasClass 'hide';
          
          $('#btc_calculator_input').closest('.form-group').addClass 'hide' unless $('#btc_calculator_input').closest('.form-group').hasClass 'hide' ;
          $('#btc_value').closest('p').removeClass 'hide' if $('#btc_value').closest('p').hasClass 'hide';
    calculate_exchange: (num, type) ->
      # calculate exchange value depending on which input field has fired
      final_rate = if type is 'btc' then @.exchange_rate * num else num / @.exchange_rate
      
      # update the DOM accordingly
      if type is 'btc'
        $('#btc_value').text(num)
        $('#usd_value').text(final_rate.toLocaleString())
        $('#usd_calculator_input').val(final_rate.toLocaleString())
      else if type is 'usd'
        $('#usd_value').text(num)
        $('#btc_value').text(final_rate.toFixed(8))
        $('#btc_calculator_input').val(final_rate.toFixed(8))
      
  calculator = new Calculator $('body').data 'exchange_rate'
<commit_msg>Format value to 2 decimal places before casting to string.<commit_after> # Place all the behaviors and hooks related to the matching controller here.
 # All this logic will automatically be available in application.js.
 
$ ->
  'use strict';
  class Calculator 
    constructor: (@exchange_rate) ->
      # accept @exchange_rate argument and set it as an instance var
      
      # event handler for 2 selectors and 2 events
      $('#btc_calculator_input, #usd_calculator_input').on 'change keyup', (evt) =>
        
        # determine which type of input has fired
        type = if evt.currentTarget.id is 'btc_calculator_input' then 'btc' else 'usd'
        
        # calculate exchange rate
        @.calculate_exchange evt.currentTarget.value, type
        
      # event handler for radio btns
      $("input[name=bitcoin_calculator]:radio").change (evt) =>
        
        # determine which radio btn is selected
        type = evt.currentTarget.value
        
        # update the DOM accordingly
        if type is 'btc'
          $('#usd_calculator_input').closest('.form-group').addClass 'hide' unless $('#usd_calculator_input').closest('.form-group').hasClass 'hide' ;
          $('#usd_value').closest('p').removeClass 'hide' if $('#usd_value').closest('p').hasClass 'hide';
          
          $('#btc_calculator_input').closest('.form-group').removeClass 'hide' if $('#btc_calculator_input').closest('.form-group').hasClass 'hide' ;
          $('#btc_value').closest('p').addClass 'hide' unless $('#btc_value').closest('p').hasClass 'hide';
        else if type is 'usd'
          $('#usd_calculator_input').closest('.form-group').removeClass 'hide' if $('#usd_calculator_input').closest('.form-group').hasClass 'hide' ;
          $('#usd_value').closest('p').addClass 'hide' unless $('#usd_value').closest('p').hasClass 'hide';
          
          $('#btc_calculator_input').closest('.form-group').addClass 'hide' unless $('#btc_calculator_input').closest('.form-group').hasClass 'hide' ;
          $('#btc_value').closest('p').removeClass 'hide' if $('#btc_value').closest('p').hasClass 'hide';
    calculate_exchange: (num, type) ->
      # calculate exchange value depending on which input field has fired
      final_rate = if type is 'btc' then @.exchange_rate * num else num / @.exchange_rate
      
      # update the DOM accordingly
      if type is 'btc'
        $('#btc_value').text(num)
        $('#usd_value').text(final_rate.toFixed(2).toLocaleString())
        $('#usd_calculator_input').val(final_rate.toFixed(2).toLocaleString())
      else if type is 'usd'
        $('#usd_value').text(num)
        $('#btc_value').text(final_rate.toFixed(8))
        $('#btc_calculator_input').val(final_rate.toFixed(8))
      
  calculator = new Calculator $('body').data 'exchange_rate'

{'fraction_non_alphanumeric': 0.1534448641842058, 'fraction_numerical': 0.001978773160640403, 'mean_word_length': 3.1773102930127726, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4606229', 'n_tokens_mistral': 1667, 'n_tokens_neox': 1659, 'n_words': 387}