From 21411832243c0a59c8e15630db77d55c719d2a77 Mon Sep 17 00:00:00 2001 From: Antoni Kepinski Date: Tue, 25 Aug 2020 23:08:24 +0200 Subject: [PATCH] Update JSDOC for better DX --- src/cashify.ts | 23 ++++++++++++++++++++--- src/convert.ts | 19 +++++++++++++++---- src/utils/parser.ts | 8 ++++++-- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/cashify.ts b/src/cashify.ts index 8fd02bf..5a80e4e 100644 --- a/src/cashify.ts +++ b/src/cashify.ts @@ -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) { } /** - * @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): number { // If provided `amount` is a string, use parsing diff --git a/src/convert.ts b/src/convert.ts index 752e5c5..7b9ea30 100644 --- a/src/convert.ts +++ b/src/convert.ts @@ -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') { diff --git a/src/utils/parser.ts b/src/utils/parser.ts index 02d2c53..d1daa55 100644 --- a/src/utils/parser.ts +++ b/src/utils/parser.ts @@ -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;