study notes
  • Introduction
  • Oct 14, 2016
  • Oct 22, 2016
  • Oct 27
  • Nov1
  • j2ee context root(4.19.2017)
  • June 20, 2017(bTrade debug record)
  • Sep 5, 2017
  • Jan 4, 2018
  • Jan 5, 2018Deploy war file to Tomcat
  • Jan 13,2018, hibernate config
  • conflict caused by PR
  • using another project's class
  • intellij cannot resolve the symbols in target folder
  • keyboard symbol
  • Oct 31, 2018
  • tax build construction simplify &effecticent
  • line ending
  • memory leak
  • Serializable
  • To learn
  • What happens when you type 'google.com' into a browser and press Enter?
  • security scan for software
Powered by GitBook
On this page

Was this helpful?

tax build construction simplify &effecticent

You can also build the object with values in-line, like:

  let taxObj = {
    type: 'TAX_' + taxRateBrackets[i].taxCode,
    amount: {
      value: taxRateBrackets[i].bracketAmount
    }
  }

It also might be more beneficial to separate the conditional / object building logic into a separate (non-exported) method that does the mapping. Like this:

function mapTaxResponse (countryCode, taxRateBracket, currencyCode) {
  if (hasVatTax(countryCode, taxRateBracket)) {
    return getVatTax(taxRateBracket, currencyCode)
  } else {
    return getNonVatTax(taxRateBracket, currencyCode)
  }
}

function hasVatTax (countryCode, taxRateBracket) {
  if (!vatcountryCodes.includes(countryCode)) {
    return false
  }

  let taxRateAmount = _.get(taxRateBracket, 'taxRateAmount')
  let taxRateId = _.get(taxRateBracket, 'taxRateId')

  return (taxRateAmount !== null 
&
&
 taxRateId !== null)
}

function getVatTax (taxRateBracket, currencyCode) {
  return {
    type: `TAX_${taxRateBracket.taxCode}`,
    amount: {
      code: currencyCode
    },
    taxRate: {
      amount: {
        value: taxRateBracket.taxRateAmount
      }
    },
    taxRecord: taxRateBracket.taxRateId
  }
}

function getNonVatTax (taxRateBracket, currencyCode) {
  return {
    type: `TAX_${taxRateBracket.taxCode},
      amount: {}
  }
}

And then the main function can be simplified to just:

function taxBuilder (rq, taxRateBrackets) {
  return taxRateBrackets.map(taxRateBracket =>
 mapTaxResponse(rq.countryCode, taxRateBracket, rq.currencyCode))
}
PreviousOct 31, 2018Nextline ending

Last updated 5 years ago

Was this helpful?