Skip to content
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 18 commits into from
May 6, 2018
Merged

Add convert method #17

merged 18 commits into from
May 6, 2018

Conversation

sarahdayan
Copy link
Collaborator

This PR adds a convert method to Dinero, allowing for users to convert Dinero objects from one currency to another.

Users need to plug their own forex API to retrieve exchange rates. The convert method queries a Forex service which calls the API and returns a promise with the right exchange rate for the destination currency. Then, convert creates a new Dinero object with the converted amount and new currency, and returns it within a promise.

@sarahdayan sarahdayan changed the base branch from develop to master April 28, 2018 01:36
@sarahdayan sarahdayan changed the base branch from master to develop April 28, 2018 01:37
setXHRHeaders(request, options.headers)
request.send()
})
}

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 supports Object#assign would also support fetch.

Copy link
Collaborator Author

@sarahdayan sarahdayan Apr 29, 2018

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.

encodeURIComponent(object[key])
].join('')
}, '')
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar question: URLSearchParams? No Edge 16 support, but pretty good support otherwise. Might be able to use just URL which does have better support.

Copy link
Collaborator Author

@sarahdayan sarahdayan Apr 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

URL isn't polyfilled in core-js yet, let's use it when it is.

@sarahdayan
Copy link
Collaborator Author

sarahdayan commented May 1, 2018

@scotttrinh (and/or anyone else) Changed a few things and made the method more API-proof. Would love a final review before merge if possible 🙂

Overview of the meaningful changes:

  • renamed the service; "forex" is only one market, not a generic name;
  • simplified the options, fewer things but more flexibility and fits more APIs;

@sarahdayan sarahdayan added the enhancement New feature or request label May 4, 2018
@@ -28,16 +28,33 @@ export const Defaults = {
* @property {String} globalFormat - The global format for new Dinero objects (see {@link module:Dinero~toFormat toFormat} for format).
* @property {String} globalRoundingMode - The global rounding mode for new Dinero objects (see {@link module:Dinero~multiply multiply} or {@link module:Dinero~divide divide} for format).
* @property {String} globalFormatRoundingMode - The global rounding mode to format new Dinero objects (see {@link module:Dinero~toFormat toFormat} or {@link module:Dinero~toRoundedUnit toRoundedUnit} for format).
* @property {String} globalExchangeRatesApi.endpoint - The global exchange rates API endpoint for new Dinero objects (see {@link module:Dinero~convert convert} for format).
* @property {String} globalExchangeRatesApi.JSONPath - The global exchange rates API JSON path for new Dinero objects (see {@link module:Dinero~convert convert} for format).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it might be a bit misleading to call this JSONPath since it doesn't seem to actually be JSONPath?

Lodash has a similar functionality which takes arrays into account which we might consider using here so that there is a well-understood/tested interface.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some testing and you can access arrays by using the index 'b.c.1', so that works for me, but I'm still not sure we should call it JSONPath just because of the ambiguity.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're totally right about the name ambiguity. I'll go for propertyPath.

Copy link

@scotttrinh scotttrinh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it! 🎉

@sarahdayan
Copy link
Collaborator Author

@scotttrinh Cool! Merging it now, deploying it probably later next week. Thanks for the review!

@sarahdayan sarahdayan merged commit 65f5035 into develop May 6, 2018
@sarahdayan sarahdayan deleted the forex branch May 6, 2018 17:50
@sarahdayan
Copy link
Collaborator Author

🎉 This PR is included in version 1.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants