@cloudparker/moldex.js 4.1.15 → 4.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -14,10 +14,6 @@ export declare class PeriodEnum {
|
|
|
14
14
|
static NEXT_YEAR: number;
|
|
15
15
|
static DATE: number;
|
|
16
16
|
}
|
|
17
|
-
export type FirestoreTimestamp = {
|
|
18
|
-
seconds: number;
|
|
19
|
-
nanoseconds: number;
|
|
20
|
-
};
|
|
21
17
|
export declare function getDateWith(yearChange: number): Date;
|
|
22
18
|
export declare function getDateTime(date: string | Date | any): number;
|
|
23
19
|
export declare function momentDiff({ startDate, endDate, unit }: {
|
|
@@ -37,9 +33,6 @@ export declare function getPeriodDates(period: PeriodEnum, currentDate?: Date):
|
|
|
37
33
|
export declare function padTo2Digits(num: number): string;
|
|
38
34
|
export declare function millisToTimeString(milliseconds: number): string;
|
|
39
35
|
export declare function timestampToMillis(timestamp: any): number;
|
|
40
|
-
export declare function timestampToDate(timestamp: FirestoreTimestamp): Date;
|
|
41
|
-
export declare function timestampToDateString(timestamp: FirestoreTimestamp): string;
|
|
42
|
-
export declare function timestampToAgo(timestamp: FirestoreTimestamp): string;
|
|
43
36
|
export declare function dateToTimestamp(date: Date): {
|
|
44
37
|
seconds: number;
|
|
45
38
|
nanoseconds: number;
|
|
@@ -50,3 +43,9 @@ export declare function millisToDate(millis: number): Date;
|
|
|
50
43
|
export declare function dateFormat(date: Date, format?: 'MM-DD-YYYY hh:mm a' | 'MM-DD-YYYY' | 'YYYY-MM-DD' | 'YYYY-MM-DDTHH:MM' | string): string;
|
|
51
44
|
export declare function isDateBetween(date: Date, startDate: Date, endDate: Date): boolean;
|
|
52
45
|
export declare function toDate(value: any): Date;
|
|
46
|
+
/**
|
|
47
|
+
* Formats a duration in seconds to mm:ss or hh:mm:ss format
|
|
48
|
+
* @param seconds - The duration in seconds
|
|
49
|
+
* @returns Formatted duration string (mm:ss if less than an hour, hh:mm:ss if an hour or more)
|
|
50
|
+
*/
|
|
51
|
+
export declare function formatDuration(seconds: number): string;
|
|
@@ -141,21 +141,6 @@ export function timestampToMillis(timestamp) {
|
|
|
141
141
|
return (timestamp.seconds * 1000) + (timestamp.nanoseconds / 1000000);
|
|
142
142
|
}
|
|
143
143
|
}
|
|
144
|
-
export function timestampToDate(timestamp) {
|
|
145
|
-
if (timestamp && timestamp.seconds) {
|
|
146
|
-
return new Date((timestamp.seconds * 1000) + (timestamp.nanoseconds / 1000000));
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
export function timestampToDateString(timestamp) {
|
|
150
|
-
if (timestamp && timestamp.seconds) {
|
|
151
|
-
return moment(new Date(timestamp.seconds * 1000)).format('MM-DD-YYYY hh:mm a');
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
export function timestampToAgo(timestamp) {
|
|
155
|
-
if (timestamp && timestamp.seconds) {
|
|
156
|
-
return moment(new Date(timestamp.seconds * 1000)).fromNow();
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
144
|
export function dateToTimestamp(date) {
|
|
160
145
|
return { seconds: date.getTime() / 1000, nanoseconds: 0 };
|
|
161
146
|
}
|
|
@@ -198,9 +183,26 @@ export function toDate(value) {
|
|
|
198
183
|
else if (typeof value == 'string') {
|
|
199
184
|
result = moment(value).toDate();
|
|
200
185
|
}
|
|
201
|
-
else if (value.seconds) {
|
|
202
|
-
result = timestampToDate(value);
|
|
203
|
-
}
|
|
204
186
|
}
|
|
205
187
|
return result;
|
|
206
188
|
}
|
|
189
|
+
/**
|
|
190
|
+
* Formats a duration in seconds to mm:ss or hh:mm:ss format
|
|
191
|
+
* @param seconds - The duration in seconds
|
|
192
|
+
* @returns Formatted duration string (mm:ss if less than an hour, hh:mm:ss if an hour or more)
|
|
193
|
+
*/
|
|
194
|
+
export function formatDuration(seconds) {
|
|
195
|
+
if (seconds < 0) {
|
|
196
|
+
seconds = 0;
|
|
197
|
+
}
|
|
198
|
+
const hours = Math.floor(seconds / 3600);
|
|
199
|
+
const minutes = Math.floor((seconds % 3600) / 60);
|
|
200
|
+
const secs = Math.floor(seconds % 60);
|
|
201
|
+
const mm = minutes.toString().padStart(2, '0');
|
|
202
|
+
const ss = secs.toString().padStart(2, '0');
|
|
203
|
+
if (hours > 0) {
|
|
204
|
+
const hh = hours.toString().padStart(2, '0');
|
|
205
|
+
return `${hh}:${mm}:${ss}`;
|
|
206
|
+
}
|
|
207
|
+
return `${mm}:${ss}`;
|
|
208
|
+
}
|
|
@@ -43,6 +43,28 @@ export declare enum CurrencySymbols {
|
|
|
43
43
|
VND = "\u20AB",
|
|
44
44
|
ZAR = "R"
|
|
45
45
|
}
|
|
46
|
+
export interface CurrencyRates {
|
|
47
|
+
[currencyCode: string]: number;
|
|
48
|
+
}
|
|
49
|
+
export interface CurrencyApiResponse {
|
|
50
|
+
result: string;
|
|
51
|
+
provider: string;
|
|
52
|
+
documentation: string;
|
|
53
|
+
terms_of_use: string;
|
|
54
|
+
time_last_update_unix: number;
|
|
55
|
+
time_last_update_utc: string;
|
|
56
|
+
time_next_update_unix: number;
|
|
57
|
+
time_next_update_utc: string;
|
|
58
|
+
time_eol_unix: number;
|
|
59
|
+
base_code: string;
|
|
60
|
+
rates: CurrencyRates;
|
|
61
|
+
}
|
|
62
|
+
export interface CurrencyData {
|
|
63
|
+
baseCode: string;
|
|
64
|
+
rates: CurrencyRates;
|
|
65
|
+
lastUpdate: string;
|
|
66
|
+
nextUpdate: string;
|
|
67
|
+
}
|
|
46
68
|
/**
|
|
47
69
|
* Converts a number into words based on the Indian numbering system.
|
|
48
70
|
* Supports up to Crore level and works for numbers up to 99 Crore.
|
|
@@ -89,3 +111,45 @@ export declare function toCurrency(value?: number, symbol?: string): string;
|
|
|
89
111
|
* @returns A string formatted with commas as per the Indian numbering system.
|
|
90
112
|
*/
|
|
91
113
|
export declare function formatIndianCurrency(amount: number | string): string;
|
|
114
|
+
/**
|
|
115
|
+
* Fetches currency exchange rates for a given base currency
|
|
116
|
+
* @param baseCurrency - The base currency code (e.g., 'USD', 'EUR', 'INR')
|
|
117
|
+
* @param forceRefresh - Force a refresh even if cached data exists (default: false)
|
|
118
|
+
* @returns Currency data with exchange rates
|
|
119
|
+
* @throws Error if the API request fails
|
|
120
|
+
*/
|
|
121
|
+
export declare function getCurrencyRates(baseCurrency?: string, forceRefresh?: boolean): Promise<CurrencyData>;
|
|
122
|
+
/**
|
|
123
|
+
* Converts an amount from one currency to another
|
|
124
|
+
* @param amount - The amount to convert
|
|
125
|
+
* @param fromCurrency - The source currency code
|
|
126
|
+
* @param toCurrency - The target currency code
|
|
127
|
+
* @returns The converted amount
|
|
128
|
+
*/
|
|
129
|
+
export declare function convertCurrency(amount: number, fromCurrency: string, toCurrency: string): Promise<number>;
|
|
130
|
+
/**
|
|
131
|
+
* Gets a specific exchange rate between two currencies
|
|
132
|
+
* @param fromCurrency - The source currency code
|
|
133
|
+
* @param toCurrency - The target currency code
|
|
134
|
+
* @returns The exchange rate
|
|
135
|
+
*/
|
|
136
|
+
export declare function getExchangeRate(fromCurrency: string, toCurrency: string): Promise<number>;
|
|
137
|
+
/**
|
|
138
|
+
* Clears the currency cache for a specific currency or all currencies
|
|
139
|
+
* @param baseCurrency - Optional base currency to clear, if not provided clears all
|
|
140
|
+
*/
|
|
141
|
+
export declare function clearCurrencyCache(baseCurrency?: string): void;
|
|
142
|
+
/**
|
|
143
|
+
* Gets the cached currency data without fetching
|
|
144
|
+
* @param baseCurrency - The base currency code
|
|
145
|
+
* @returns Cached currency data or null if not cached
|
|
146
|
+
*/
|
|
147
|
+
export declare function getCachedCurrencyRates(baseCurrency: string): CurrencyData | null;
|
|
148
|
+
/**
|
|
149
|
+
* Formats a currency amount with the appropriate symbol and formatting
|
|
150
|
+
* @param amount - The amount to format
|
|
151
|
+
* @param currencyCode - The currency code (e.g., 'USD', 'EUR', 'INR')
|
|
152
|
+
* @param locale - The locale for formatting (defaults to 'en-US')
|
|
153
|
+
* @returns Formatted currency string
|
|
154
|
+
*/
|
|
155
|
+
export declare function formatCurrency(amount: number, currencyCode: string, locale?: string): string;
|
|
@@ -47,6 +47,8 @@ export var CurrencySymbols;
|
|
|
47
47
|
CurrencySymbols["VND"] = "\u20AB";
|
|
48
48
|
CurrencySymbols["ZAR"] = "R";
|
|
49
49
|
})(CurrencySymbols || (CurrencySymbols = {}));
|
|
50
|
+
const CURRENCY_API_BASE = 'https://open.er-api.com/v6/latest';
|
|
51
|
+
const currencyCache = new Map();
|
|
50
52
|
/**
|
|
51
53
|
* Converts a number into words based on the Indian numbering system.
|
|
52
54
|
* Supports up to Crore level and works for numbers up to 99 Crore.
|
|
@@ -138,3 +140,156 @@ export function formatIndianCurrency(amount) {
|
|
|
138
140
|
}
|
|
139
141
|
return otherDigits + (otherDigits ? "," : "") + lastThreeDigits + "." + decimalPart;
|
|
140
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
* Checks if cached currency data is still valid
|
|
145
|
+
* @param cachedData - The cached currency data
|
|
146
|
+
* @returns true if cache is valid, false otherwise
|
|
147
|
+
*/
|
|
148
|
+
function isCacheValid(cachedData) {
|
|
149
|
+
const nowUnix = Math.floor(Date.now() / 1000);
|
|
150
|
+
return cachedData.nextUpdateUnix > nowUnix;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Fetches currency exchange rates for a given base currency
|
|
154
|
+
* @param baseCurrency - The base currency code (e.g., 'USD', 'EUR', 'INR')
|
|
155
|
+
* @param forceRefresh - Force a refresh even if cached data exists (default: false)
|
|
156
|
+
* @returns Currency data with exchange rates
|
|
157
|
+
* @throws Error if the API request fails
|
|
158
|
+
*/
|
|
159
|
+
export async function getCurrencyRates(baseCurrency = 'USD', forceRefresh = false) {
|
|
160
|
+
const cacheKey = baseCurrency.toUpperCase();
|
|
161
|
+
// Check cache first
|
|
162
|
+
if (!forceRefresh) {
|
|
163
|
+
const cached = currencyCache.get(cacheKey);
|
|
164
|
+
if (cached && isCacheValid(cached)) {
|
|
165
|
+
console.log(`[currency.service] Using cached rates for ${cacheKey}`);
|
|
166
|
+
return {
|
|
167
|
+
baseCode: cached.baseCode,
|
|
168
|
+
rates: cached.rates,
|
|
169
|
+
lastUpdate: cached.lastUpdate,
|
|
170
|
+
nextUpdate: cached.nextUpdate
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
try {
|
|
175
|
+
console.log(`[currency.service] Fetching fresh rates for ${cacheKey}`);
|
|
176
|
+
const response = await fetch(`${CURRENCY_API_BASE}/${cacheKey}`);
|
|
177
|
+
if (!response.ok) {
|
|
178
|
+
throw new Error(`Currency API request failed: ${response.status} ${response.statusText}`);
|
|
179
|
+
}
|
|
180
|
+
const data = await response.json();
|
|
181
|
+
if (data.result !== 'success') {
|
|
182
|
+
throw new Error('Currency API returned unsuccessful result');
|
|
183
|
+
}
|
|
184
|
+
const currencyData = {
|
|
185
|
+
baseCode: data.base_code,
|
|
186
|
+
rates: data.rates,
|
|
187
|
+
lastUpdate: data.time_last_update_utc,
|
|
188
|
+
nextUpdate: data.time_next_update_utc,
|
|
189
|
+
nextUpdateUnix: data.time_next_update_unix
|
|
190
|
+
};
|
|
191
|
+
// Store in cache
|
|
192
|
+
currencyCache.set(cacheKey, currencyData);
|
|
193
|
+
return {
|
|
194
|
+
baseCode: currencyData.baseCode,
|
|
195
|
+
rates: currencyData.rates,
|
|
196
|
+
lastUpdate: currencyData.lastUpdate,
|
|
197
|
+
nextUpdate: currencyData.nextUpdate
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
console.error('[currency.service] Failed to fetch currency rates:', error);
|
|
202
|
+
// If fetch fails, try to return stale cache as fallback
|
|
203
|
+
const cached = currencyCache.get(cacheKey);
|
|
204
|
+
if (cached) {
|
|
205
|
+
console.warn('[currency.service] Returning stale cached data due to fetch error');
|
|
206
|
+
return {
|
|
207
|
+
baseCode: cached.baseCode,
|
|
208
|
+
rates: cached.rates,
|
|
209
|
+
lastUpdate: cached.lastUpdate,
|
|
210
|
+
nextUpdate: cached.nextUpdate
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
throw error;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Converts an amount from one currency to another
|
|
218
|
+
* @param amount - The amount to convert
|
|
219
|
+
* @param fromCurrency - The source currency code
|
|
220
|
+
* @param toCurrency - The target currency code
|
|
221
|
+
* @returns The converted amount
|
|
222
|
+
*/
|
|
223
|
+
export async function convertCurrency(amount, fromCurrency, toCurrency) {
|
|
224
|
+
const data = await getCurrencyRates(fromCurrency);
|
|
225
|
+
const rate = data.rates[toCurrency.toUpperCase()];
|
|
226
|
+
if (!rate) {
|
|
227
|
+
throw new Error(`Exchange rate not found for ${toCurrency}`);
|
|
228
|
+
}
|
|
229
|
+
return amount * rate;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Gets a specific exchange rate between two currencies
|
|
233
|
+
* @param fromCurrency - The source currency code
|
|
234
|
+
* @param toCurrency - The target currency code
|
|
235
|
+
* @returns The exchange rate
|
|
236
|
+
*/
|
|
237
|
+
export async function getExchangeRate(fromCurrency, toCurrency) {
|
|
238
|
+
const data = await getCurrencyRates(fromCurrency);
|
|
239
|
+
const rate = data.rates[toCurrency.toUpperCase()];
|
|
240
|
+
if (!rate) {
|
|
241
|
+
throw new Error(`Exchange rate not found for ${toCurrency}`);
|
|
242
|
+
}
|
|
243
|
+
return rate;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Clears the currency cache for a specific currency or all currencies
|
|
247
|
+
* @param baseCurrency - Optional base currency to clear, if not provided clears all
|
|
248
|
+
*/
|
|
249
|
+
export function clearCurrencyCache(baseCurrency) {
|
|
250
|
+
if (baseCurrency) {
|
|
251
|
+
currencyCache.delete(baseCurrency.toUpperCase());
|
|
252
|
+
console.log(`[currency.service] Cleared cache for ${baseCurrency.toUpperCase()}`);
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
currencyCache.clear();
|
|
256
|
+
console.log('[currency.service] Cleared all currency cache');
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Gets the cached currency data without fetching
|
|
261
|
+
* @param baseCurrency - The base currency code
|
|
262
|
+
* @returns Cached currency data or null if not cached
|
|
263
|
+
*/
|
|
264
|
+
export function getCachedCurrencyRates(baseCurrency) {
|
|
265
|
+
const cached = currencyCache.get(baseCurrency.toUpperCase());
|
|
266
|
+
if (!cached)
|
|
267
|
+
return null;
|
|
268
|
+
return {
|
|
269
|
+
baseCode: cached.baseCode,
|
|
270
|
+
rates: cached.rates,
|
|
271
|
+
lastUpdate: cached.lastUpdate,
|
|
272
|
+
nextUpdate: cached.nextUpdate
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Formats a currency amount with the appropriate symbol and formatting
|
|
277
|
+
* @param amount - The amount to format
|
|
278
|
+
* @param currencyCode - The currency code (e.g., 'USD', 'EUR', 'INR')
|
|
279
|
+
* @param locale - The locale for formatting (defaults to 'en-US')
|
|
280
|
+
* @returns Formatted currency string
|
|
281
|
+
*/
|
|
282
|
+
export function formatCurrency(amount, currencyCode, locale = 'en-US') {
|
|
283
|
+
try {
|
|
284
|
+
return new Intl.NumberFormat(locale, {
|
|
285
|
+
style: 'currency',
|
|
286
|
+
currency: currencyCode.toUpperCase(),
|
|
287
|
+
minimumFractionDigits: 2,
|
|
288
|
+
maximumFractionDigits: 2
|
|
289
|
+
}).format(amount);
|
|
290
|
+
}
|
|
291
|
+
catch (error) {
|
|
292
|
+
console.error('[currency.service] Failed to format currency:', error);
|
|
293
|
+
return `${currencyCode.toUpperCase()} ${amount.toFixed(2)}`;
|
|
294
|
+
}
|
|
295
|
+
}
|