dff-util 1.3.7 → 1.3.9
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.
- package/README.md +1022 -698
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/main/api-call.d.ts +6 -0
- package/dist/main/api-call.js +84 -0
- package/dist/main/const-type.d.ts +1 -1
- package/dist/main/countries.d.ts +1 -1
- package/dist/main/countries.js +2 -2
- package/dist/main/util.d.ts +0 -2
- package/dist/main/util.js +2 -58
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -25,3 +25,4 @@ __exportStar(require("./main/const-messages"), exports);
|
|
|
25
25
|
__exportStar(require("./main/countries"), exports);
|
|
26
26
|
__exportStar(require("./main/languages"), exports);
|
|
27
27
|
__exportStar(require("./main/case-mapper"), exports);
|
|
28
|
+
__exportStar(require("./main/api-call"), exports);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { RequestBodyType } from './const-type';
|
|
2
|
+
/** Render an inline Handlebars template using the hosted HBS service. */
|
|
3
|
+
export declare function CallHbs(template: string, data: RequestBodyType | RequestBodyType[] | null | undefined, multi?: boolean): string | string[];
|
|
4
|
+
export declare const CallLangText: (data: string, source: string, target: string) => string;
|
|
5
|
+
/** Return the exchange rate for one unit of the source currency. */
|
|
6
|
+
export declare const CallCurrencyConvert: (from: string, to: string) => number;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CallCurrencyConvert = exports.CallLangText = void 0;
|
|
4
|
+
exports.CallHbs = CallHbs;
|
|
5
|
+
// All API calls are synchronous and require XMLHttpRequest support.
|
|
6
|
+
function requestText(url, label, body) {
|
|
7
|
+
if (typeof XMLHttpRequest === 'undefined') {
|
|
8
|
+
throw new Error(`${label} requires a browser with synchronous XMLHttpRequest support`);
|
|
9
|
+
}
|
|
10
|
+
const request = new XMLHttpRequest();
|
|
11
|
+
request.open(body === undefined ? 'GET' : 'POST', url, false);
|
|
12
|
+
if (body !== undefined)
|
|
13
|
+
request.setRequestHeader('Content-Type', 'application/json');
|
|
14
|
+
request.send(body === undefined ? null : JSON.stringify(body));
|
|
15
|
+
if (request.status < 200 || request.status >= 300) {
|
|
16
|
+
const message = request.responseText;
|
|
17
|
+
throw new Error(`${label} failed: ${request.status} ${request.statusText}${message ? `: ${message}` : ''}`);
|
|
18
|
+
}
|
|
19
|
+
return request.responseText;
|
|
20
|
+
}
|
|
21
|
+
/** Render an inline Handlebars template using the hosted HBS service. */
|
|
22
|
+
function CallHbs(template, data, multi = false) {
|
|
23
|
+
if (typeof template !== 'string') {
|
|
24
|
+
throw new Error('Invalid input: template must be a string');
|
|
25
|
+
}
|
|
26
|
+
// With no data, no request is made: return the template unchanged,
|
|
27
|
+
// or a single-item array when rendering in multi mode.
|
|
28
|
+
if (data === null || data === undefined) {
|
|
29
|
+
return multi ? [template] : template;
|
|
30
|
+
}
|
|
31
|
+
const isObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
32
|
+
if (!(Array.isArray(data) ? data.every(isObject) : isObject(data))) {
|
|
33
|
+
throw new Error('Invalid input: data must be a JSON object or an array of JSON objects');
|
|
34
|
+
}
|
|
35
|
+
if (multi && !Array.isArray(data)) {
|
|
36
|
+
throw new Error('Invalid input: data must be an array for multi rendering');
|
|
37
|
+
}
|
|
38
|
+
const text = requestText(`https://hbs.rndpro.in/${multi ? 'multi' : 'render'}`, 'HBS rendering', { template, data });
|
|
39
|
+
if (!multi)
|
|
40
|
+
return text;
|
|
41
|
+
const rendered = JSON.parse(text);
|
|
42
|
+
if (!Array.isArray(rendered) || !rendered.every((item) => typeof item === 'string')) {
|
|
43
|
+
throw new Error('Invalid HBS response: expected an array of strings');
|
|
44
|
+
}
|
|
45
|
+
return rendered;
|
|
46
|
+
}
|
|
47
|
+
const CallLangText = (data, source, target) => {
|
|
48
|
+
if (!data || !source || !target) {
|
|
49
|
+
throw new Error('Invalid input: data, source, or target is missing');
|
|
50
|
+
}
|
|
51
|
+
const src = source.split('-')[0]; // e.g., "en-US" -> "en"
|
|
52
|
+
const tgt = target.split('-')[0]; // e.g., "te-IN" -> "te"
|
|
53
|
+
if (!/^[a-zA-Z]{2}$/.test(src) || !/^[a-zA-Z]{2}$/.test(tgt)) {
|
|
54
|
+
throw new Error('Invalid language code format');
|
|
55
|
+
}
|
|
56
|
+
const encodedData = encodeURIComponent(data);
|
|
57
|
+
const url = `https://lingva.ml/api/v1/${src}/${tgt}/${encodedData}`;
|
|
58
|
+
const json = JSON.parse(requestText(url, 'Translation'));
|
|
59
|
+
if (json.error) {
|
|
60
|
+
throw new Error(`API error: ${json.error}`);
|
|
61
|
+
}
|
|
62
|
+
if (json.translation !== undefined && typeof json.translation !== 'string') {
|
|
63
|
+
throw new Error('Invalid translation response: expected a string');
|
|
64
|
+
}
|
|
65
|
+
return json.translation || '';
|
|
66
|
+
};
|
|
67
|
+
exports.CallLangText = CallLangText;
|
|
68
|
+
/** Return the exchange rate for one unit of the source currency. */
|
|
69
|
+
const CallCurrencyConvert = (from, to) => {
|
|
70
|
+
var _a;
|
|
71
|
+
if (!from || !to) {
|
|
72
|
+
throw new Error('Invalid input: from or to is missing');
|
|
73
|
+
}
|
|
74
|
+
if (from === to)
|
|
75
|
+
return 1;
|
|
76
|
+
const url = `https://api.frankfurter.dev/v1/latest?from=${encodeURIComponent(from)}&to=${encodeURIComponent(to)}`;
|
|
77
|
+
const json = JSON.parse(requestText(url, 'Currency conversion'));
|
|
78
|
+
const rate = (_a = json.rates) === null || _a === void 0 ? void 0 : _a[to];
|
|
79
|
+
if (typeof rate !== 'number' || !Number.isFinite(rate)) {
|
|
80
|
+
throw new Error('Invalid currency response: expected a numeric exchange rate');
|
|
81
|
+
}
|
|
82
|
+
return rate;
|
|
83
|
+
};
|
|
84
|
+
exports.CallCurrencyConvert = CallCurrencyConvert;
|
package/dist/main/countries.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { CountryType } from "./const-type";
|
|
2
|
-
export declare const
|
|
2
|
+
export declare const Countries: CountryType[];
|
package/dist/main/countries.js
CHANGED
package/dist/main/util.d.ts
CHANGED
|
@@ -11,9 +11,7 @@ export declare const DateTime24HrFormat: (time: string) => string;
|
|
|
11
11
|
export declare const DateTime12HrFormat: (timestamp: Date, showDate?: boolean, showTime?: boolean) => string;
|
|
12
12
|
export declare const DateAndTime: (dateString: string, timeString: string) => Date | null;
|
|
13
13
|
export declare const TimeAgo: (date: Date) => string;
|
|
14
|
-
export declare const LangText: (data: string, source: string, target: string) => Promise<any>;
|
|
15
14
|
export declare const LangCountryCode: (lang: string) => LangCountryType;
|
|
16
|
-
export declare const CurrencyConvert: (from: string, to: string) => Promise<number>;
|
|
17
15
|
export declare function EncodeBase64(input: string): string;
|
|
18
16
|
export declare function DecodeBase64(encoded: string): string;
|
|
19
17
|
export declare function EncodeURL(input: string): string;
|
package/dist/main/util.js
CHANGED
|
@@ -1,15 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.QueryCond = exports.
|
|
3
|
+
exports.QueryCond = exports.LangCountryCode = exports.TimeAgo = exports.DateAndTime = exports.DateTime12HrFormat = exports.DateTime24HrFormat = exports.TruncateText = exports.AppUUID4 = exports.AppAddDays = exports.AppDaysBack = exports.AppCode = exports.AppCodeByType = exports.AppUniqueCode = exports.AppRandomString = void 0;
|
|
13
4
|
exports.EncodeBase64 = EncodeBase64;
|
|
14
5
|
exports.DecodeBase64 = DecodeBase64;
|
|
15
6
|
exports.EncodeURL = EncodeURL;
|
|
@@ -158,33 +149,9 @@ const TimeAgo = (date) => {
|
|
|
158
149
|
return 'Just now';
|
|
159
150
|
};
|
|
160
151
|
exports.TimeAgo = TimeAgo;
|
|
161
|
-
const LangText = (data, source, target) => __awaiter(void 0, void 0, void 0, function* () {
|
|
162
|
-
if (!data || !source || !target) {
|
|
163
|
-
throw new Error('Invalid input: data, source, or target is missing');
|
|
164
|
-
}
|
|
165
|
-
const src = source.split('-')[0]; // e.g., "en-US" -> "en"
|
|
166
|
-
const tgt = target.split('-')[0]; // e.g., "te-IN" -> "te"
|
|
167
|
-
if (!/^[a-zA-Z]{2}$/.test(src) || !/^[a-zA-Z]{2}$/.test(tgt)) {
|
|
168
|
-
throw new Error('Invalid language code format');
|
|
169
|
-
}
|
|
170
|
-
const encodedData = encodeURIComponent(data);
|
|
171
|
-
const url = `https://lingva.ml/api/v1/${src}/${tgt}/${encodedData}`;
|
|
172
|
-
const res = yield fetch(url, {
|
|
173
|
-
method: 'GET',
|
|
174
|
-
});
|
|
175
|
-
if (!res.ok) {
|
|
176
|
-
throw new Error(`Translation failed: ${res.status} ${res.statusText}`);
|
|
177
|
-
}
|
|
178
|
-
const json = yield res.json();
|
|
179
|
-
if (json.error) {
|
|
180
|
-
throw new Error(`API error: ${json.error}`);
|
|
181
|
-
}
|
|
182
|
-
return json.translation || '';
|
|
183
|
-
});
|
|
184
|
-
exports.LangText = LangText;
|
|
185
152
|
const LangCountryCode = (lang) => {
|
|
186
153
|
const countryCode = lang.split('-')[1];
|
|
187
|
-
const country = countries_1.
|
|
154
|
+
const country = countries_1.Countries.find((c) => c.code === countryCode) || {};
|
|
188
155
|
const language = languages_1.languages.find((l) => l.lang === lang) || {};
|
|
189
156
|
return {
|
|
190
157
|
lang: lang,
|
|
@@ -199,29 +166,6 @@ const LangCountryCode = (lang) => {
|
|
|
199
166
|
};
|
|
200
167
|
};
|
|
201
168
|
exports.LangCountryCode = LangCountryCode;
|
|
202
|
-
const CurrencyConvert = (from, to) => __awaiter(void 0, void 0, void 0, function* () {
|
|
203
|
-
if (!from || !to) {
|
|
204
|
-
throw new Error('Invalid input: from or to is missing');
|
|
205
|
-
}
|
|
206
|
-
if (from === to) {
|
|
207
|
-
return 1;
|
|
208
|
-
}
|
|
209
|
-
try {
|
|
210
|
-
const url = `https://api.frankfurter.app/latest?from=${from}&to=${to}`;
|
|
211
|
-
const res = yield fetch(url, {
|
|
212
|
-
method: 'GET',
|
|
213
|
-
});
|
|
214
|
-
if (!res.ok) {
|
|
215
|
-
throw new Error(`Currency conversion failed: ${res.status} ${res.statusText}`);
|
|
216
|
-
}
|
|
217
|
-
const json = yield res.json();
|
|
218
|
-
return json.rates[to];
|
|
219
|
-
}
|
|
220
|
-
catch (error) {
|
|
221
|
-
throw new Error(`Currency conversion failed: ${error}`);
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
exports.CurrencyConvert = CurrencyConvert;
|
|
225
169
|
function EncodeBase64(input) {
|
|
226
170
|
return btoa(input);
|
|
227
171
|
}
|