-
Notifications
You must be signed in to change notification settings - Fork 188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add convert method #17
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
544f6de
feat(dinero): Add convert method
sarahdayan 1fa8ed4
build(eslint): Add browser env so window objects are testable
sarahdayan ec74bed
test(helpers): Add tests
sarahdayan d1fa576
test(forex): Add tests
sarahdayan 874a1e6
test(dinero): Add tests
sarahdayan cdefb7a
fix(dinero#convert): Use better term in error message
sarahdayan 7555d0b
docs(dinero#convert): Add usage examples
sarahdayan c6bc4c2
fix(dinero#convert): Cast rate into float in case API returns a string
sarahdayan 1a1dd7a
docs(dinero#convert): Add examples of forex APIs
sarahdayan 3ccf8dd
Merge branch 'master' into forex
sarahdayan 8069ef9
Merge branch 'master' into forex
sarahdayan b454a1b
fix(dinero-polyfilled): Add promise polyfill
sarahdayan c224e08
refactor(forex): Make service name more generic (forex is only one ma…
sarahdayan 34ee820
fix(dinero#convert): Cover more API use cases
sarahdayan 6d45165
docs(dinero#convert): Make it more obvious that example API is not a …
sarahdayan 6da424e
Merge branch 'develop' into forex
sarahdayan 8b9eb4b
Merge branch 'develop' into forex
sarahdayan 0d197aa
fix(dinero#convert): Change ambiguous property name
sarahdayan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { getJSON, flattenObject } from './helpers' | ||
|
||
export default function CurrencyConverter(options) { | ||
/* istanbul ignore next */ | ||
const mergeTags = (string = '', tags) => { | ||
for (const tag in tags) { | ||
string = string.replace(`{{${tag}}}`, tags[tag]) | ||
} | ||
return string | ||
} | ||
|
||
return { | ||
/** | ||
* Returns the exchange rate. | ||
* @param {String} from - The base currency. | ||
* @param {String} to - The destination currency. | ||
* @return {Promise} | ||
* @ignore | ||
*/ | ||
getExchangeRate(from, to) { | ||
return getJSON(mergeTags(options.endpoint, { from, to }), { | ||
headers: options.headers | ||
}).then( | ||
data => | ||
flattenObject(data)[mergeTags(options.propertyPath, { from, to })] | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import CurrencyConverter from '../../src/services/currency-converter' | ||
import { getJSON } from '../../src/services/helpers' | ||
|
||
jest.mock('../../src/services/helpers', () => | ||
Object.assign(require.requireActual('../../src/services/helpers'), { | ||
getJSON: jest.fn() | ||
}) | ||
) | ||
|
||
const options = { | ||
endpoint: 'https://yourexchangerates.api/latest?base={{from}}', | ||
propertyPath: 'rates.{{to}}', | ||
headers: { | ||
'user-key': 'xxxxxxxxx' | ||
}, | ||
roundingMode: 'HALF_UP' | ||
} | ||
|
||
describe('CurrencyConverter', () => { | ||
describe('#getExchangeRate()', () => { | ||
test('should return a rate as a number when input and output currencies are valid', async () => { | ||
getJSON.mockResolvedValue({ | ||
base: 'USD', | ||
date: '2018-03-31', | ||
rates: { | ||
EUR: 0.81162 | ||
} | ||
}) | ||
await expect( | ||
CurrencyConverter(options).getExchangeRate('USD', 'EUR') | ||
).resolves.toEqual(0.81162) | ||
}) | ||
test('should throw when API returns an error', async () => { | ||
getJSON.mockRejectedValue(new Error()) | ||
await expect( | ||
CurrencyConverter(options).getExchangeRate('USD', 'EUR') | ||
).rejects.toThrow() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if there is an explicit browser-support contract, but any reason not to use
fetch
here? Seems like any browser that supportsObject#assign
would also supportfetch
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to be backward-compatible at least down to IE11 (I shipped polyfilled versions today in v1.2.0 as I realized there were some holes). I picked core-js for polyfills (the same as Babel uses), and unfortunately, it doesn't provide one for
Fetch
.I want to avoid adding dependencies for each feature, so let's wait for this one and refactor later when it's time to drop IE altogether.