inibase 1.0.0-rc.46 → 1.0.0-rc.47
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.d.ts +4 -5
- package/dist/index.js +62 -62
- package/dist/utils.d.ts +3 -1
- package/dist/utils.js +54 -34
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -74,8 +74,7 @@ export default class Inibase {
|
|
|
74
74
|
private _schemaToIdsPath;
|
|
75
75
|
setTableSchema(tableName: string, schema: Schema): Promise<void>;
|
|
76
76
|
getTableSchema(tableName: string): Promise<Schema | undefined>;
|
|
77
|
-
|
|
78
|
-
static getField(keyPath: string, schema: Schema): Field | null;
|
|
77
|
+
getSchemaWhenTableNotEmpty(tableName: string, schema?: Schema): Promise<never | Schema>;
|
|
79
78
|
private validateData;
|
|
80
79
|
private formatField;
|
|
81
80
|
private formatData;
|
|
@@ -96,9 +95,9 @@ export default class Inibase {
|
|
|
96
95
|
post(tableName: string, data: Data | Data[], options?: Options, returnPostedData?: boolean): Promise<void | null>;
|
|
97
96
|
post(tableName: string, data: Data, options: Options | undefined, returnPostedData: true): Promise<Data | null>;
|
|
98
97
|
post(tableName: string, data: Data[], options: Options | undefined, returnPostedData: true): Promise<Data[] | null>;
|
|
99
|
-
put(tableName: string, data: Data | Data[], where?: number | string | (number | string)[] | Criteria, options?: Options,
|
|
100
|
-
put(tableName: string, data: Data, where: number | string | (number | string)[] | Criteria | undefined, options: Options | undefined,
|
|
101
|
-
put(tableName: string, data: Data[], where: number | string | (number | string)[] | Criteria | undefined, options: Options | undefined,
|
|
98
|
+
put(tableName: string, data: Data | Data[], where?: number | string | (number | string)[] | Criteria, options?: Options, returnUpdatedData?: false): Promise<void | null>;
|
|
99
|
+
put(tableName: string, data: Data, where: number | string | (number | string)[] | Criteria | undefined, options: Options | undefined, returnUpdatedData: true): Promise<Data | null>;
|
|
100
|
+
put(tableName: string, data: Data[], where: number | string | (number | string)[] | Criteria | undefined, options: Options | undefined, returnUpdatedData: true): Promise<Data[] | null>;
|
|
102
101
|
delete(tableName: string, where?: number | string, _id?: string | string[]): Promise<string | null>;
|
|
103
102
|
delete(tableName: string, where?: (number | string)[] | Criteria, _id?: string | string[]): Promise<string[] | null>;
|
|
104
103
|
sum(tableName: string, columns: string, where?: number | string | (number | string)[] | Criteria): Promise<number>;
|
package/dist/index.js
CHANGED
|
@@ -147,43 +147,31 @@ export default class Inibase {
|
|
|
147
147
|
},
|
|
148
148
|
];
|
|
149
149
|
}
|
|
150
|
-
async
|
|
151
|
-
const
|
|
150
|
+
async getSchemaWhenTableNotEmpty(tableName, schema) {
|
|
151
|
+
const tablePath = join(this.folder, this.database, tableName);
|
|
152
|
+
if (!schema)
|
|
153
|
+
schema = await this.getTableSchema(tableName);
|
|
152
154
|
if (!schema)
|
|
153
155
|
throw this.throwError("NO_SCHEMA", tableName);
|
|
154
156
|
if (!(await File.isExists(join(tablePath, "id.inib"))))
|
|
155
157
|
throw this.throwError("NO_ITEMS", tableName);
|
|
156
158
|
return schema;
|
|
157
159
|
}
|
|
158
|
-
static getField(keyPath, schema) {
|
|
159
|
-
let RETURN = null;
|
|
160
|
-
const keyPathSplited = keyPath.split(".");
|
|
161
|
-
for (const [index, key] of keyPathSplited.entries()) {
|
|
162
|
-
const foundItem = schema.find((item) => item.key === key);
|
|
163
|
-
if (!foundItem)
|
|
164
|
-
return null;
|
|
165
|
-
if (index === keyPathSplited.length - 1)
|
|
166
|
-
RETURN = foundItem;
|
|
167
|
-
if ((foundItem.type === "array" || foundItem.type === "object") &&
|
|
168
|
-
foundItem.children &&
|
|
169
|
-
Utils.isArrayOfObjects(foundItem.children))
|
|
170
|
-
RETURN = foundItem.children;
|
|
171
|
-
}
|
|
172
|
-
if (!RETURN)
|
|
173
|
-
return null;
|
|
174
|
-
return Utils.isArrayOfObjects(RETURN) ? RETURN[0] : RETURN;
|
|
175
|
-
}
|
|
176
160
|
validateData(data, schema, skipRequiredField = false) {
|
|
177
161
|
if (Utils.isArrayOfObjects(data))
|
|
178
162
|
for (const single_data of data)
|
|
179
163
|
this.validateData(single_data, schema, skipRequiredField);
|
|
180
164
|
else if (Utils.isObject(data)) {
|
|
181
165
|
for (const field of schema) {
|
|
182
|
-
if (!
|
|
183
|
-
field.
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
166
|
+
if (!Object.hasOwn(data, field.key) ||
|
|
167
|
+
data[field.key] === null ||
|
|
168
|
+
data[field.key] === undefined) {
|
|
169
|
+
if (field.required && !skipRequiredField)
|
|
170
|
+
throw this.throwError("FIELD_REQUIRED", field.key);
|
|
171
|
+
else
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (Object.hasOwn(data, field.key) &&
|
|
187
175
|
!Utils.validateFieldType(data[field.key], field.type, (field.type === "array" || field.type === "object") &&
|
|
188
176
|
field.children &&
|
|
189
177
|
!Utils.isArrayOfObjects(field.children)
|
|
@@ -203,14 +191,15 @@ export default class Inibase {
|
|
|
203
191
|
}
|
|
204
192
|
formatField(value, field, formatOnlyAvailiableKeys) {
|
|
205
193
|
if (Array.isArray(field.type))
|
|
206
|
-
field.type = Utils.detectFieldType(value, field.type) ??
|
|
194
|
+
field.type = (Utils.detectFieldType(value, field.type) ??
|
|
195
|
+
field.type[0]);
|
|
207
196
|
switch (field.type) {
|
|
208
197
|
case "array":
|
|
209
198
|
if (typeof field.children === "string") {
|
|
210
199
|
if (field.children === "table") {
|
|
211
200
|
if (Array.isArray(value)) {
|
|
212
201
|
if (Utils.isArrayOfObjects(value)) {
|
|
213
|
-
if (value.every((item) =>
|
|
202
|
+
if (value.every((item) => Object.hasOwn(item, "id") &&
|
|
214
203
|
(Utils.isValidID(item.id) || Utils.isNumber(item.id))))
|
|
215
204
|
value.map((item) => item.id
|
|
216
205
|
? Utils.isNumber(item.id)
|
|
@@ -245,7 +234,7 @@ export default class Inibase {
|
|
|
245
234
|
if (Array.isArray(value))
|
|
246
235
|
value = value[0];
|
|
247
236
|
if (Utils.isObject(value)) {
|
|
248
|
-
if (
|
|
237
|
+
if (Object.hasOwn(value, "id") &&
|
|
249
238
|
(Utils.isValidID(value.id) ||
|
|
250
239
|
Utils.isNumber(value.id)))
|
|
251
240
|
return Utils.isNumber(value.id)
|
|
@@ -287,7 +276,7 @@ export default class Inibase {
|
|
|
287
276
|
else if (Utils.isObject(data)) {
|
|
288
277
|
let RETURN = {};
|
|
289
278
|
for (const field of schema) {
|
|
290
|
-
if (!
|
|
279
|
+
if (!Object.hasOwn(data, field.key)) {
|
|
291
280
|
if (formatOnlyAvailiableKeys || !field.required)
|
|
292
281
|
continue;
|
|
293
282
|
RETURN[field.key] = this.getDefaultValue(field);
|
|
@@ -468,19 +457,22 @@ export default class Inibase {
|
|
|
468
457
|
else if (field.children === "table" ||
|
|
469
458
|
(Array.isArray(field.type) && field.type.includes("table")) ||
|
|
470
459
|
(Array.isArray(field.children) && field.children.includes("table"))) {
|
|
471
|
-
if (
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
.
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
await
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
460
|
+
if ((await File.isExists(join(this.folder, this.database, field.key))) &&
|
|
461
|
+
(await File.isExists(join(tablePath, (prefix ?? "") + field.key + ".inib")))) {
|
|
462
|
+
if (options.columns)
|
|
463
|
+
options.columns = options.columns
|
|
464
|
+
.filter((column) => column.includes(`${field.key}.`))
|
|
465
|
+
.map((column) => column.replace(`${field.key}.`, ""));
|
|
466
|
+
const items = await File.get(join(tablePath, (prefix ?? "") + field.key + ".inib"), linesNumber, field.type, field.children, this.salt);
|
|
467
|
+
if (items)
|
|
468
|
+
await Promise.allSettled(Object.entries(items).map(async ([index, item]) => {
|
|
469
|
+
if (!RETURN[index])
|
|
470
|
+
RETURN[index] = {};
|
|
471
|
+
RETURN[index][field.key] = item
|
|
472
|
+
? await this.get(field.key, item, options)
|
|
473
|
+
: this.getDefaultValue(field);
|
|
474
|
+
}));
|
|
475
|
+
}
|
|
484
476
|
}
|
|
485
477
|
else if (await File.isExists(join(tablePath, (prefix ?? "") + field.key + ".inib"))) {
|
|
486
478
|
const items = await File.get(join(tablePath, (prefix ?? "") + field.key + ".inib"), linesNumber, field.type, field?.children, this.salt);
|
|
@@ -516,13 +508,13 @@ export default class Inibase {
|
|
|
516
508
|
.map((column) => column.replace(`${field.key}.`, ""));
|
|
517
509
|
const items = await File.get(join(tablePath, (prefix ?? "") + field.key + ".inib"), linesNumber, "number", undefined, this.salt);
|
|
518
510
|
if (items)
|
|
519
|
-
|
|
511
|
+
await Promise.allSettled(Object.entries(items).map(async ([index, item]) => {
|
|
520
512
|
if (!RETURN[index])
|
|
521
513
|
RETURN[index] = {};
|
|
522
514
|
RETURN[index][field.key] = item
|
|
523
515
|
? await this.get(field.key, item, options)
|
|
524
516
|
: this.getDefaultValue(field);
|
|
525
|
-
}
|
|
517
|
+
}));
|
|
526
518
|
}
|
|
527
519
|
}
|
|
528
520
|
else if (await File.isExists(join(tablePath, (prefix ?? "") + field.key + ".inib"))) {
|
|
@@ -570,7 +562,7 @@ export default class Inibase {
|
|
|
570
562
|
allTrue = true;
|
|
571
563
|
let index = -1;
|
|
572
564
|
for await (const [key, value] of Object.entries(criteria)) {
|
|
573
|
-
const field =
|
|
565
|
+
const field = Utils.getField(key, schema);
|
|
574
566
|
index++;
|
|
575
567
|
let searchOperator = undefined, searchComparedAtValue = undefined, searchLogicalOperator = undefined;
|
|
576
568
|
if (Utils.isObject(value)) {
|
|
@@ -693,9 +685,13 @@ export default class Inibase {
|
|
|
693
685
|
options.page = options.page || 1;
|
|
694
686
|
options.perPage = options.perPage || 15;
|
|
695
687
|
let RETURN;
|
|
696
|
-
let schema =
|
|
688
|
+
let schema = await this.getSchemaWhenTableNotEmpty(tableName, tableSchema);
|
|
697
689
|
if (options.columns && options.columns.length)
|
|
698
690
|
schema = this._filterSchemaByColumns(schema, options.columns);
|
|
691
|
+
if (where &&
|
|
692
|
+
((Array.isArray(where) && !where.length) ||
|
|
693
|
+
(Utils.isObject(where) && !Object.keys(where).length)))
|
|
694
|
+
where = undefined;
|
|
699
695
|
if (!where) {
|
|
700
696
|
// Display all data
|
|
701
697
|
RETURN = Object.values(await this.getItemsFromSchema(tableName, schema, Array.from({ length: options.perPage }, (_, index) => (options.page - 1) * options.perPage +
|
|
@@ -841,9 +837,10 @@ export default class Inibase {
|
|
|
841
837
|
: await File.append(path, content))));
|
|
842
838
|
await Promise.all(renameList.map(async ([tempPath, filePath]) => rename(tempPath, filePath)));
|
|
843
839
|
renameList = [];
|
|
840
|
+
totalItems += Array.isArray(RETURN) ? RETURN.length : 1;
|
|
844
841
|
if (Config.isCacheEnabled) {
|
|
845
842
|
await this.clearCache(tablePath);
|
|
846
|
-
await File.write(join(tablePath, ".cache", "pagination.inib"), `${lastId},${totalItems
|
|
843
|
+
await File.write(join(tablePath, ".cache", "pagination.inib"), `${lastId},${totalItems}`, true);
|
|
847
844
|
}
|
|
848
845
|
if (returnPostedData)
|
|
849
846
|
return this.get(tableName, Config.isReverseEnabled
|
|
@@ -864,19 +861,19 @@ export default class Inibase {
|
|
|
864
861
|
async put(tableName, data, where, options = {
|
|
865
862
|
page: 1,
|
|
866
863
|
perPage: 15,
|
|
867
|
-
},
|
|
864
|
+
}, returnUpdatedData) {
|
|
868
865
|
let renameList = [];
|
|
869
|
-
const tablePath = join(this.folder, this.database, tableName), schema = await this.
|
|
866
|
+
const tablePath = join(this.folder, this.database, tableName), schema = await this.getSchemaWhenTableNotEmpty(tableName);
|
|
870
867
|
data = this.formatData(data, schema, true);
|
|
871
868
|
if (!where) {
|
|
872
869
|
if (Utils.isArrayOfObjects(data)) {
|
|
873
|
-
if (!data.every((item) =>
|
|
870
|
+
if (!data.every((item) => Object.hasOwn(item, "id") && Utils.isValidID(item.id)))
|
|
874
871
|
throw this.throwError("INVALID_ID");
|
|
875
872
|
return this.put(tableName, data, data
|
|
876
873
|
.filter(({ id }) => id !== undefined)
|
|
877
874
|
.map(({ id }) => id));
|
|
878
875
|
}
|
|
879
|
-
else if (
|
|
876
|
+
else if (Object.hasOwn(data, "id")) {
|
|
880
877
|
if (!Utils.isValidID(data.id))
|
|
881
878
|
throw this.throwError("INVALID_ID", data.id);
|
|
882
879
|
return this.put(tableName, data, data.id);
|
|
@@ -909,8 +906,8 @@ export default class Inibase {
|
|
|
909
906
|
await Promise.all(renameList.map(async ([tempPath, filePath]) => rename(tempPath, filePath)));
|
|
910
907
|
if (Config.isCacheEnabled)
|
|
911
908
|
await this.clearCache(join(tablePath, ".cache"));
|
|
912
|
-
if (
|
|
913
|
-
return this.get(tableName, where, options, undefined, undefined, schema);
|
|
909
|
+
if (returnUpdatedData)
|
|
910
|
+
return await this.get(tableName, where, options, undefined, undefined, schema);
|
|
914
911
|
}
|
|
915
912
|
finally {
|
|
916
913
|
if (renameList.length)
|
|
@@ -951,7 +948,7 @@ export default class Inibase {
|
|
|
951
948
|
renameList = [];
|
|
952
949
|
if (Config.isCacheEnabled)
|
|
953
950
|
await this.clearCache(tablePath);
|
|
954
|
-
if (
|
|
951
|
+
if (returnUpdatedData)
|
|
955
952
|
return this.get(tableName, where, options, !Array.isArray(where), undefined, schema);
|
|
956
953
|
}
|
|
957
954
|
finally {
|
|
@@ -962,14 +959,17 @@ export default class Inibase {
|
|
|
962
959
|
}
|
|
963
960
|
else if (Utils.isObject(where)) {
|
|
964
961
|
const lineNumbers = await this.get(tableName, where, undefined, undefined, true, schema);
|
|
965
|
-
|
|
962
|
+
if (returnUpdatedData)
|
|
963
|
+
return this.put(tableName, data, lineNumbers, options, returnUpdatedData);
|
|
964
|
+
else
|
|
965
|
+
await this.put(tableName, data, lineNumbers, options, returnUpdatedData);
|
|
966
966
|
}
|
|
967
967
|
else
|
|
968
968
|
throw this.throwError("INVALID_PARAMETERS");
|
|
969
969
|
}
|
|
970
970
|
async delete(tableName, where, _id) {
|
|
971
971
|
let renameList = [];
|
|
972
|
-
const tablePath = join(this.folder, this.database, tableName), schema = await this.
|
|
972
|
+
const tablePath = join(this.folder, this.database, tableName), schema = await this.getSchemaWhenTableNotEmpty(tableName);
|
|
973
973
|
if (!where) {
|
|
974
974
|
try {
|
|
975
975
|
await File.lock(join(tablePath, ".tmp"));
|
|
@@ -1035,7 +1035,7 @@ export default class Inibase {
|
|
|
1035
1035
|
}
|
|
1036
1036
|
async sum(tableName, columns, where) {
|
|
1037
1037
|
let RETURN = {};
|
|
1038
|
-
const tablePath = join(this.folder, this.database, tableName), schema = await this.
|
|
1038
|
+
const tablePath = join(this.folder, this.database, tableName), schema = await this.getSchemaWhenTableNotEmpty(tableName);
|
|
1039
1039
|
if (!Array.isArray(columns))
|
|
1040
1040
|
columns = [columns];
|
|
1041
1041
|
for await (const column of columns) {
|
|
@@ -1055,7 +1055,7 @@ export default class Inibase {
|
|
|
1055
1055
|
}
|
|
1056
1056
|
async max(tableName, columns, where) {
|
|
1057
1057
|
let RETURN = {};
|
|
1058
|
-
const tablePath = join(this.folder, this.database, tableName), schema = await this.
|
|
1058
|
+
const tablePath = join(this.folder, this.database, tableName), schema = await this.getSchemaWhenTableNotEmpty(tableName);
|
|
1059
1059
|
if (!Array.isArray(columns))
|
|
1060
1060
|
columns = [columns];
|
|
1061
1061
|
for await (const column of columns) {
|
|
@@ -1075,7 +1075,7 @@ export default class Inibase {
|
|
|
1075
1075
|
}
|
|
1076
1076
|
async min(tableName, columns, where) {
|
|
1077
1077
|
let RETURN = {};
|
|
1078
|
-
const tablePath = join(this.folder, this.database, tableName), schema = await this.
|
|
1078
|
+
const tablePath = join(this.folder, this.database, tableName), schema = await this.getSchemaWhenTableNotEmpty(tableName);
|
|
1079
1079
|
if (!Array.isArray(columns))
|
|
1080
1080
|
columns = [columns];
|
|
1081
1081
|
for await (const column of columns) {
|
|
@@ -1098,7 +1098,7 @@ export default class Inibase {
|
|
|
1098
1098
|
perPage: 15,
|
|
1099
1099
|
}) {
|
|
1100
1100
|
// TO-DO: Cache Results based on "Columns and Sort Direction"
|
|
1101
|
-
const tablePath = join(this.folder, this.database, tableName), schema = await this.
|
|
1101
|
+
const tablePath = join(this.folder, this.database, tableName), schema = await this.getSchemaWhenTableNotEmpty(tableName);
|
|
1102
1102
|
// Default values for page and perPage
|
|
1103
1103
|
options.page = options.page || 1;
|
|
1104
1104
|
options.perPage = options.perPage || 15;
|
|
@@ -1138,7 +1138,7 @@ export default class Inibase {
|
|
|
1138
1138
|
let index = 2;
|
|
1139
1139
|
const sortColumns = sortArray
|
|
1140
1140
|
.map(([key, ascending], i) => {
|
|
1141
|
-
const field =
|
|
1141
|
+
const field = Utils.getField(key, schema);
|
|
1142
1142
|
if (field)
|
|
1143
1143
|
return `-k${i + index},${i + index}${field.type === "number" ? "n" : ""}${!ascending ? "r" : ""}`;
|
|
1144
1144
|
else
|
|
@@ -1171,7 +1171,7 @@ export default class Inibase {
|
|
|
1171
1171
|
const outputObject = {};
|
|
1172
1172
|
// Extract values for each file, including "id.inib"
|
|
1173
1173
|
filesPathes.forEach((fileName, index) => {
|
|
1174
|
-
const Field =
|
|
1174
|
+
const Field = Utils.getField(parse(fileName).name, schema);
|
|
1175
1175
|
if (Field)
|
|
1176
1176
|
outputObject[Field.key] = File.decode(splitedFileColumns[index], Field?.type, Field?.children, this.salt);
|
|
1177
1177
|
});
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { FieldType, ComparisonOperator } from "./index.js";
|
|
1
|
+
import type { FieldType, ComparisonOperator, Field, Schema } from "./index.js";
|
|
2
2
|
/**
|
|
3
3
|
* Type guard function to check if the input is an array of objects.
|
|
4
4
|
*
|
|
@@ -180,6 +180,7 @@ export declare function FormatObjectCriteriaValue(value: string, isParentArray?:
|
|
|
180
180
|
];
|
|
181
181
|
type ValidKey = number | string;
|
|
182
182
|
export declare const swapKeyValue: <K extends ValidKey, V extends ValidKey>(object: Record<K, V>) => Record<V, K>;
|
|
183
|
+
export declare function getField(keyPath: string, schema: Schema): Field | null;
|
|
183
184
|
export default class Utils {
|
|
184
185
|
static isNumber: (input: any) => input is number;
|
|
185
186
|
static isObject: (obj: any) => obj is Record<any, any>;
|
|
@@ -202,5 +203,6 @@ export default class Utils {
|
|
|
202
203
|
static isArrayOfNulls: (input: any) => input is null[] | null[][];
|
|
203
204
|
static FormatObjectCriteriaValue: typeof FormatObjectCriteriaValue;
|
|
204
205
|
static swapKeyValue: <K extends ValidKey, V extends ValidKey>(object: Record<K, V>) => Record<V, K>;
|
|
206
|
+
static getField: typeof getField;
|
|
205
207
|
}
|
|
206
208
|
export {};
|
package/dist/utils.js
CHANGED
|
@@ -216,7 +216,7 @@ export const isValidID = (input) => {
|
|
|
216
216
|
* @param {string} str - The string to be checked.
|
|
217
217
|
* @returns {boolean} Returns true if the string is valid JSON, otherwise false.
|
|
218
218
|
*/
|
|
219
|
-
export const isJSON = (str) => str[0] === "{" || str[0] === "[";
|
|
219
|
+
export const isJSON = (str) => str === "null" || str === "undefined" || str[0] === "{" || str[0] === "[";
|
|
220
220
|
/**
|
|
221
221
|
* Identifies and returns properties that have changed between two objects.
|
|
222
222
|
*
|
|
@@ -239,42 +239,43 @@ export const findChangedProperties = (obj1, obj2) => {
|
|
|
239
239
|
* @returns The detected field type as a string, or undefined if no matching type is found.
|
|
240
240
|
*/
|
|
241
241
|
export const detectFieldType = (input, availableTypes) => {
|
|
242
|
-
if (
|
|
243
|
-
if ((input
|
|
244
|
-
input === "
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
if (
|
|
242
|
+
if (input !== null && input !== undefined)
|
|
243
|
+
if (!Array.isArray(input)) {
|
|
244
|
+
if ((input === "0" ||
|
|
245
|
+
input === "1" ||
|
|
246
|
+
input === "true" ||
|
|
247
|
+
input === "false") &&
|
|
248
|
+
availableTypes.includes("boolean"))
|
|
249
|
+
return "boolean";
|
|
250
|
+
else if (isNumber(input)) {
|
|
251
|
+
if (availableTypes.includes("table"))
|
|
252
|
+
return "table";
|
|
253
|
+
else if (availableTypes.includes("date"))
|
|
254
|
+
return "date";
|
|
255
|
+
else if (availableTypes.includes("number"))
|
|
256
|
+
return "number";
|
|
257
|
+
}
|
|
258
|
+
else if (availableTypes.includes("table") && isValidID(input))
|
|
251
259
|
return "table";
|
|
252
|
-
else if (availableTypes.includes("
|
|
253
|
-
return "
|
|
254
|
-
else if (availableTypes.includes("
|
|
255
|
-
return "
|
|
260
|
+
else if (input.startsWith("[") && availableTypes.includes("array"))
|
|
261
|
+
return "array";
|
|
262
|
+
else if (availableTypes.includes("email") && isEmail(input))
|
|
263
|
+
return "email";
|
|
264
|
+
else if (availableTypes.includes("url") && isURL(input))
|
|
265
|
+
return "url";
|
|
266
|
+
else if (availableTypes.includes("password") && isPassword(input))
|
|
267
|
+
return "password";
|
|
268
|
+
else if (availableTypes.includes("json") && isJSON(input))
|
|
269
|
+
return "json";
|
|
270
|
+
else if (availableTypes.includes("json") && isDate(input))
|
|
271
|
+
return "json";
|
|
272
|
+
else if (availableTypes.includes("string") && isString(input))
|
|
273
|
+
return "string";
|
|
274
|
+
else if (availableTypes.includes("ip") && isIP(input))
|
|
275
|
+
return "ip";
|
|
256
276
|
}
|
|
257
|
-
else
|
|
258
|
-
return "table";
|
|
259
|
-
else if (input.includes(",") && availableTypes.includes("array"))
|
|
277
|
+
else
|
|
260
278
|
return "array";
|
|
261
|
-
else if (availableTypes.includes("email") && isEmail(input))
|
|
262
|
-
return "email";
|
|
263
|
-
else if (availableTypes.includes("url") && isURL(input))
|
|
264
|
-
return "url";
|
|
265
|
-
else if (availableTypes.includes("password") && isPassword(input))
|
|
266
|
-
return "password";
|
|
267
|
-
else if (availableTypes.includes("json") && isJSON(input))
|
|
268
|
-
return "json";
|
|
269
|
-
else if (availableTypes.includes("json") && isDate(input))
|
|
270
|
-
return "json";
|
|
271
|
-
else if (availableTypes.includes("string") && isString(input))
|
|
272
|
-
return "string";
|
|
273
|
-
else if (availableTypes.includes("ip") && isIP(input))
|
|
274
|
-
return "ip";
|
|
275
|
-
}
|
|
276
|
-
else
|
|
277
|
-
return "array";
|
|
278
279
|
return undefined;
|
|
279
280
|
};
|
|
280
281
|
/**
|
|
@@ -393,6 +394,24 @@ export function FormatObjectCriteriaValue(value, isParentArray = false) {
|
|
|
393
394
|
}
|
|
394
395
|
}
|
|
395
396
|
export const swapKeyValue = (object) => Object.entries(object).reduce((swapped, [key, value]) => ({ ...swapped, [value]: key }), {});
|
|
397
|
+
export function getField(keyPath, schema) {
|
|
398
|
+
let RETURN = null;
|
|
399
|
+
const keyPathSplited = keyPath.split(".");
|
|
400
|
+
for (const [index, key] of keyPathSplited.entries()) {
|
|
401
|
+
const foundItem = schema.find((item) => item.key === key);
|
|
402
|
+
if (!foundItem)
|
|
403
|
+
return null;
|
|
404
|
+
if (index === keyPathSplited.length - 1)
|
|
405
|
+
RETURN = foundItem;
|
|
406
|
+
if ((foundItem.type === "array" || foundItem.type === "object") &&
|
|
407
|
+
foundItem.children &&
|
|
408
|
+
isArrayOfObjects(foundItem.children))
|
|
409
|
+
RETURN = foundItem.children;
|
|
410
|
+
}
|
|
411
|
+
if (!RETURN)
|
|
412
|
+
return null;
|
|
413
|
+
return isArrayOfObjects(RETURN) ? RETURN[0] : RETURN;
|
|
414
|
+
}
|
|
396
415
|
export default class Utils {
|
|
397
416
|
static isNumber = isNumber;
|
|
398
417
|
static isObject = isObject;
|
|
@@ -415,4 +434,5 @@ export default class Utils {
|
|
|
415
434
|
static isArrayOfNulls = isArrayOfNulls;
|
|
416
435
|
static FormatObjectCriteriaValue = FormatObjectCriteriaValue;
|
|
417
436
|
static swapKeyValue = swapKeyValue;
|
|
437
|
+
static getField = getField;
|
|
418
438
|
}
|