You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was looking to use node-sales-tax in a small expense tracking project and I wanted to automatically set the sales tax base on the user location. While reading the documentation, I found that the country code and province code are needed to get the sales tax, but I did not see any helpers for converting a location into these codes. After searching for a bit, I found the answer from the U.S. Customs and Border Patrol of all places. They provide an excel sheet that has the country and province code for 1637 provinces.
I wrote some code to download the xlsx and convert it to a json file ( I am a novice, so I apologize for all issues with the code):
constXLSX=require('xlsx')constfs=require('fs')constaxios=require('axios')constpath=require('path')letcurrentDir=process.cwd()letxlsxFileName=path.join(currentDir,'./international-province-codes.xls')letjsonFileName=path.join(currentDir,'./codes.json')// excel sheet has 3 columnsconstcolumns={countryCode:'A',provinceCode:'B',provinceName:'C'}asyncfunctiongetFile(){// customs and border patrol provides a xlsx file with all international state/province codesletxlsxUrl='https://www.cbp.gov/sites/default/files/documents/codes_7.xls'constresponse=awaitaxios.get(xlsxUrl,{responseType:'stream',})awaitresponse.data.pipe(fs.createWriteStream(xlsxFileName))returnxlsxFileName}asyncfunctionparseXlsx(){letsheettry{sheet=XLSX.readFile(awaitgetFile())}catch(err){if(err.errno===-4058){console.error('Excel sheet not found')process.exit()}}sheet=sheet.Sheets.Sheet1// remove unnecessary keysletvalidKeys=Object.keys(sheet).filter(key=>// keys are a letter and a number e.g 'A1'key&&sheet[key]?.w&&(key[0]==columns.countryCode||key[0]==columns.provinceCode||key[0]==columns.provinceName))// convert validKeys into json objectletprovinceCodes={}validKeys.forEach(key=>{letrowNumber=parseInt(key.substring(1))letcountryCell=columns.countryCode+rowNumberletprovinceCell=columns.provinceCode+rowNumberletnameCell=columns.provinceName+rowNumber// even after filtering to make sure the cells exist// the cell values are sometimes null ???letcountryCode=sheet[countryCell]?.wletprovinceCode=sheet[provinceCell]?.wletprovinceName=sheet[nameCell]?.w// remove diacritical/accent characters found solution here: // https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascriptprovinceName=provinceName?.normalize("NFD").replace(/\p{Diacritic}/gu,"").toLowerCase()provinceCodes[provinceName]={ countryCode,provinceCode}})// write to file fs.writeFileSync(jsonFileName,JSON.stringify(provinceCodes,null,2))}parseXlsx()
The resulting json should allow users to to use states/provinces to get both country and province codes
The text was updated successfully, but these errors were encountered:
I was looking to use node-sales-tax in a small expense tracking project and I wanted to automatically set the sales tax base on the user location. While reading the documentation, I found that the country code and province code are needed to get the sales tax, but I did not see any helpers for converting a location into these codes. After searching for a bit, I found the answer from the U.S. Customs and Border Patrol of all places. They provide an excel sheet that has the country and province code for 1637 provinces.
I wrote some code to download the xlsx and convert it to a json file ( I am a novice, so I apologize for all issues with the code):
The resulting json should allow users to to use states/provinces to get both country and province codes
The text was updated successfully, but these errors were encountered: