Skip to content

Commit

Permalink
Add currency-converter
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Dec 11, 2024
1 parent 123942b commit 7febbbf
Show file tree
Hide file tree
Showing 11 changed files with 1,551 additions and 4 deletions.
1 change: 1 addition & 0 deletions backend/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default ({ mode }: { mode: string }) => {
':bookcars-types': path.resolve(__dirname, '../packages/bookcars-types'),
':bookcars-helper': path.resolve(__dirname, '../packages/bookcars-helper'),
':disable-react-devtools': path.resolve(__dirname, '../packages/disable-react-devtools'),
':currency-converter': path.resolve(__dirname, '../packages/currency-converter'),
},
},

Expand Down
1 change: 1 addition & 0 deletions frontend/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default ({ mode }: { mode: string }) => {
':bookcars-types': path.resolve(__dirname, '../packages/bookcars-types'),
':bookcars-helper': path.resolve(__dirname, '../packages/bookcars-helper'),
':disable-react-devtools': path.resolve(__dirname, '../packages/disable-react-devtools'),
':currency-converter': path.resolve(__dirname, '../packages/currency-converter'),
},
},

Expand Down
16 changes: 16 additions & 0 deletions packages/bookcars-helper/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as bookcarsTypes from ':bookcars-types'
import CurrencyConverter, { CurrencyCode } from ':currency-converter'

/**
* Format a number.
Expand Down Expand Up @@ -336,6 +337,21 @@ export const calculateTotalPrice = (car: bookcarsTypes.Car, from: Date, to: Date
return _price
}

/**
* Convert price from a given currency to another.
*
* @async
* @param {number} amount
* @param {CurrencyCode} from
* @param {CurrencyCode} to
* @returns {Promise<number>}
*/
export const convertPrice = async (amount: number, from: CurrencyCode, to: CurrencyCode): Promise<number> => {
const cc = new CurrencyConverter({ from, to, amount })
const res = await cc.convert()
return res
}

/**
* Check whether language is french
*
Expand Down
2 changes: 1 addition & 1 deletion packages/bookcars-helper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"type": "module",
"scripts": {
"build": "rimraf ./index.js ./index.d.ts && tsc"
"build": "rimraf ./tsconfig.tsbuildinfo ./index.js ./index.d.ts && tsc --build --verbose"
},
"author": "Akram El Assas",
"license": "ISC",
Expand Down
6 changes: 4 additions & 2 deletions packages/bookcars-helper/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
"paths": {
":bookcars-types": ["../bookcars-types"]
":bookcars-types": ["../bookcars-types"],
":currency-converter": ["../currency-converter"]
}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
Expand Down Expand Up @@ -113,6 +114,7 @@
"node_modules"
],
"references": [
{ "path": "../bookcars-types" }
{ "path": "../bookcars-types" },
{ "path": "../currency-converter" }
]
}
2 changes: 1 addition & 1 deletion packages/bookcars-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"type": "module",
"scripts": {
"build": "rimraf ./index.js ./index.d.ts && tsc"
"build": "rimraf ./tsconfig.tsbuildinfo ./index.js ./index.d.ts && tsc --build --verbose"
},
"keywords": [],
"author": "",
Expand Down
8 changes: 8 additions & 0 deletions packages/currency-converter/check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import CurrencyConverter from './index.js'

const amount = 100
const currencyConverter = new CurrencyConverter({ from: 'USD', to: 'EUR', amount })

const res = await currencyConverter.convert()

console.log(`${amount} USD = ${res} EUR`)
Loading

0 comments on commit 7febbbf

Please sign in to comment.