dff-util 1.2.9 → 1.3.1

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
@@ -575,6 +575,50 @@ const main = async () => {
575
575
 
576
576
  main();
577
577
 
578
+ ---------------------------------------------------
579
+
580
+ // Base64
581
+ const text = "Hello World!";
582
+ const encodedBase64 = encodeBase64(text); // "SGVsbG8gV29ybGQh"
583
+ const decodedBase64 = decodeBase64(encodedBase64); // "Hello World!"
584
+
585
+ console.log(encodedBase64, decodedBase64);
586
+
587
+ // URL Encode/Decode
588
+ const url = "https://example.com/query?name=John Doe&city=New York";
589
+
590
+ // First encode
591
+ const encodedURL1 = encodeURL(url);
592
+ console.log(encodedURL1);
593
+ // Output: "https%3A%2F%2Fexample.com%2Fquery%3Fname%3DJohn%20Doe%26city%3DNew%20York"
594
+
595
+ // Encode again (idempotent)
596
+ const encodedURL2 = encodeURL(encodedURL1);
597
+ console.log(encodedURL2 === encodedURL1); // true ✅
598
+
599
+ // Decode
600
+ const decodedURL1 = decodeURL(encodedURL1);
601
+ console.log(decodedURL1); // "https://example.com/query?name=John Doe&city=New York"
602
+
603
+ // Decode again (idempotent)
604
+ const decodedURL2 = decodeURL(decodedURL1);
605
+ console.log(decodedURL2 === decodedURL1); // true ✅
606
+
607
+ const original = "Hello World! @#$% 😊";
608
+
609
+ // Encode
610
+ const safe = safeEncode(original);
611
+ console.log(safe);
612
+ // Output: "U0dWc2JHOGdWMjl5YkdRPQ%3D%3D"
613
+
614
+ // Decode
615
+ const decoded = safeDecode(safe);
616
+ console.log(decoded);
617
+ // Output: "Hello World! @#$% 😊"
618
+
619
+ // Multiple encode/decode calls (idempotent)
620
+ console.log(safeEncode(safeDecode(safe)) === safe); // true
621
+
578
622
 
579
623
 
580
624
  ```
@@ -4,5 +4,5 @@ export declare function toPascalCase(str: string): string;
4
4
  export declare function toUpperCase(str: string): string;
5
5
  export declare function toLowerCase(str: string): string;
6
6
  export declare function toViewMapper(obj: any): any;
7
- export declare function toEntityMapper(obj: any): any;
7
+ export declare function toEntityMapper<T = any>(obj: T): any;
8
8
  export declare function toSchemaMapper(obj: any): any;
@@ -8,6 +8,7 @@ exports.toLowerCase = toLowerCase;
8
8
  exports.toViewMapper = toViewMapper;
9
9
  exports.toEntityMapper = toEntityMapper;
10
10
  exports.toSchemaMapper = toSchemaMapper;
11
+ const const_value_1 = require("./const-value");
11
12
  function toCamelCase(str) {
12
13
  return str
13
14
  .replace(/[_\s-]+(.)?/g, (_, chr) => (chr ? chr.toUpperCase() : ''))
@@ -45,11 +46,23 @@ function toLowerCase(str) {
45
46
  function isPlainObject(obj) {
46
47
  return Object.prototype.toString.call(obj) === '[object Object]';
47
48
  }
49
+ const LANGUAGE_CODE_SET = new Set(const_value_1.AllLanguageCodes);
50
+ function isLanguageMap(value) {
51
+ if (!isPlainObject(value))
52
+ return false;
53
+ const keys = Object.keys(value);
54
+ if (keys.length === 0)
55
+ return false;
56
+ return keys.every((k) => LANGUAGE_CODE_SET.has(k));
57
+ }
48
58
  function toViewMapper(obj) {
49
59
  if (Array.isArray(obj)) {
50
60
  return obj.map(toViewMapper);
51
61
  }
52
- else if (isPlainObject(obj)) {
62
+ if (isLanguageMap(obj)) {
63
+ return obj;
64
+ }
65
+ if (isPlainObject(obj)) {
53
66
  return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
54
67
  toCamelCase(key === '_id' ? 'id' : key),
55
68
  toViewMapper(value),
@@ -59,21 +72,27 @@ function toViewMapper(obj) {
59
72
  }
60
73
  function toEntityMapper(obj) {
61
74
  if (Array.isArray(obj)) {
62
- return obj.map(toEntityMapper);
75
+ return obj.map((v) => toEntityMapper(v));
63
76
  }
64
- else if (isPlainObject(obj)) {
65
- return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
66
- toSnakeCase(key),
67
- toEntityMapper(value),
68
- ]));
77
+ if (isLanguageMap(obj)) {
78
+ return obj; // preserve language codes exactly
69
79
  }
70
- return obj; // Date, Buffer, Map, etc. returned as-is
80
+ if (isPlainObject(obj)) {
81
+ return Object.fromEntries(Object.entries(obj).map(([key, value]) => {
82
+ const mappedKey = toSnakeCase(key);
83
+ return [mappedKey, toEntityMapper(value)];
84
+ }));
85
+ }
86
+ return obj; // primitives, Date, Buffer, etc.
71
87
  }
72
88
  function toSchemaMapper(obj) {
73
89
  if (Array.isArray(obj)) {
74
90
  return obj.map(toEntityMapper);
75
91
  }
76
- else if (isPlainObject(obj)) {
92
+ if (isLanguageMap(obj)) {
93
+ return obj; // do not transform language code keys
94
+ }
95
+ if (isPlainObject(obj)) {
77
96
  return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
78
97
  toSnakeCase(key === 'id' ? '_id' : key),
79
98
  toEntityMapper(value),
@@ -43,3 +43,4 @@ export declare const ConstValue: {
43
43
  DARK: string;
44
44
  MonthNames: string[];
45
45
  };
46
+ export declare const AllLanguageCodes: string[];
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ConstValue = void 0;
3
+ exports.AllLanguageCodes = exports.ConstValue = void 0;
4
4
  exports.ConstValue = {
5
5
  // Logical operators
6
6
  AND: '&&',
@@ -63,3 +63,213 @@ exports.ConstValue = {
63
63
  "Dec",
64
64
  ],
65
65
  };
66
+ exports.AllLanguageCodes = [
67
+ "ps-AF",
68
+ "sq-AL",
69
+ "ar-DZ",
70
+ "ca-AD",
71
+ "pt-AO",
72
+ "en-AG",
73
+ "es-AR",
74
+ "hy-AM",
75
+ "en-AU",
76
+ "de-AT",
77
+ "az-AZ",
78
+ "en-BS",
79
+ "ar-BH",
80
+ "bn-BD",
81
+ "en-BB",
82
+ "be-BY",
83
+ "nl-BE",
84
+ "en-BZ",
85
+ "fr-BJ",
86
+ "dz-BT",
87
+ "es-BO",
88
+ "bs-BA",
89
+ "en-BW",
90
+ "pt-BR",
91
+ "ms-BN",
92
+ "bg-BG",
93
+ "fr-BF",
94
+ "rn-BI",
95
+ "pt-CV",
96
+ "km-KH",
97
+ "fr-CM",
98
+ "en-CA",
99
+ "fr-CF",
100
+ "fr-TD",
101
+ "es-CL",
102
+ "zh-CN",
103
+ "es-CO",
104
+ "ar-KM",
105
+ "fr-CG",
106
+ "fr-CD",
107
+ "es-CR",
108
+ "fr-CI",
109
+ "hr-HR",
110
+ "es-CU",
111
+ "el-CY",
112
+ "cs-CZ",
113
+ "da-DK",
114
+ "fr-DJ",
115
+ "en-DM",
116
+ "es-DO",
117
+ "es-EC",
118
+ "ar-EG",
119
+ "es-SV",
120
+ "es-GQ",
121
+ "ti-ER",
122
+ "et-EE",
123
+ "si-SZ",
124
+ "am-ET",
125
+ "en-FJ",
126
+ "fi-FI",
127
+ "fr-FR",
128
+ "fr-GA",
129
+ "en-GM",
130
+ "ka-GE",
131
+ "de-DE",
132
+ "en-GH",
133
+ "el-GR",
134
+ "en-GD",
135
+ "es-GT",
136
+ "fr-GN",
137
+ "pt-GW",
138
+ "en-GY",
139
+ "fr-HT",
140
+ "es-HN",
141
+ "hu-HU",
142
+ "is-IS",
143
+ "hi-IN",
144
+ "kn-IN",
145
+ "ta-IN",
146
+ "te-IN",
147
+ "bn-IN",
148
+ "mr-IN",
149
+ "gu-IN",
150
+ "ml-IN",
151
+ "pa-IN",
152
+ "or-IN",
153
+ "as-IN",
154
+ "id-ID",
155
+ "fa-IR",
156
+ "ar-IQ",
157
+ "ga-IE",
158
+ "he-IL",
159
+ "it-IT",
160
+ "en-JM",
161
+ "ja-JP",
162
+ "ar-JO",
163
+ "kk-KZ",
164
+ "sw-KE",
165
+ "en-KI",
166
+ "ko-KP",
167
+ "ko-KR",
168
+ "ar-KW",
169
+ "ky-KG",
170
+ "lo-LA",
171
+ "lv-LV",
172
+ "ar-LB",
173
+ "st-LS",
174
+ "en-LR",
175
+ "ar-LY",
176
+ "de-LI",
177
+ "lt-LT",
178
+ "fr-LU",
179
+ "mg-MG",
180
+ "ny-MW",
181
+ "ms-MY",
182
+ "dv-MV",
183
+ "fr-ML",
184
+ "mt-MT",
185
+ "en-MH",
186
+ "ar-MR",
187
+ "en-MU",
188
+ "es-MX",
189
+ "en-FM",
190
+ "ro-MD",
191
+ "fr-MC",
192
+ "mn-MN",
193
+ "sr-ME",
194
+ "ar-MA",
195
+ "pt-MZ",
196
+ "my-MM",
197
+ "en-NA",
198
+ "en-NR",
199
+ "ne-NP",
200
+ "nl-NL",
201
+ "en-NZ",
202
+ "es-NI",
203
+ "fr-NE",
204
+ "en-NG",
205
+ "yo-NG",
206
+ "ha-NG",
207
+ "ig-NG",
208
+ "en-NU",
209
+ "nb-NO",
210
+ "ar-OM",
211
+ "ur-PK",
212
+ "en-PW",
213
+ "ar-PS",
214
+ "es-PA",
215
+ "en-PG",
216
+ "es-PY",
217
+ "es-PE",
218
+ "tl-PH",
219
+ "pl-PL",
220
+ "pt-PT",
221
+ "ar-QA",
222
+ "ro-RO",
223
+ "ru-RU",
224
+ "rw-RW",
225
+ "en-KN",
226
+ "en-LC",
227
+ "en-VC",
228
+ "sm-WS",
229
+ "it-SM",
230
+ "pt-ST",
231
+ "ar-SA",
232
+ "fr-SN",
233
+ "sr-RS",
234
+ "en-SC",
235
+ "en-SL",
236
+ "en-SG",
237
+ "sk-SK",
238
+ "sl-SI",
239
+ "en-SB",
240
+ "so-SO",
241
+ "af-ZA",
242
+ "en-SS",
243
+ "es-ES",
244
+ "si-LK",
245
+ "ar-SD",
246
+ "nl-SR",
247
+ "sv-SE",
248
+ "de-CH",
249
+ "ar-SY",
250
+ "tg-TJ",
251
+ "sw-TZ",
252
+ "th-TH",
253
+ "pt-TL",
254
+ "fr-TG",
255
+ "to-TO",
256
+ "en-TT",
257
+ "ar-TN",
258
+ "tr-TR",
259
+ "tk-TM",
260
+ "en-TV",
261
+ "en-UG",
262
+ "uk-UA",
263
+ "ar-AE",
264
+ "en-GB",
265
+ "en-US",
266
+ "es-UY",
267
+ "uz-UZ",
268
+ "bi-VU",
269
+ "it-VA",
270
+ "es-VE",
271
+ "vi-VN",
272
+ "ar-YE",
273
+ "en-ZM",
274
+ "en-ZW",
275
+ ];
@@ -14,3 +14,9 @@ export declare const TimeAgo: (date: Date) => string;
14
14
  export declare const LangText: (data: string, source: string, target: string) => Promise<any>;
15
15
  export declare const LangCountryCode: (lang: string) => LangCountryType;
16
16
  export declare const CurrencyConvert: (from: string, to: string) => Promise<number>;
17
+ export declare function EncodeBase64(input: string): string;
18
+ export declare function DecodeBase64(encoded: string): string;
19
+ export declare function EncodeURL(input: string): string;
20
+ export declare function DecodeURL(input: string): string;
21
+ export declare function SafeEncode(input: string): string;
22
+ export declare function SafeDecode(encoded: string): string;
package/dist/main/util.js CHANGED
@@ -10,6 +10,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.CurrencyConvert = exports.LangCountryCode = exports.LangText = 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
+ exports.EncodeBase64 = EncodeBase64;
14
+ exports.DecodeBase64 = DecodeBase64;
15
+ exports.EncodeURL = EncodeURL;
16
+ exports.DecodeURL = DecodeURL;
17
+ exports.SafeEncode = SafeEncode;
18
+ exports.SafeDecode = SafeDecode;
13
19
  const randomstring_1 = require("randomstring");
14
20
  const reg_exr_1 = require("./reg-exr");
15
21
  const countries_1 = require("./countries");
@@ -216,3 +222,32 @@ const CurrencyConvert = (from, to) => __awaiter(void 0, void 0, void 0, function
216
222
  }
217
223
  });
218
224
  exports.CurrencyConvert = CurrencyConvert;
225
+ function EncodeBase64(input) {
226
+ return Buffer.from(input, 'utf-8').toString('base64');
227
+ }
228
+ function DecodeBase64(encoded) {
229
+ return Buffer.from(encoded, 'base64').toString('utf-8');
230
+ }
231
+ function EncodeURL(input) {
232
+ try {
233
+ const decoded = decodeURIComponent(input);
234
+ return encodeURIComponent(decoded);
235
+ }
236
+ catch (_a) {
237
+ return encodeURIComponent(input);
238
+ }
239
+ }
240
+ function DecodeURL(input) {
241
+ try {
242
+ return decodeURIComponent(input);
243
+ }
244
+ catch (_a) {
245
+ return input;
246
+ }
247
+ }
248
+ function SafeEncode(input) {
249
+ return EncodeURL(EncodeBase64(input));
250
+ }
251
+ function SafeDecode(encoded) {
252
+ return DecodeBase64(DecodeURL(encoded));
253
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ // bun run src/tests/mapper.ts
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ const case_mapper_1 = require("../main/case-mapper");
5
+ const dto = {
6
+ id: "abc123",
7
+ nameLang: {
8
+ "en-US": "Hello",
9
+ "te-IN": "హలో",
10
+ "ta-IN": "வணக்கம்",
11
+ },
12
+ createdAt: new Date(),
13
+ createdBy: "user123",
14
+ };
15
+ const entity = (0, case_mapper_1.toViewMapper)(dto);
16
+ const schema = (0, case_mapper_1.toSchemaMapper)(entity);
17
+ const newEntity = (0, case_mapper_1.toViewMapper)(dto);
18
+ console.log(entity, schema, newEntity);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dff-util",
3
- "version": "1.2.9",
3
+ "version": "1.3.1",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
@@ -28,4 +28,4 @@
28
28
  "jwt-encode": "^1.0.1",
29
29
  "randomstring": "1.3.0"
30
30
  }
31
- }
31
+ }