dff-util 1.1.6 → 1.1.8
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 +306 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/main/const-type.d.ts +39 -0
- package/dist/main/countries.d.ts +2 -0
- package/dist/main/countries.js +1955 -0
- package/dist/main/languages.d.ts +2 -0
- package/dist/main/languages.js +1669 -0
- package/dist/main/util.d.ts +4 -0
- package/dist/main/util.js +87 -12
- package/dist/tests/lang.d.ts +0 -0
- package/dist/tests/lang.js +3 -0
- package/dist/tests/util.d.ts +1 -0
- package/dist/tests/util.js +33 -0
- package/package.json +1 -1
- /package/dist/main/{regexr.d.ts → reg-exr.d.ts} +0 -0
- /package/dist/main/{regexr.js → reg-exr.js} +0 -0
package/dist/main/util.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { LangCountryType } from "./const-type";
|
|
1
2
|
export declare const AppRandomString: (length: number, charset: string) => string;
|
|
2
3
|
export declare const AppUniqueCode: () => string;
|
|
3
4
|
export declare const AppCodeByType: (name: string, type?: null) => string;
|
|
@@ -5,3 +6,6 @@ export declare const AppCode: (name: string) => string | null;
|
|
|
5
6
|
export declare const AppDaysBack: (date: Date, backValue: number, isDays?: boolean) => Date;
|
|
6
7
|
export declare const AppAddDays: (date: Date, addDays: number, isDays?: boolean) => Date;
|
|
7
8
|
export declare const AppUUID4: () => string;
|
|
9
|
+
export declare const LangText: (data: string, source: string, target: string) => Promise<any>;
|
|
10
|
+
export declare const CurrencyConvert: (from: string, to: string) => Promise<number>;
|
|
11
|
+
export declare const LangCountryCode: (lang: string) => LangCountryType;
|
package/dist/main/util.js
CHANGED
|
@@ -1,8 +1,19 @@
|
|
|
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
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AppUUID4 = exports.AppAddDays = exports.AppDaysBack = exports.AppCode = exports.AppCodeByType = exports.AppUniqueCode = exports.AppRandomString = void 0;
|
|
12
|
+
exports.LangCountryCode = exports.CurrencyConvert = exports.LangText = exports.AppUUID4 = exports.AppAddDays = exports.AppDaysBack = exports.AppCode = exports.AppCodeByType = exports.AppUniqueCode = exports.AppRandomString = void 0;
|
|
4
13
|
const randomstring_1 = require("randomstring");
|
|
5
|
-
const
|
|
14
|
+
const reg_exr_1 = require("./reg-exr");
|
|
15
|
+
const countries_1 = require("./countries");
|
|
16
|
+
const languages_1 = require("./languages");
|
|
6
17
|
const AppRandomString = (length, charset) => {
|
|
7
18
|
return (0, randomstring_1.generate)({ length: length, charset: charset });
|
|
8
19
|
};
|
|
@@ -19,15 +30,15 @@ const AppUniqueCode = () => {
|
|
|
19
30
|
};
|
|
20
31
|
exports.AppUniqueCode = AppUniqueCode;
|
|
21
32
|
const AppCodeByType = (name, type = null) => {
|
|
22
|
-
let str =
|
|
33
|
+
let str = "";
|
|
23
34
|
if (type) {
|
|
24
|
-
str = type +
|
|
35
|
+
str = type + "_" + name;
|
|
25
36
|
}
|
|
26
37
|
else {
|
|
27
|
-
str = name +
|
|
38
|
+
str = name + "_" + (0, exports.AppUniqueCode)();
|
|
28
39
|
}
|
|
29
|
-
str = str.replace(
|
|
30
|
-
str = str.replace(/\s/g,
|
|
40
|
+
str = str.replace(reg_exr_1.RegExp.NON_ALPHA_NUMERIC, "_");
|
|
41
|
+
str = str.replace(/\s/g, "_");
|
|
31
42
|
str = str.substr(0, 128);
|
|
32
43
|
return str.toUpperCase();
|
|
33
44
|
};
|
|
@@ -35,10 +46,10 @@ exports.AppCodeByType = AppCodeByType;
|
|
|
35
46
|
const AppCode = (name) => {
|
|
36
47
|
if (name == null)
|
|
37
48
|
return null;
|
|
38
|
-
let str =
|
|
49
|
+
let str = "";
|
|
39
50
|
str = name.trim();
|
|
40
|
-
str = str.replace(
|
|
41
|
-
str = str.replace(/\s/g,
|
|
51
|
+
str = str.replace(reg_exr_1.RegExp.NON_ALPHA_NUMERIC, "_");
|
|
52
|
+
str = str.replace(/\s/g, "_");
|
|
42
53
|
return str.toUpperCase();
|
|
43
54
|
};
|
|
44
55
|
exports.AppCode = AppCode;
|
|
@@ -67,10 +78,74 @@ const AppAddDays = (date, addDays, isDays = true) => {
|
|
|
67
78
|
};
|
|
68
79
|
exports.AppAddDays = AppAddDays;
|
|
69
80
|
const AppUUID4 = () => {
|
|
70
|
-
return
|
|
81
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
|
|
71
82
|
const r = (Math.random() * 16) | 0;
|
|
72
|
-
const v = c ==
|
|
83
|
+
const v = c == "x" ? r : (r & 0x3) | 0x8;
|
|
73
84
|
return v.toString(16);
|
|
74
85
|
});
|
|
75
86
|
};
|
|
76
87
|
exports.AppUUID4 = AppUUID4;
|
|
88
|
+
const LangText = (data, source, target) => __awaiter(void 0, void 0, void 0, function* () {
|
|
89
|
+
if (!data || !source || !target) {
|
|
90
|
+
throw new Error("Invalid input: data, source, or target is missing");
|
|
91
|
+
}
|
|
92
|
+
const src = source.split("-")[0]; // e.g., "en-US" -> "en"
|
|
93
|
+
const tgt = target.split("-")[0]; // e.g., "te-IN" -> "te"
|
|
94
|
+
if (!/^[a-zA-Z]{2}$/.test(src) || !/^[a-zA-Z]{2}$/.test(tgt)) {
|
|
95
|
+
throw new Error("Invalid language code format");
|
|
96
|
+
}
|
|
97
|
+
const encodedData = encodeURIComponent(data);
|
|
98
|
+
const url = `https://lingva.ml/api/v1/${src}/${tgt}/${encodedData}`;
|
|
99
|
+
const res = yield fetch(url, {
|
|
100
|
+
method: "GET",
|
|
101
|
+
});
|
|
102
|
+
if (!res.ok) {
|
|
103
|
+
throw new Error(`Translation failed: ${res.status} ${res.statusText}`);
|
|
104
|
+
}
|
|
105
|
+
const json = yield res.json();
|
|
106
|
+
if (json.error) {
|
|
107
|
+
throw new Error(`API error: ${json.error}`);
|
|
108
|
+
}
|
|
109
|
+
return json.translation || "";
|
|
110
|
+
});
|
|
111
|
+
exports.LangText = LangText;
|
|
112
|
+
const CurrencyConvert = (from, to) => __awaiter(void 0, void 0, void 0, function* () {
|
|
113
|
+
if (!from || !to) {
|
|
114
|
+
throw new Error("Invalid input: from or to is missing");
|
|
115
|
+
}
|
|
116
|
+
if (from === to) {
|
|
117
|
+
return 1;
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
const url = `https://api.frankfurter.app/latest?from=${from}&to=${to}`;
|
|
121
|
+
const res = yield fetch(url, {
|
|
122
|
+
method: "GET",
|
|
123
|
+
});
|
|
124
|
+
if (!res.ok) {
|
|
125
|
+
throw new Error(`Currency conversion failed: ${res.status} ${res.statusText}`);
|
|
126
|
+
}
|
|
127
|
+
const json = yield res.json();
|
|
128
|
+
return json.rates[to];
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
throw new Error(`Currency conversion failed: ${error}`);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
exports.CurrencyConvert = CurrencyConvert;
|
|
135
|
+
const LangCountryCode = (lang) => {
|
|
136
|
+
const countryCode = lang.split("-")[1];
|
|
137
|
+
const country = countries_1.countries.find((c) => c.code === countryCode) || {};
|
|
138
|
+
const language = languages_1.languages.find((l) => l.lang === lang) || {};
|
|
139
|
+
return {
|
|
140
|
+
lang: lang,
|
|
141
|
+
country: countryCode,
|
|
142
|
+
name: language.name,
|
|
143
|
+
locale: language.locale,
|
|
144
|
+
currencyCode: country.currencyCode,
|
|
145
|
+
currency: country.currency,
|
|
146
|
+
telCode: country.telCode,
|
|
147
|
+
flag: country.flag,
|
|
148
|
+
dir: language.dir,
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
exports.LangCountryCode = LangCountryCode;
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// bun run src/tests/util.ts
|
|
3
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
5
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
6
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
7
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
8
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
9
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
const util_1 = require("../main/util");
|
|
14
|
+
const CallLangCountry = () => {
|
|
15
|
+
const lang = "en-US";
|
|
16
|
+
const country = (0, util_1.LangCountryCode)(lang);
|
|
17
|
+
console.log("LangCountryCode:: ", country);
|
|
18
|
+
};
|
|
19
|
+
const CallLangText = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
|
+
const resp = yield (0, util_2.LangText)("Hello, how are you?", "en-US", "te-IN");
|
|
21
|
+
console.log("Resp Kannada:: ", resp);
|
|
22
|
+
});
|
|
23
|
+
const CallCurrencyConvert = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
24
|
+
const resp = yield (0, util_1.CurrencyConvert)("INR", "USD");
|
|
25
|
+
console.log("Resp Currency:: ", resp);
|
|
26
|
+
});
|
|
27
|
+
const util_2 = require("../main/util");
|
|
28
|
+
const run = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
29
|
+
yield CallLangCountry();
|
|
30
|
+
yield CallLangText();
|
|
31
|
+
yield CallCurrencyConvert();
|
|
32
|
+
});
|
|
33
|
+
run();
|
package/package.json
CHANGED
|
File without changes
|
|
File without changes
|