Add type definitions for features added between Dinero.js v1.4 and v1.6. (#38856)

* Accept promises in ExchangeRatesApiOptions' endpoint property.

* Accept promises in ExchangeRatesApiOptions' endpoint property.

https://github.com/sarahdayan/dinero.js/releases/tag/v1.6.0

* Update dinero.js version.

* Add toJson() alias to Dinero.

https://github.com/sarahdayan/dinero.js/releases/tag/v1.5.0

* Correct code formatting.

* Assert that promises can be used in Dinero globalExchangeRatesApi.

* Assert that the Dinero toJson() alias returns a Dinero object.
This commit is contained in:
Jørgen Vatle
2019-10-15 10:09:43 -07:00
committed by Andrew Branch
parent 5d1074409c
commit ba3797e984
2 changed files with 44 additions and 29 deletions
+42 -28
View File
@@ -10,22 +10,35 @@ Dinero.globalFormat = '0,0 dollar';
Dinero.globalRoundingMode = 'HALF_UP';
Dinero.globalFormatRoundingMode = '0,0 dollar';
Dinero.globalExchangeRatesApi = {
endpoint: 'https://yourexchangerates.api/latest?base={{from}}',
propertyPath: 'data.rates.{{to}}',
headers: {
'user-key': 'xxxxxxxxx',
},
endpoint: 'https://yourexchangerates.api/latest?base={{from}}',
propertyPath: 'data.rates.{{to}}',
headers: {
'user-key': 'xxxxxxxxx',
},
};
Dinero.globalExchangeRatesApi = {
endpoint: 'https://yourexchangerates.api/latest?base={{from}}',
propertyPath: 'data.rates.{{to}}',
roundingMode: 'HALF_UP',
endpoint: 'https://yourexchangerates.api/latest?base={{from}}',
propertyPath: 'data.rates.{{to}}',
roundingMode: 'HALF_UP',
};
Dinero.globalExchangeRatesApi = {
endpoint: new Promise((resolve) =>
resolve({
data: {
rates: {
USD: 1.337,
},
},
}),
),
propertyPath: 'data.rates.{{to}}',
roundingMode: 'HALF_UP',
};
Dinero();
Dinero({ amount: 1 });
Dinero({ currency: 'USD' });
Dinero({ precision: 2 });
Dinero({amount: 1});
Dinero({currency: 'USD'});
Dinero({precision: 2});
let number: number;
let string: string;
@@ -40,8 +53,8 @@ string = dinero.getCurrency();
string = dinero.setLocale('fr-FR').getLocale();
number = dinero.convertPrecision(4).getPrecision();
number = dinero.convertPrecision(4, 'HALF_DOWN').getPrecision();
dinero = dinero.add(Dinero({ amount: 104545, precision: 4 }));
dinero = dinero.subtract(Dinero({ amount: 104545, precision: 4 }));
dinero = dinero.add(Dinero({amount: 104545, precision: 4}));
dinero = dinero.subtract(Dinero({amount: 104545, precision: 4}));
dinero = dinero.multiply(2.00125);
dinero = dinero.multiply(2.00125, 'HALF_UP');
dinero = dinero.divide(2);
@@ -50,32 +63,33 @@ dinero = dinero.percentage(50);
dineroArr = dinero.allocate([50, 50]);
dinero.convert('EUR').then(d => (dinero = d));
dinero.convert('XBT', {
endpoint: 'https://yourexchangerates.api/latest?base={{from}}',
propertyPath: 'data.rates.{{to}}',
headers: {
'user-key': 'xxxxxxxxx',
},
roundingMode: 'HALF_UP',
endpoint: 'https://yourexchangerates.api/latest?base={{from}}',
propertyPath: 'data.rates.{{to}}',
headers: {
'user-key': 'xxxxxxxxx',
},
roundingMode: 'HALF_UP',
});
boolean = dinero.equalsTo(Dinero({ amount: 500, currency: 'EUR' }));
boolean = dinero.lessThan(Dinero({ amount: 800 }));
boolean = dinero.lessThanOrEqual(Dinero({ amount: 800 }));
boolean = dinero.greaterThan(Dinero({ amount: 800 }));
boolean = dinero.greaterThanOrEqual(Dinero({ amount: 800 }));
boolean = dinero.equalsTo(Dinero({amount: 500, currency: 'EUR'}));
boolean = dinero.lessThan(Dinero({amount: 800}));
boolean = dinero.lessThanOrEqual(Dinero({amount: 800}));
boolean = dinero.greaterThan(Dinero({amount: 800}));
boolean = dinero.greaterThanOrEqual(Dinero({amount: 800}));
boolean = dinero.isZero();
boolean = dinero.isPositive();
boolean = dinero.isNegative();
boolean = dinero.hasSubUnits();
boolean = dinero.hasCents();
boolean = dinero.hasSameCurrency(Dinero({ amount: 1000, currency: 'EUR' }));
boolean = dinero.hasSameAmount(Dinero({ amount: 1000, currency: 'EUR' }));
boolean = dinero.hasSameCurrency(Dinero({amount: 1000, currency: 'EUR'}));
boolean = dinero.hasSameAmount(Dinero({amount: 1000, currency: 'EUR'}));
string = dinero.toFormat('0,0 dollar');
string = dinero.toFormat('0,0 dollar', 'HALF_EVEN');
number = dinero.toUnit();
number = dinero.toRoundedUnit(1);
number = dinero.toRoundedUnit(1, 'HALF_EVEN');
dineroObject = dinero.toObject();
dineroObject = dinero.toJson();
dineroArr = Dinero.normalizePrecision([
Dinero({ amount: 100, precision: 2 }),
Dinero({ amount: 1000, precision: 3 }),
Dinero({amount: 100, precision: 2}),
Dinero({amount: 1000, precision: 3}),
]);
+2 -1
View File
@@ -61,6 +61,7 @@ declare namespace DineroFactory {
toUnit(): number;
toRoundedUnit(digits: number, roundingMode?: RoundingMode): number;
toObject(): DineroObject;
toJson(): DineroObject;
}
type RoundingMode =
@@ -72,7 +73,7 @@ declare namespace DineroFactory {
| 'HALF_AWAY_FROM_ZERO';
interface ExchangeRatesApiOptions {
endpoint: string;
endpoint: string | Promise<{[key: string]: any}>;
propertyPath: string;
headers?: { [header: string]: string };
roundingMode?: RoundingMode;