dff-util 1.2.6 → 1.2.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 CHANGED
@@ -842,4 +842,77 @@ export type AllLanguageCodesType =
842
842
  | "en-ZW";
843
843
  ```
844
844
 
845
- ### Available methods TruncateText, DateTime24HrFormat,DateTime12HrFormat, DateAndTime, TimeAgo
845
+ ### Available methods TruncateText, DateTime24HrFormat,DateTime12HrFormat, DateAndTime, TimeAgo
846
+
847
+ ### Case
848
+ ```
849
+ toCamelCase("user_name") // "userName"
850
+ toCamelCase("user-name") // "userName"
851
+ toCamelCase("User Name") // "userName"
852
+ toCamelCase("USER_NAME") // "userName"
853
+
854
+ toSnakeCase("userName") // "user_name"
855
+ toSnakeCase("UserName") // "user_name"
856
+ toSnakeCase("user name") // "user_name"
857
+ toSnakeCase("user-name") // "user_name"
858
+
859
+ toLowerCase("userName") // "user_name"
860
+ toLowerCase("UserName") // "user_name"
861
+ toLowerCase("user-name") // "user_name"
862
+
863
+ toUpperCase("userName") // "USER_NAME"
864
+ toUpperCase("user name") // "USER_NAME"
865
+ toUpperCase("user-name") // "USER_NAME"
866
+ toUpperCase("UserName") // "USER_NAME"
867
+
868
+ toPascalCase("user_name") // "UserName"
869
+ toPascalCase("user name") // "UserName"
870
+ toPascalCase("user-name") // "UserName"
871
+ toPascalCase("USER_NAME") // "UserName"
872
+
873
+ input:
874
+ const mongoDoc = {
875
+ _id: 'abc123',
876
+ user_name: 'prasad',
877
+ address_info: {
878
+ city_name: 'hyderabad'
879
+ }
880
+ };
881
+
882
+ toViewMapper(mongoDoc) -->
883
+ {
884
+ id: 'abc123',
885
+ userName: 'prasad',
886
+ addressInfo: {
887
+ cityName: 'hyderabad'
888
+ }
889
+ }
890
+
891
+
892
+ const viewObj = {
893
+ id: 'abc123',
894
+ userName: 'prasad',
895
+ addressInfo: {
896
+ cityName: 'hyderabad'
897
+ }
898
+ }
899
+
900
+ toEntityMapper(viewObj) -->
901
+ {
902
+ id: 'abc123',
903
+ user_name: 'prasad',
904
+ address_info: {
905
+ city_name: 'hyderabad'
906
+ }
907
+ }
908
+
909
+ toSchemaMapper(viewObj) -->
910
+ {
911
+ _id: 'abc123',
912
+ user_name: 'prasad',
913
+ address_info: {
914
+ city_name: 'hyderabad'
915
+ }
916
+ }
917
+
918
+ ```
package/dist/index.d.ts CHANGED
@@ -8,3 +8,4 @@ export * from './main/const-keys';
8
8
  export * from './main/const-messages';
9
9
  export * from './main/countries';
10
10
  export * from './main/languages';
11
+ export * from './main/case-mapper';
package/dist/index.js CHANGED
@@ -24,3 +24,4 @@ __exportStar(require("./main/const-keys"), exports);
24
24
  __exportStar(require("./main/const-messages"), exports);
25
25
  __exportStar(require("./main/countries"), exports);
26
26
  __exportStar(require("./main/languages"), exports);
27
+ __exportStar(require("./main/case-mapper"), exports);
@@ -0,0 +1,8 @@
1
+ export declare function toCamelCase(str: string): string;
2
+ export declare function toSnakeCase(str: string): string;
3
+ export declare function toPascalCase(str: string): string;
4
+ export declare function toUpperCase(str: string): string;
5
+ export declare function toLowerCase(str: string): string;
6
+ export declare function toViewMapper(obj: any): any;
7
+ export declare function toEntityMapper(obj: any): any;
8
+ export declare function toSchemaMapper(obj: any): any;
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toCamelCase = toCamelCase;
4
+ exports.toSnakeCase = toSnakeCase;
5
+ exports.toPascalCase = toPascalCase;
6
+ exports.toUpperCase = toUpperCase;
7
+ exports.toLowerCase = toLowerCase;
8
+ exports.toViewMapper = toViewMapper;
9
+ exports.toEntityMapper = toEntityMapper;
10
+ exports.toSchemaMapper = toSchemaMapper;
11
+ function toCamelCase(str) {
12
+ return str
13
+ .replace(/[_\s-]+(.)?/g, (_, chr) => (chr ? chr.toUpperCase() : ''))
14
+ .replace(/^[A-Z]/, (chr) => chr.toLowerCase());
15
+ }
16
+ function toSnakeCase(str) {
17
+ return str
18
+ .replace(/([a-z0-9])([A-Z])/g, '$1_$2') // camelCase → camel_Case
19
+ .replace(/[\s-]+/g, '_') // space/dash → underscore
20
+ .replace(/__+/g, '_') // collapse multiple underscores
21
+ .toLowerCase();
22
+ }
23
+ function toPascalCase(str) {
24
+ return str
25
+ .replace(/[_\s-]+/g, ' ')
26
+ .replace(/\w+/g, (word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
27
+ .replace(/\s+/g, '');
28
+ }
29
+ function toUpperCase(str) {
30
+ return str
31
+ .replace(/([a-z0-9])([A-Z])/g, '$1_$2') // camelCase to snake_case
32
+ .replace(/[\s-]+/g, '_') // spaces/hyphens to underscores
33
+ .replace(/([A-Z]+)/g, '_$1') // handle acronyms (optional)
34
+ .replace(/^_+|_+$/g, '') // trim leading/trailing _
35
+ .toUpperCase();
36
+ }
37
+ function toLowerCase(str) {
38
+ return str
39
+ .replace(/([a-z0-9])([A-Z])/g, '$1_$2')
40
+ .replace(/[\s-]+/g, '_')
41
+ .replace(/([A-Z]+)/g, '_$1')
42
+ .replace(/^_+|_+$/g, '')
43
+ .toLowerCase();
44
+ }
45
+ function isPlainObject(obj) {
46
+ return Object.prototype.toString.call(obj) === '[object Object]';
47
+ }
48
+ function toViewMapper(obj) {
49
+ if (Array.isArray(obj)) {
50
+ return obj.map(toViewMapper);
51
+ }
52
+ else if (isPlainObject(obj)) {
53
+ return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
54
+ toCamelCase(key === '_id' ? 'id' : key),
55
+ toViewMapper(value),
56
+ ]));
57
+ }
58
+ return obj; // Date, Buffer, Map, etc. returned as-is
59
+ }
60
+ function toEntityMapper(obj) {
61
+ if (Array.isArray(obj)) {
62
+ return obj.map(toEntityMapper);
63
+ }
64
+ else if (isPlainObject(obj)) {
65
+ return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
66
+ toSnakeCase(key),
67
+ toEntityMapper(value),
68
+ ]));
69
+ }
70
+ return obj; // Date, Buffer, Map, etc. returned as-is
71
+ }
72
+ function toSchemaMapper(obj) {
73
+ if (Array.isArray(obj)) {
74
+ return obj.map(toEntityMapper);
75
+ }
76
+ else if (isPlainObject(obj)) {
77
+ return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
78
+ toSnakeCase(key === 'id' ? '_id' : key),
79
+ toEntityMapper(value),
80
+ ]));
81
+ }
82
+ return obj; // Date, Buffer, Map, etc. returned as-is
83
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dff-util",
3
- "version": "1.2.6",
3
+ "version": "1.2.8",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {