dff-util 1.2.6 → 1.2.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
@@ -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, isMongoId?: boolean): 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,82 @@
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, isMongoId = false) {
17
+ if (str === 'id' && isMongoId)
18
+ return '_id';
19
+ return str
20
+ .replace(/([a-z0-9])([A-Z])/g, '$1_$2') // camelCase → camel_Case
21
+ .replace(/[\s-]+/g, '_') // space/dash → underscore
22
+ .replace(/__+/g, '_') // collapse multiple underscores
23
+ .toLowerCase();
24
+ }
25
+ function toPascalCase(str) {
26
+ return str
27
+ .replace(/[_\s-]+/g, ' ')
28
+ .replace(/\w+/g, (word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
29
+ .replace(/\s+/g, '');
30
+ }
31
+ function toUpperCase(str) {
32
+ return str
33
+ .replace(/([a-z0-9])([A-Z])/g, '$1_$2') // camelCase to snake_case
34
+ .replace(/[\s-]+/g, '_') // spaces/hyphens to underscores
35
+ .replace(/([A-Z]+)/g, '_$1') // handle acronyms (optional)
36
+ .replace(/^_+|_+$/g, '') // trim leading/trailing _
37
+ .toUpperCase();
38
+ }
39
+ function toLowerCase(str) {
40
+ return str
41
+ .replace(/([a-z0-9])([A-Z])/g, '$1_$2')
42
+ .replace(/[\s-]+/g, '_')
43
+ .replace(/([A-Z]+)/g, '_$1')
44
+ .replace(/^_+|_+$/g, '')
45
+ .toLowerCase();
46
+ }
47
+ function toViewMapper(obj) {
48
+ if (Array.isArray(obj)) {
49
+ return obj.map(toViewMapper);
50
+ }
51
+ else if (obj !== null && typeof obj === 'object') {
52
+ return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
53
+ toCamelCase(key),
54
+ toViewMapper(value),
55
+ ]));
56
+ }
57
+ return obj;
58
+ }
59
+ function toEntityMapper(obj) {
60
+ if (Array.isArray(obj)) {
61
+ return obj.map(toEntityMapper);
62
+ }
63
+ else if (obj !== null && typeof obj === 'object') {
64
+ return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
65
+ toSnakeCase(key),
66
+ toEntityMapper(value),
67
+ ]));
68
+ }
69
+ return obj;
70
+ }
71
+ function toSchemaMapper(obj) {
72
+ if (Array.isArray(obj)) {
73
+ return obj.map(toSchemaMapper);
74
+ }
75
+ else if (obj !== null && typeof obj === 'object') {
76
+ return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
77
+ toSnakeCase(key, true),
78
+ toSchemaMapper(value),
79
+ ]));
80
+ }
81
+ return obj;
82
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dff-util",
3
- "version": "1.2.6",
3
+ "version": "1.2.7",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {