inibase 1.0.0-rc.46 → 1.0.0-rc.48
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/file.js +1 -17
- package/dist/index.d.ts +4 -5
- package/dist/index.js +62 -65
- package/dist/utils.d.ts +3 -1
- package/dist/utils.js +54 -34
- package/package.json +1 -1
package/dist/file.js
CHANGED
|
@@ -99,13 +99,6 @@ const secureString = (input) => {
|
|
|
99
99
|
// Replace characters using a single regular expression.
|
|
100
100
|
return decodedInput.replace(/\\n/g, "\n").replace(/\n/g, "\\n");
|
|
101
101
|
};
|
|
102
|
-
/**
|
|
103
|
-
* Secures each element in an array or a single value using secureString.
|
|
104
|
-
*
|
|
105
|
-
* @param arr_str - An array or a single value of any type.
|
|
106
|
-
* @returns An array with each element secured, or a single secured value.
|
|
107
|
-
*/
|
|
108
|
-
const secureArray = (arr_str) => Array.isArray(arr_str) ? arr_str.map(secureArray) : secureString(arr_str);
|
|
109
102
|
/**
|
|
110
103
|
* Encodes the input using 'secureString' and 'Inison.stringify' functions.
|
|
111
104
|
* If the input is an array, it is first secured and then joined into a string.
|
|
@@ -114,16 +107,7 @@ const secureArray = (arr_str) => Array.isArray(arr_str) ? arr_str.map(secureArra
|
|
|
114
107
|
* @param input - A value or array of values (string, number, boolean, null).
|
|
115
108
|
* @returns The secured and/or joined string.
|
|
116
109
|
*/
|
|
117
|
-
export const encode = (input) => Array.isArray(input)
|
|
118
|
-
? Inison.stringify(secureArray(input))
|
|
119
|
-
: secureString(input);
|
|
120
|
-
/**
|
|
121
|
-
* Decodes each element in an array or a single value using unSecureString.
|
|
122
|
-
*
|
|
123
|
-
* @param arr_str - An array or a single value of any type.
|
|
124
|
-
* @returns An array with each element decoded, or a single decoded value.
|
|
125
|
-
*/
|
|
126
|
-
const unSecureArray = (arr_str) => Array.isArray(arr_str) ? arr_str.map(unSecureArray) : unSecureString(arr_str);
|
|
110
|
+
export const encode = (input) => Array.isArray(input) ? Inison.stringify(input) : secureString(input);
|
|
127
111
|
/**
|
|
128
112
|
* Reverses the encoding done by 'secureString'. Replaces encoded characters with their original symbols.
|
|
129
113
|
*
|
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
|
@@ -8,7 +8,6 @@ import Utils from "./utils.js";
|
|
|
8
8
|
import UtilsServer from "./utils.server.js";
|
|
9
9
|
import Config from "./config.js";
|
|
10
10
|
import { inspect } from "node:util";
|
|
11
|
-
import Inison from "inison";
|
|
12
11
|
export default class Inibase {
|
|
13
12
|
folder;
|
|
14
13
|
database;
|
|
@@ -147,43 +146,31 @@ export default class Inibase {
|
|
|
147
146
|
},
|
|
148
147
|
];
|
|
149
148
|
}
|
|
150
|
-
async
|
|
151
|
-
const
|
|
149
|
+
async getSchemaWhenTableNotEmpty(tableName, schema) {
|
|
150
|
+
const tablePath = join(this.folder, this.database, tableName);
|
|
151
|
+
if (!schema)
|
|
152
|
+
schema = await this.getTableSchema(tableName);
|
|
152
153
|
if (!schema)
|
|
153
154
|
throw this.throwError("NO_SCHEMA", tableName);
|
|
154
155
|
if (!(await File.isExists(join(tablePath, "id.inib"))))
|
|
155
156
|
throw this.throwError("NO_ITEMS", tableName);
|
|
156
157
|
return schema;
|
|
157
158
|
}
|
|
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
159
|
validateData(data, schema, skipRequiredField = false) {
|
|
177
160
|
if (Utils.isArrayOfObjects(data))
|
|
178
161
|
for (const single_data of data)
|
|
179
162
|
this.validateData(single_data, schema, skipRequiredField);
|
|
180
163
|
else if (Utils.isObject(data)) {
|
|
181
164
|
for (const field of schema) {
|
|
182
|
-
if (!
|
|
183
|
-
field.
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
165
|
+
if (!Object.hasOwn(data, field.key) ||
|
|
166
|
+
data[field.key] === null ||
|
|
167
|
+
data[field.key] === undefined) {
|
|
168
|
+
if (field.required && !skipRequiredField)
|
|
169
|
+
throw this.throwError("FIELD_REQUIRED", field.key);
|
|
170
|
+
else
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
if (Object.hasOwn(data, field.key) &&
|
|
187
174
|
!Utils.validateFieldType(data[field.key], field.type, (field.type === "array" || field.type === "object") &&
|
|
188
175
|
field.children &&
|
|
189
176
|
!Utils.isArrayOfObjects(field.children)
|
|
@@ -203,14 +190,15 @@ export default class Inibase {
|
|
|
203
190
|
}
|
|
204
191
|
formatField(value, field, formatOnlyAvailiableKeys) {
|
|
205
192
|
if (Array.isArray(field.type))
|
|
206
|
-
field.type = Utils.detectFieldType(value, field.type) ??
|
|
193
|
+
field.type = (Utils.detectFieldType(value, field.type) ??
|
|
194
|
+
field.type[0]);
|
|
207
195
|
switch (field.type) {
|
|
208
196
|
case "array":
|
|
209
197
|
if (typeof field.children === "string") {
|
|
210
198
|
if (field.children === "table") {
|
|
211
199
|
if (Array.isArray(value)) {
|
|
212
200
|
if (Utils.isArrayOfObjects(value)) {
|
|
213
|
-
if (value.every((item) =>
|
|
201
|
+
if (value.every((item) => Object.hasOwn(item, "id") &&
|
|
214
202
|
(Utils.isValidID(item.id) || Utils.isNumber(item.id))))
|
|
215
203
|
value.map((item) => item.id
|
|
216
204
|
? Utils.isNumber(item.id)
|
|
@@ -245,7 +233,7 @@ export default class Inibase {
|
|
|
245
233
|
if (Array.isArray(value))
|
|
246
234
|
value = value[0];
|
|
247
235
|
if (Utils.isObject(value)) {
|
|
248
|
-
if (
|
|
236
|
+
if (Object.hasOwn(value, "id") &&
|
|
249
237
|
(Utils.isValidID(value.id) ||
|
|
250
238
|
Utils.isNumber(value.id)))
|
|
251
239
|
return Utils.isNumber(value.id)
|
|
@@ -273,8 +261,6 @@ export default class Inibase {
|
|
|
273
261
|
return Utils.isNumber(value)
|
|
274
262
|
? value
|
|
275
263
|
: UtilsServer.decodeID(value, this.salt);
|
|
276
|
-
case "json":
|
|
277
|
-
return Inison.stringify(value);
|
|
278
264
|
default:
|
|
279
265
|
return value;
|
|
280
266
|
}
|
|
@@ -287,7 +273,7 @@ export default class Inibase {
|
|
|
287
273
|
else if (Utils.isObject(data)) {
|
|
288
274
|
let RETURN = {};
|
|
289
275
|
for (const field of schema) {
|
|
290
|
-
if (!
|
|
276
|
+
if (!Object.hasOwn(data, field.key)) {
|
|
291
277
|
if (formatOnlyAvailiableKeys || !field.required)
|
|
292
278
|
continue;
|
|
293
279
|
RETURN[field.key] = this.getDefaultValue(field);
|
|
@@ -468,19 +454,22 @@ export default class Inibase {
|
|
|
468
454
|
else if (field.children === "table" ||
|
|
469
455
|
(Array.isArray(field.type) && field.type.includes("table")) ||
|
|
470
456
|
(Array.isArray(field.children) && field.children.includes("table"))) {
|
|
471
|
-
if (
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
.
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
await
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
457
|
+
if ((await File.isExists(join(this.folder, this.database, field.key))) &&
|
|
458
|
+
(await File.isExists(join(tablePath, (prefix ?? "") + field.key + ".inib")))) {
|
|
459
|
+
if (options.columns)
|
|
460
|
+
options.columns = options.columns
|
|
461
|
+
.filter((column) => column.includes(`${field.key}.`))
|
|
462
|
+
.map((column) => column.replace(`${field.key}.`, ""));
|
|
463
|
+
const items = await File.get(join(tablePath, (prefix ?? "") + field.key + ".inib"), linesNumber, field.type, field.children, this.salt);
|
|
464
|
+
if (items)
|
|
465
|
+
await Promise.allSettled(Object.entries(items).map(async ([index, item]) => {
|
|
466
|
+
if (!RETURN[index])
|
|
467
|
+
RETURN[index] = {};
|
|
468
|
+
RETURN[index][field.key] = item
|
|
469
|
+
? await this.get(field.key, item, options)
|
|
470
|
+
: this.getDefaultValue(field);
|
|
471
|
+
}));
|
|
472
|
+
}
|
|
484
473
|
}
|
|
485
474
|
else if (await File.isExists(join(tablePath, (prefix ?? "") + field.key + ".inib"))) {
|
|
486
475
|
const items = await File.get(join(tablePath, (prefix ?? "") + field.key + ".inib"), linesNumber, field.type, field?.children, this.salt);
|
|
@@ -516,13 +505,13 @@ export default class Inibase {
|
|
|
516
505
|
.map((column) => column.replace(`${field.key}.`, ""));
|
|
517
506
|
const items = await File.get(join(tablePath, (prefix ?? "") + field.key + ".inib"), linesNumber, "number", undefined, this.salt);
|
|
518
507
|
if (items)
|
|
519
|
-
|
|
508
|
+
await Promise.allSettled(Object.entries(items).map(async ([index, item]) => {
|
|
520
509
|
if (!RETURN[index])
|
|
521
510
|
RETURN[index] = {};
|
|
522
511
|
RETURN[index][field.key] = item
|
|
523
512
|
? await this.get(field.key, item, options)
|
|
524
513
|
: this.getDefaultValue(field);
|
|
525
|
-
}
|
|
514
|
+
}));
|
|
526
515
|
}
|
|
527
516
|
}
|
|
528
517
|
else if (await File.isExists(join(tablePath, (prefix ?? "") + field.key + ".inib"))) {
|
|
@@ -570,7 +559,7 @@ export default class Inibase {
|
|
|
570
559
|
allTrue = true;
|
|
571
560
|
let index = -1;
|
|
572
561
|
for await (const [key, value] of Object.entries(criteria)) {
|
|
573
|
-
const field =
|
|
562
|
+
const field = Utils.getField(key, schema);
|
|
574
563
|
index++;
|
|
575
564
|
let searchOperator = undefined, searchComparedAtValue = undefined, searchLogicalOperator = undefined;
|
|
576
565
|
if (Utils.isObject(value)) {
|
|
@@ -693,9 +682,13 @@ export default class Inibase {
|
|
|
693
682
|
options.page = options.page || 1;
|
|
694
683
|
options.perPage = options.perPage || 15;
|
|
695
684
|
let RETURN;
|
|
696
|
-
let schema =
|
|
685
|
+
let schema = await this.getSchemaWhenTableNotEmpty(tableName, tableSchema);
|
|
697
686
|
if (options.columns && options.columns.length)
|
|
698
687
|
schema = this._filterSchemaByColumns(schema, options.columns);
|
|
688
|
+
if (where &&
|
|
689
|
+
((Array.isArray(where) && !where.length) ||
|
|
690
|
+
(Utils.isObject(where) && !Object.keys(where).length)))
|
|
691
|
+
where = undefined;
|
|
699
692
|
if (!where) {
|
|
700
693
|
// Display all data
|
|
701
694
|
RETURN = Object.values(await this.getItemsFromSchema(tableName, schema, Array.from({ length: options.perPage }, (_, index) => (options.page - 1) * options.perPage +
|
|
@@ -841,9 +834,10 @@ export default class Inibase {
|
|
|
841
834
|
: await File.append(path, content))));
|
|
842
835
|
await Promise.all(renameList.map(async ([tempPath, filePath]) => rename(tempPath, filePath)));
|
|
843
836
|
renameList = [];
|
|
837
|
+
totalItems += Array.isArray(RETURN) ? RETURN.length : 1;
|
|
844
838
|
if (Config.isCacheEnabled) {
|
|
845
839
|
await this.clearCache(tablePath);
|
|
846
|
-
await File.write(join(tablePath, ".cache", "pagination.inib"), `${lastId},${totalItems
|
|
840
|
+
await File.write(join(tablePath, ".cache", "pagination.inib"), `${lastId},${totalItems}`, true);
|
|
847
841
|
}
|
|
848
842
|
if (returnPostedData)
|
|
849
843
|
return this.get(tableName, Config.isReverseEnabled
|
|
@@ -864,19 +858,19 @@ export default class Inibase {
|
|
|
864
858
|
async put(tableName, data, where, options = {
|
|
865
859
|
page: 1,
|
|
866
860
|
perPage: 15,
|
|
867
|
-
},
|
|
861
|
+
}, returnUpdatedData) {
|
|
868
862
|
let renameList = [];
|
|
869
|
-
const tablePath = join(this.folder, this.database, tableName), schema = await this.
|
|
863
|
+
const tablePath = join(this.folder, this.database, tableName), schema = await this.getSchemaWhenTableNotEmpty(tableName);
|
|
870
864
|
data = this.formatData(data, schema, true);
|
|
871
865
|
if (!where) {
|
|
872
866
|
if (Utils.isArrayOfObjects(data)) {
|
|
873
|
-
if (!data.every((item) =>
|
|
867
|
+
if (!data.every((item) => Object.hasOwn(item, "id") && Utils.isValidID(item.id)))
|
|
874
868
|
throw this.throwError("INVALID_ID");
|
|
875
869
|
return this.put(tableName, data, data
|
|
876
870
|
.filter(({ id }) => id !== undefined)
|
|
877
871
|
.map(({ id }) => id));
|
|
878
872
|
}
|
|
879
|
-
else if (
|
|
873
|
+
else if (Object.hasOwn(data, "id")) {
|
|
880
874
|
if (!Utils.isValidID(data.id))
|
|
881
875
|
throw this.throwError("INVALID_ID", data.id);
|
|
882
876
|
return this.put(tableName, data, data.id);
|
|
@@ -909,8 +903,8 @@ export default class Inibase {
|
|
|
909
903
|
await Promise.all(renameList.map(async ([tempPath, filePath]) => rename(tempPath, filePath)));
|
|
910
904
|
if (Config.isCacheEnabled)
|
|
911
905
|
await this.clearCache(join(tablePath, ".cache"));
|
|
912
|
-
if (
|
|
913
|
-
return this.get(tableName, where, options, undefined, undefined, schema);
|
|
906
|
+
if (returnUpdatedData)
|
|
907
|
+
return await this.get(tableName, where, options, undefined, undefined, schema);
|
|
914
908
|
}
|
|
915
909
|
finally {
|
|
916
910
|
if (renameList.length)
|
|
@@ -951,7 +945,7 @@ export default class Inibase {
|
|
|
951
945
|
renameList = [];
|
|
952
946
|
if (Config.isCacheEnabled)
|
|
953
947
|
await this.clearCache(tablePath);
|
|
954
|
-
if (
|
|
948
|
+
if (returnUpdatedData)
|
|
955
949
|
return this.get(tableName, where, options, !Array.isArray(where), undefined, schema);
|
|
956
950
|
}
|
|
957
951
|
finally {
|
|
@@ -962,14 +956,17 @@ export default class Inibase {
|
|
|
962
956
|
}
|
|
963
957
|
else if (Utils.isObject(where)) {
|
|
964
958
|
const lineNumbers = await this.get(tableName, where, undefined, undefined, true, schema);
|
|
965
|
-
|
|
959
|
+
if (returnUpdatedData)
|
|
960
|
+
return this.put(tableName, data, lineNumbers, options, returnUpdatedData);
|
|
961
|
+
else
|
|
962
|
+
await this.put(tableName, data, lineNumbers, options, returnUpdatedData);
|
|
966
963
|
}
|
|
967
964
|
else
|
|
968
965
|
throw this.throwError("INVALID_PARAMETERS");
|
|
969
966
|
}
|
|
970
967
|
async delete(tableName, where, _id) {
|
|
971
968
|
let renameList = [];
|
|
972
|
-
const tablePath = join(this.folder, this.database, tableName), schema = await this.
|
|
969
|
+
const tablePath = join(this.folder, this.database, tableName), schema = await this.getSchemaWhenTableNotEmpty(tableName);
|
|
973
970
|
if (!where) {
|
|
974
971
|
try {
|
|
975
972
|
await File.lock(join(tablePath, ".tmp"));
|
|
@@ -1035,7 +1032,7 @@ export default class Inibase {
|
|
|
1035
1032
|
}
|
|
1036
1033
|
async sum(tableName, columns, where) {
|
|
1037
1034
|
let RETURN = {};
|
|
1038
|
-
const tablePath = join(this.folder, this.database, tableName), schema = await this.
|
|
1035
|
+
const tablePath = join(this.folder, this.database, tableName), schema = await this.getSchemaWhenTableNotEmpty(tableName);
|
|
1039
1036
|
if (!Array.isArray(columns))
|
|
1040
1037
|
columns = [columns];
|
|
1041
1038
|
for await (const column of columns) {
|
|
@@ -1055,7 +1052,7 @@ export default class Inibase {
|
|
|
1055
1052
|
}
|
|
1056
1053
|
async max(tableName, columns, where) {
|
|
1057
1054
|
let RETURN = {};
|
|
1058
|
-
const tablePath = join(this.folder, this.database, tableName), schema = await this.
|
|
1055
|
+
const tablePath = join(this.folder, this.database, tableName), schema = await this.getSchemaWhenTableNotEmpty(tableName);
|
|
1059
1056
|
if (!Array.isArray(columns))
|
|
1060
1057
|
columns = [columns];
|
|
1061
1058
|
for await (const column of columns) {
|
|
@@ -1075,7 +1072,7 @@ export default class Inibase {
|
|
|
1075
1072
|
}
|
|
1076
1073
|
async min(tableName, columns, where) {
|
|
1077
1074
|
let RETURN = {};
|
|
1078
|
-
const tablePath = join(this.folder, this.database, tableName), schema = await this.
|
|
1075
|
+
const tablePath = join(this.folder, this.database, tableName), schema = await this.getSchemaWhenTableNotEmpty(tableName);
|
|
1079
1076
|
if (!Array.isArray(columns))
|
|
1080
1077
|
columns = [columns];
|
|
1081
1078
|
for await (const column of columns) {
|
|
@@ -1098,7 +1095,7 @@ export default class Inibase {
|
|
|
1098
1095
|
perPage: 15,
|
|
1099
1096
|
}) {
|
|
1100
1097
|
// TO-DO: Cache Results based on "Columns and Sort Direction"
|
|
1101
|
-
const tablePath = join(this.folder, this.database, tableName), schema = await this.
|
|
1098
|
+
const tablePath = join(this.folder, this.database, tableName), schema = await this.getSchemaWhenTableNotEmpty(tableName);
|
|
1102
1099
|
// Default values for page and perPage
|
|
1103
1100
|
options.page = options.page || 1;
|
|
1104
1101
|
options.perPage = options.perPage || 15;
|
|
@@ -1138,7 +1135,7 @@ export default class Inibase {
|
|
|
1138
1135
|
let index = 2;
|
|
1139
1136
|
const sortColumns = sortArray
|
|
1140
1137
|
.map(([key, ascending], i) => {
|
|
1141
|
-
const field =
|
|
1138
|
+
const field = Utils.getField(key, schema);
|
|
1142
1139
|
if (field)
|
|
1143
1140
|
return `-k${i + index},${i + index}${field.type === "number" ? "n" : ""}${!ascending ? "r" : ""}`;
|
|
1144
1141
|
else
|
|
@@ -1171,7 +1168,7 @@ export default class Inibase {
|
|
|
1171
1168
|
const outputObject = {};
|
|
1172
1169
|
// Extract values for each file, including "id.inib"
|
|
1173
1170
|
filesPathes.forEach((fileName, index) => {
|
|
1174
|
-
const Field =
|
|
1171
|
+
const Field = Utils.getField(parse(fileName).name, schema);
|
|
1175
1172
|
if (Field)
|
|
1176
1173
|
outputObject[Field.key] = File.decode(splitedFileColumns[index], Field?.type, Field?.children, this.salt);
|
|
1177
1174
|
});
|
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
|
}
|