@contentstorage/core 0.3.31 → 0.3.32
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.
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as crypto from 'crypto';
|
|
2
2
|
import { TypeGroup } from './model.js';
|
|
3
|
-
import { findTypeById, getTypeDescriptionGroup, isArray, isDate, isHash, isObject, onlyUnique } from './util.js';
|
|
3
|
+
import { findTypeById, getTypeDescriptionGroup, isArray, isDate, isHash, isObject, onlyUnique, } from './util.js';
|
|
4
4
|
function createTypeDescription(typeObj, isUnion) {
|
|
5
5
|
if (isArray(typeObj)) {
|
|
6
6
|
return {
|
|
@@ -27,12 +27,19 @@ function getIdByType(typeObj, types, isUnion = false) {
|
|
|
27
27
|
return typeDesc.id;
|
|
28
28
|
}
|
|
29
29
|
function Hash(content) {
|
|
30
|
-
|
|
30
|
+
// Create a new SHA-1 hash object
|
|
31
|
+
const sha1Hash = crypto.createHash('sha1');
|
|
32
|
+
// Update the hash object with the content
|
|
33
|
+
sha1Hash.update(content);
|
|
34
|
+
// Calculate the digest in hexadecimal format
|
|
35
|
+
return sha1Hash.digest('hex');
|
|
31
36
|
}
|
|
32
37
|
function typeObjectMatchesTypeDesc(typeObj, typeDesc, isUnion) {
|
|
33
38
|
if (isArray(typeObj)) {
|
|
34
|
-
|
|
35
|
-
|
|
39
|
+
return (
|
|
40
|
+
// @ts-ignore
|
|
41
|
+
arraysContainSameElements(typeObj, typeDesc.arrayOfTypes) &&
|
|
42
|
+
typeDesc.isUnion === isUnion);
|
|
36
43
|
}
|
|
37
44
|
else {
|
|
38
45
|
return objectsHaveSameEntries(typeObj, typeDesc.typeObj);
|
|
@@ -41,7 +48,7 @@ function typeObjectMatchesTypeDesc(typeObj, typeDesc, isUnion) {
|
|
|
41
48
|
function arraysContainSameElements(arr1, arr2) {
|
|
42
49
|
if (arr1 === undefined || arr2 === undefined)
|
|
43
50
|
return false;
|
|
44
|
-
return arr1.sort().join(
|
|
51
|
+
return arr1.sort().join('') === arr2.sort().join('');
|
|
45
52
|
}
|
|
46
53
|
function objectsHaveSameEntries(obj1, obj2) {
|
|
47
54
|
if (obj1 === undefined || obj2 === undefined)
|
|
@@ -56,10 +63,10 @@ function objectsHaveSameEntries(obj1, obj2) {
|
|
|
56
63
|
}
|
|
57
64
|
function getSimpleTypeName(value) {
|
|
58
65
|
if (value === null) {
|
|
59
|
-
return
|
|
66
|
+
return 'null';
|
|
60
67
|
}
|
|
61
68
|
else if (value instanceof Date) {
|
|
62
|
-
return
|
|
69
|
+
return 'Date';
|
|
63
70
|
}
|
|
64
71
|
else {
|
|
65
72
|
return typeof value;
|
|
@@ -126,11 +133,12 @@ function getMergedObjects(typesOfArray, types) {
|
|
|
126
133
|
return getIdByType(typeObj, types, true);
|
|
127
134
|
}
|
|
128
135
|
function toOptionalKey(key) {
|
|
129
|
-
return key.endsWith(
|
|
136
|
+
return key.endsWith('--?') ? key : `${key}--?`;
|
|
130
137
|
}
|
|
131
138
|
function getMergedArrays(typesOfArray, types) {
|
|
132
139
|
// @ts-expect-error
|
|
133
|
-
const idsOfArrayTypes = typesOfArray
|
|
140
|
+
const idsOfArrayTypes = typesOfArray
|
|
141
|
+
?.map((typeDesc) => typeDesc.arrayOfTypes)
|
|
134
142
|
// @ts-expect-error
|
|
135
143
|
.reduce((a, b) => [...a, ...b], [])
|
|
136
144
|
.filter(onlyUnique);
|
|
@@ -155,16 +163,18 @@ function getMergedUnion(typesOfArray, types) {
|
|
|
155
163
|
}
|
|
156
164
|
function getInnerArrayType(typesOfArray, types) {
|
|
157
165
|
// return inner array type
|
|
158
|
-
const containsUndefined = typesOfArray.includes(
|
|
159
|
-
const arrayTypesDescriptions = typesOfArray
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
const
|
|
166
|
+
const containsUndefined = typesOfArray.includes('undefined');
|
|
167
|
+
const arrayTypesDescriptions = typesOfArray
|
|
168
|
+
.map((id) => findTypeById(id, types))
|
|
169
|
+
.filter((_) => !!_);
|
|
170
|
+
const allArrayType = arrayTypesDescriptions.filter((typeDesc) => getTypeDescriptionGroup(typeDesc) === TypeGroup.Array).length === typesOfArray.length;
|
|
171
|
+
const allArrayTypeWithUndefined = arrayTypesDescriptions.filter((typeDesc) => getTypeDescriptionGroup(typeDesc) === TypeGroup.Array).length +
|
|
172
|
+
1 ===
|
|
163
173
|
typesOfArray.length && containsUndefined;
|
|
164
|
-
const allObjectTypeWithUndefined = arrayTypesDescriptions.filter((typeDesc) => getTypeDescriptionGroup(typeDesc) === TypeGroup.Object).length +
|
|
174
|
+
const allObjectTypeWithUndefined = arrayTypesDescriptions.filter((typeDesc) => getTypeDescriptionGroup(typeDesc) === TypeGroup.Object).length +
|
|
175
|
+
1 ===
|
|
165
176
|
typesOfArray.length && containsUndefined;
|
|
166
|
-
const allObjectType = arrayTypesDescriptions.filter((typeDesc) => getTypeDescriptionGroup(typeDesc) === TypeGroup.Object).length ===
|
|
167
|
-
typesOfArray.length;
|
|
177
|
+
const allObjectType = arrayTypesDescriptions.filter((typeDesc) => getTypeDescriptionGroup(typeDesc) === TypeGroup.Object).length === typesOfArray.length;
|
|
168
178
|
if (typesOfArray.length === 0) {
|
|
169
179
|
// no types in array -> empty union type
|
|
170
180
|
return getIdByType([], types, true);
|
|
@@ -183,11 +193,11 @@ function getInnerArrayType(typesOfArray, types) {
|
|
|
183
193
|
return getMergedArrays(arrayTypesDescriptions, types);
|
|
184
194
|
// all array types with posibble undefined, result type = undefined | (*mergedArray*)[]
|
|
185
195
|
if (allArrayTypeWithUndefined) {
|
|
186
|
-
return getMergedUnion([getMergedArrays(arrayTypesDescriptions, types),
|
|
196
|
+
return getMergedUnion([getMergedArrays(arrayTypesDescriptions, types), 'undefined'], types);
|
|
187
197
|
}
|
|
188
198
|
// all object types with posibble undefined, result type = undefined | *mergedObject*
|
|
189
199
|
if (allObjectTypeWithUndefined) {
|
|
190
|
-
return getMergedUnion([getMergedObjects(arrayTypesDescriptions, types),
|
|
200
|
+
return getMergedUnion([getMergedObjects(arrayTypesDescriptions, types), 'undefined'], types);
|
|
191
201
|
}
|
|
192
202
|
// if they are mixed or all primitive we cant merge them so we return as mixed union type
|
|
193
203
|
return getMergedUnion(typesOfArray, types);
|
|
@@ -197,7 +207,9 @@ export function getTypeStructure(targetObj, // object that we want to create typ
|
|
|
197
207
|
types = []) {
|
|
198
208
|
switch (getTypeGroup(targetObj)) {
|
|
199
209
|
case TypeGroup.Array:
|
|
200
|
-
const typesOfArray = targetObj
|
|
210
|
+
const typesOfArray = targetObj
|
|
211
|
+
.map((_) => getTypeStructure(_, types).rootTypeId)
|
|
212
|
+
.filter(onlyUnique);
|
|
201
213
|
const arrayInnerTypeId = getInnerArrayType(typesOfArray, types); // create "union type of array types"
|
|
202
214
|
const typeId = getIdByType([arrayInnerTypeId], types); // create type "array of union type"
|
|
203
215
|
return {
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@contentstorage/core",
|
|
3
3
|
"author": "Kaido Hussar <kaidohus@gmail.com>",
|
|
4
4
|
"homepage": "https://contentstorage.app",
|
|
5
|
-
"version": "0.3.
|
|
5
|
+
"version": "0.3.32",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"description": "Fetch content from contentstorage and generate TypeScript types",
|
|
8
8
|
"module": "dist/index.js",
|
|
@@ -28,7 +28,6 @@
|
|
|
28
28
|
"axios": "^1.7.2",
|
|
29
29
|
"chalk": "^4.1.2",
|
|
30
30
|
"es7-shim": "^6.0.0",
|
|
31
|
-
"hash.js": "^1.0.3",
|
|
32
31
|
"pluralize": "^3.1.0"
|
|
33
32
|
},
|
|
34
33
|
"devDependencies": {
|