loginterface 1.0.5 → 1.0.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/dist/index.js +8 -3
- package/index.ts +24 -19
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -22,7 +22,7 @@ export const logInterface = (interfaceName) => {
|
|
|
22
22
|
*/
|
|
23
23
|
const generateInterfaces = (obj, rootName = 'Generated') => {
|
|
24
24
|
if (typeof obj !== 'object') {
|
|
25
|
-
console.log(`%cInterface must be
|
|
25
|
+
console.log(`%cInterface must be an object or array`, 'color: #0fbffe; font-size: 14px');
|
|
26
26
|
return;
|
|
27
27
|
}
|
|
28
28
|
const data = obj;
|
|
@@ -41,13 +41,16 @@ const generateInterfaces = (obj, rootName = 'Generated') => {
|
|
|
41
41
|
const resolveType = (value, parentName, propName) => {
|
|
42
42
|
if (value === null)
|
|
43
43
|
return 'any';
|
|
44
|
+
if (value instanceof Date) {
|
|
45
|
+
return 'Date'; // <-- treat Date as primitive type
|
|
46
|
+
}
|
|
44
47
|
if (Array.isArray(value)) {
|
|
45
48
|
if (value.length === 0)
|
|
46
49
|
return 'any[]';
|
|
47
|
-
const firstItem = value.find((v) => v !== null);
|
|
50
|
+
const firstItem = value.find((v) => v !== null && v !== undefined);
|
|
48
51
|
if (!firstItem)
|
|
49
52
|
return 'any[]';
|
|
50
|
-
if (typeof firstItem === 'object' && !Array.isArray(firstItem)) {
|
|
53
|
+
if (typeof firstItem === 'object' && !(firstItem instanceof Date) && !Array.isArray(firstItem)) {
|
|
51
54
|
const interfaceName = parentName + capitalize(propName);
|
|
52
55
|
generateInterface(firstItem, interfaceName);
|
|
53
56
|
return `${interfaceName}[]`;
|
|
@@ -55,6 +58,8 @@ const generateInterfaces = (obj, rootName = 'Generated') => {
|
|
|
55
58
|
return `${resolvePrimitive(firstItem)}[]`;
|
|
56
59
|
}
|
|
57
60
|
if (typeof value === 'object') {
|
|
61
|
+
if (value instanceof Date)
|
|
62
|
+
return 'Date'; // double check
|
|
58
63
|
const interfaceName = parentName + capitalize(propName);
|
|
59
64
|
generateInterface(value, interfaceName);
|
|
60
65
|
return interfaceName;
|
package/index.ts
CHANGED
|
@@ -25,7 +25,7 @@ export const logInterface = (interfaceName: string) => {
|
|
|
25
25
|
const generateInterfaces = (obj: any, rootName = 'Generated') => {
|
|
26
26
|
if(typeof obj !== 'object') {
|
|
27
27
|
console.log(
|
|
28
|
-
`%cInterface must be
|
|
28
|
+
`%cInterface must be an object or array`,
|
|
29
29
|
'color: #0fbffe; font-size: 14px',
|
|
30
30
|
);
|
|
31
31
|
return;
|
|
@@ -47,32 +47,37 @@ const generateInterfaces = (obj: any, rootName = 'Generated') => {
|
|
|
47
47
|
};
|
|
48
48
|
|
|
49
49
|
const resolveType = (value: any, parentName: string, propName: string) => {
|
|
50
|
-
|
|
50
|
+
if (value === null) return 'any';
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
if (value instanceof Date) {
|
|
53
|
+
return 'Date'; // <-- treat Date as primitive type
|
|
54
|
+
}
|
|
54
55
|
|
|
55
|
-
|
|
56
|
+
if (Array.isArray(value)) {
|
|
57
|
+
if (value.length === 0) return 'any[]';
|
|
56
58
|
|
|
57
|
-
|
|
59
|
+
const firstItem = value.find((v) => v !== null && v !== undefined);
|
|
58
60
|
|
|
59
|
-
|
|
60
|
-
const interfaceName = parentName + capitalize(propName);
|
|
61
|
-
generateInterface(firstItem, interfaceName);
|
|
62
|
-
return `${interfaceName}[]`;
|
|
63
|
-
}
|
|
61
|
+
if (!firstItem) return 'any[]';
|
|
64
62
|
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if (typeof value === 'object') {
|
|
63
|
+
if (typeof firstItem === 'object' && !(firstItem instanceof Date) && !Array.isArray(firstItem)) {
|
|
69
64
|
const interfaceName = parentName + capitalize(propName);
|
|
70
|
-
generateInterface(
|
|
71
|
-
return interfaceName
|
|
65
|
+
generateInterface(firstItem, interfaceName);
|
|
66
|
+
return `${interfaceName}[]`;
|
|
72
67
|
}
|
|
73
68
|
|
|
74
|
-
return resolvePrimitive(
|
|
75
|
-
}
|
|
69
|
+
return `${resolvePrimitive(firstItem)}[]`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (typeof value === 'object') {
|
|
73
|
+
if (value instanceof Date) return 'Date'; // double check
|
|
74
|
+
const interfaceName = parentName + capitalize(propName);
|
|
75
|
+
generateInterface(value, interfaceName);
|
|
76
|
+
return interfaceName;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return resolvePrimitive(value);
|
|
80
|
+
};
|
|
76
81
|
|
|
77
82
|
const generateInterface = (obj: any, name: any) => {
|
|
78
83
|
if (processed.has(name)) return;
|