dff-util 1.1.6 → 1.1.7

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 CHANGED
@@ -454,3 +454,30 @@ const main = async () => {
454
454
 
455
455
  main();
456
456
  ```
457
+
458
+ ### LangData
459
+ ```
460
+ // bun run src/tests/lang.ts
461
+ import { LangData } from "../main/util";
462
+
463
+ const run = async () => {
464
+ try {
465
+ const resp = await LangData("Hello, how are you?", "en-US", "ka-IN");
466
+ console.log("Resp Kannada:: ", resp);
467
+
468
+ const resp1 = await LangData("Hello, how are you?", "en-US", "ar-SA");
469
+ console.log("Resp Arabic:: ", resp1);
470
+
471
+ const resp2 = await LangData("Hello, how are you?", "en-US", "te-IN");
472
+ console.log("Resp Telugu:: ", resp2);
473
+
474
+ const resp3 = await LangData("Hello, how are you?", "en-US", "ta-IN");
475
+ console.log("Resp Tamil:: ", resp3);
476
+
477
+ } catch (error) {
478
+ console.log(error);
479
+ }
480
+ };
481
+
482
+ run();
483
+ ```
@@ -5,3 +5,4 @@ export declare const AppCode: (name: string) => string | null;
5
5
  export declare const AppDaysBack: (date: Date, backValue: number, isDays?: boolean) => Date;
6
6
  export declare const AppAddDays: (date: Date, addDays: number, isDays?: boolean) => Date;
7
7
  export declare const AppUUID4: () => string;
8
+ export declare const LangData: (data: string, source: string, target: string) => Promise<any>;
package/dist/main/util.js CHANGED
@@ -1,6 +1,15 @@
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.LangData = exports.AppUUID4 = exports.AppAddDays = exports.AppDaysBack = exports.AppCode = exports.AppCodeByType = exports.AppUniqueCode = exports.AppRandomString = void 0;
4
13
  const randomstring_1 = require("randomstring");
5
14
  const regexr_1 = require("./regexr");
6
15
  const AppRandomString = (length, charset) => {
@@ -19,15 +28,15 @@ const AppUniqueCode = () => {
19
28
  };
20
29
  exports.AppUniqueCode = AppUniqueCode;
21
30
  const AppCodeByType = (name, type = null) => {
22
- let str = '';
31
+ let str = "";
23
32
  if (type) {
24
- str = type + '_' + name;
33
+ str = type + "_" + name;
25
34
  }
26
35
  else {
27
- str = name + '_' + (0, exports.AppUniqueCode)();
36
+ str = name + "_" + (0, exports.AppUniqueCode)();
28
37
  }
29
- str = str.replace(regexr_1.RegExp.NON_ALPHA_NUMERIC, '_');
30
- str = str.replace(/\s/g, '_');
38
+ str = str.replace(regexr_1.RegExp.NON_ALPHA_NUMERIC, "_");
39
+ str = str.replace(/\s/g, "_");
31
40
  str = str.substr(0, 128);
32
41
  return str.toUpperCase();
33
42
  };
@@ -35,10 +44,10 @@ exports.AppCodeByType = AppCodeByType;
35
44
  const AppCode = (name) => {
36
45
  if (name == null)
37
46
  return null;
38
- let str = '';
47
+ let str = "";
39
48
  str = name.trim();
40
- str = str.replace(regexr_1.RegExp.NON_ALPHA_NUMERIC, '_');
41
- str = str.replace(/\s/g, '_');
49
+ str = str.replace(regexr_1.RegExp.NON_ALPHA_NUMERIC, "_");
50
+ str = str.replace(/\s/g, "_");
42
51
  return str.toUpperCase();
43
52
  };
44
53
  exports.AppCode = AppCode;
@@ -67,10 +76,34 @@ const AppAddDays = (date, addDays, isDays = true) => {
67
76
  };
68
77
  exports.AppAddDays = AppAddDays;
69
78
  const AppUUID4 = () => {
70
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
79
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
71
80
  const r = (Math.random() * 16) | 0;
72
- const v = c == 'x' ? r : (r & 0x3) | 0x8;
81
+ const v = c == "x" ? r : (r & 0x3) | 0x8;
73
82
  return v.toString(16);
74
83
  });
75
84
  };
76
85
  exports.AppUUID4 = AppUUID4;
86
+ const LangData = (data, source, target) => __awaiter(void 0, void 0, void 0, function* () {
87
+ if (!data || !source || !target) {
88
+ throw new Error("Invalid input: data, source, or target is missing");
89
+ }
90
+ const src = source.split("-")[0]; // e.g., "en-US" -> "en"
91
+ const tgt = target.split("-")[0]; // e.g., "te-IN" -> "te"
92
+ if (!/^[a-zA-Z]{2}$/.test(src) || !/^[a-zA-Z]{2}$/.test(tgt)) {
93
+ throw new Error("Invalid language code format");
94
+ }
95
+ const encodedData = encodeURIComponent(data);
96
+ const url = `https://lingva.ml/api/v1/${src}/${tgt}/${encodedData}`;
97
+ const res = yield fetch(url, {
98
+ method: "GET",
99
+ });
100
+ if (!res.ok) {
101
+ throw new Error(`Translation failed: ${res.status} ${res.statusText}`);
102
+ }
103
+ const json = yield res.json();
104
+ if (json.error) {
105
+ throw new Error(`API error: ${json.error}`);
106
+ }
107
+ return json.translation || "";
108
+ });
109
+ exports.LangData = LangData;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ // bun run src/tests/lang.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 run = () => __awaiter(void 0, void 0, void 0, function* () {
15
+ try {
16
+ const resp = yield (0, util_1.LangData)("Hello, how are you?", "en-US", "ka-IN");
17
+ console.log("Resp Kannada:: ", resp);
18
+ const resp1 = yield (0, util_1.LangData)("Hello, how are you?", "en-US", "ar-SA");
19
+ console.log("Resp Arabic:: ", resp1);
20
+ const resp2 = yield (0, util_1.LangData)("Hello, how are you?", "en-US", "te-IN");
21
+ console.log("Resp Telugu:: ", resp2);
22
+ const resp3 = yield (0, util_1.LangData)("Hello, how are you?", "en-US", "ta-IN");
23
+ console.log("Resp Tamil:: ", resp3);
24
+ }
25
+ catch (error) {
26
+ console.log(error);
27
+ }
28
+ });
29
+ run();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dff-util",
3
- "version": "1.1.6",
3
+ "version": "1.1.7",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {