Skip to content

Commit

Permalink
Update JSDOC for better DX
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoni Kepinski committed Aug 25, 2020
1 parent d0855cf commit 2141183
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
23 changes: 20 additions & 3 deletions src/cashify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,29 @@ import convert from './convert';
import parse from './utils/parser';

export default class Cashify {
/**
* @constructor
* @param {Object} [options] Conversion options.
*/
constructor(public readonly options: Partial<Options>) { }

/**
* @param amount Amount of money you want to convert.
* @param options Conversion options.
* @return Conversion result.
* Function, which converts currencies based on provided rates.
*
* @param {number | string} amount - Amount of money you want to convert.
* @param {Object} [options] - Conversion options.
* @return {number} Conversion result.
*
* @example
* const rates = {
* GBP: 0.92,
* EUR: 1.00,
* USD: 1.12
* };
*
* const cashify = new Cashify({base: 'EUR', rates});
*
* cashify.convert(10, {from: 'EUR', to: 'GBP'}); //=> 9.2
*/
convert(amount: number | string, options?: Partial<Options>): number {
// If provided `amount` is a string, use parsing
Expand Down
19 changes: 15 additions & 4 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@ import {Options} from './lib/options';
import parse from './utils/parser';

/**
* @param amount Amount of money you want to convert.
* @param options Conversion options.
* @return Conversion result.
*/
* Function, which converts currencies based on provided rates.
*
* @param {number | string} amount - Amount of money you want to convert.
* @param {Object} options - Conversion options.
* @return {number} Conversion result.
*
* @example
* const rates = {
* GBP: 0.92,
* EUR: 1.00,
* USD: 1.12
* };
*
* convert(10, {from: 'EUR', to: 'GBP', base: 'EUR', rates}); //=> 9.2
*/
export default function convert(amount: number | string, {from, to, base, rates}: Options): number {
// If provided `amount` is a string, use parsing
if (typeof amount === 'string') {
Expand Down
8 changes: 6 additions & 2 deletions src/utils/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ interface Options {

/**
* Expression parser
* @param expression Expression you want to parse, ex. `10 usd to pln` or `€1.23 eur`
* @return Object with parsing results
*
* @param {string} expression - Expression you want to parse, ex. `10 usd to pln` or `€1.23 eur`
* @return {Object} Object with parsing results
*
* @example
* parse('10 EUR to GBP'); //=> {amount: 10, from: 'EUR', to: 'GBP'}
*/
export default function parse(expression: string): Options {
const amount = Number.parseFloat(expression.replace(/[^\d-.]/g, '')) || undefined;
Expand Down

0 comments on commit 2141183

Please sign in to comment.