dff-util 1.3.0 → 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.
@@ -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
+ ];
@@ -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.3.0",
3
+ "version": "1.3.1",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {