dff-util 1.2.7 → 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/dist/main/case-mapper.d.ts +1 -1
- package/dist/main/case-mapper.js +14 -13
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export declare function toCamelCase(str: string): string;
|
|
2
|
-
export declare function toSnakeCase(str: string
|
|
2
|
+
export declare function toSnakeCase(str: string): string;
|
|
3
3
|
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;
|
package/dist/main/case-mapper.js
CHANGED
|
@@ -13,9 +13,7 @@ function toCamelCase(str) {
|
|
|
13
13
|
.replace(/[_\s-]+(.)?/g, (_, chr) => (chr ? chr.toUpperCase() : ''))
|
|
14
14
|
.replace(/^[A-Z]/, (chr) => chr.toLowerCase());
|
|
15
15
|
}
|
|
16
|
-
function toSnakeCase(str
|
|
17
|
-
if (str === 'id' && isMongoId)
|
|
18
|
-
return '_id';
|
|
16
|
+
function toSnakeCase(str) {
|
|
19
17
|
return str
|
|
20
18
|
.replace(/([a-z0-9])([A-Z])/g, '$1_$2') // camelCase → camel_Case
|
|
21
19
|
.replace(/[\s-]+/g, '_') // space/dash → underscore
|
|
@@ -44,39 +42,42 @@ function toLowerCase(str) {
|
|
|
44
42
|
.replace(/^_+|_+$/g, '')
|
|
45
43
|
.toLowerCase();
|
|
46
44
|
}
|
|
45
|
+
function isPlainObject(obj) {
|
|
46
|
+
return Object.prototype.toString.call(obj) === '[object Object]';
|
|
47
|
+
}
|
|
47
48
|
function toViewMapper(obj) {
|
|
48
49
|
if (Array.isArray(obj)) {
|
|
49
50
|
return obj.map(toViewMapper);
|
|
50
51
|
}
|
|
51
|
-
else if (obj
|
|
52
|
+
else if (isPlainObject(obj)) {
|
|
52
53
|
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
|
|
53
|
-
toCamelCase(key),
|
|
54
|
+
toCamelCase(key === '_id' ? 'id' : key),
|
|
54
55
|
toViewMapper(value),
|
|
55
56
|
]));
|
|
56
57
|
}
|
|
57
|
-
return obj;
|
|
58
|
+
return obj; // Date, Buffer, Map, etc. returned as-is
|
|
58
59
|
}
|
|
59
60
|
function toEntityMapper(obj) {
|
|
60
61
|
if (Array.isArray(obj)) {
|
|
61
62
|
return obj.map(toEntityMapper);
|
|
62
63
|
}
|
|
63
|
-
else if (obj
|
|
64
|
+
else if (isPlainObject(obj)) {
|
|
64
65
|
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
|
|
65
66
|
toSnakeCase(key),
|
|
66
67
|
toEntityMapper(value),
|
|
67
68
|
]));
|
|
68
69
|
}
|
|
69
|
-
return obj;
|
|
70
|
+
return obj; // Date, Buffer, Map, etc. returned as-is
|
|
70
71
|
}
|
|
71
72
|
function toSchemaMapper(obj) {
|
|
72
73
|
if (Array.isArray(obj)) {
|
|
73
|
-
return obj.map(
|
|
74
|
+
return obj.map(toEntityMapper);
|
|
74
75
|
}
|
|
75
|
-
else if (obj
|
|
76
|
+
else if (isPlainObject(obj)) {
|
|
76
77
|
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
|
|
77
|
-
toSnakeCase(key
|
|
78
|
-
|
|
78
|
+
toSnakeCase(key === 'id' ? '_id' : key),
|
|
79
|
+
toEntityMapper(value),
|
|
79
80
|
]));
|
|
80
81
|
}
|
|
81
|
-
return obj;
|
|
82
|
+
return obj; // Date, Buffer, Map, etc. returned as-is
|
|
82
83
|
}
|