inibase 1.0.0-rc.4 → 1.0.0-rc.42
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/README.md +161 -91
- package/dist/config.d.ts +5 -0
- package/dist/config.js +5 -0
- package/dist/file.d.ts +176 -0
- package/dist/file.js +625 -0
- package/dist/file.thread.d.ts +1 -0
- package/dist/file.thread.js +5 -0
- package/dist/index.d.ts +111 -0
- package/dist/index.js +1178 -0
- package/dist/index.thread.d.ts +1 -0
- package/dist/index.thread.js +6 -0
- package/dist/utils.d.ts +206 -0
- package/dist/utils.js +418 -0
- package/dist/utils.server.d.ts +107 -0
- package/dist/utils.server.js +254 -0
- package/package.json +75 -18
- package/file.ts +0 -327
- package/index.ts +0 -1280
- package/tsconfig.json +0 -6
- package/utils.ts +0 -110
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import Inibase from "./index.js";
|
|
2
|
+
import { parentPort, workerData } from "node:worker_threads";
|
|
3
|
+
const { _constructor, functionName, arg } = workerData;
|
|
4
|
+
// @ts-ignore
|
|
5
|
+
new Inibase(..._constructor)[functionName](...arg)
|
|
6
|
+
.then((res) => parentPort?.postMessage(res));
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import type { FieldType, ComparisonOperator } from "./index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Type guard function to check if the input is an array of objects.
|
|
4
|
+
*
|
|
5
|
+
* @param input - The value to be checked.
|
|
6
|
+
* @returns boolean - True if the input is an array of objects, false otherwise.
|
|
7
|
+
*
|
|
8
|
+
* Note: Considers empty arrays and arrays where every element is an object.
|
|
9
|
+
*/
|
|
10
|
+
export declare const isArrayOfObjects: (input: any) => input is Record<any, any>[];
|
|
11
|
+
/**
|
|
12
|
+
* Type guard function to check if the input is an array of arrays.
|
|
13
|
+
*
|
|
14
|
+
* @param input - The value to be checked.
|
|
15
|
+
* @returns boolean - True if the input is an array of arrays, false otherwise.
|
|
16
|
+
*
|
|
17
|
+
* Note: Considers empty arrays and arrays where every element is also an array.
|
|
18
|
+
*/
|
|
19
|
+
export declare const isArrayOfArrays: (input: any) => input is any[][];
|
|
20
|
+
/**
|
|
21
|
+
* Type guard function to check if the input is an array of nulls or an array of arrays of nulls.
|
|
22
|
+
*
|
|
23
|
+
* @param input - The value to be checked.
|
|
24
|
+
* @returns boolean - True if the input is an array consisting entirely of nulls or arrays of nulls, false otherwise.
|
|
25
|
+
*
|
|
26
|
+
* Note: Recursively checks each element, allowing for nested arrays of nulls.
|
|
27
|
+
*/
|
|
28
|
+
export declare const isArrayOfNulls: (input: any) => input is null[] | null[][];
|
|
29
|
+
/**
|
|
30
|
+
* Type guard function to check if the input is an object.
|
|
31
|
+
*
|
|
32
|
+
* @param obj - The value to be checked.
|
|
33
|
+
* @returns boolean - True if the input is an object (excluding arrays), false otherwise.
|
|
34
|
+
*
|
|
35
|
+
* Note: Checks if the input is non-null and either has 'Object' as its constructor name or is of type 'object' without being an array.
|
|
36
|
+
*/
|
|
37
|
+
export declare const isObject: (obj: any) => obj is Record<any, any>;
|
|
38
|
+
/**
|
|
39
|
+
* Recursively merges properties from a source object into a target object. If a property exists in both, the source's value overwrites the target's.
|
|
40
|
+
*
|
|
41
|
+
* @param target - The target object to merge properties into.
|
|
42
|
+
* @param source - The source object from which properties are merged.
|
|
43
|
+
* @returns any - The modified target object with merged properties.
|
|
44
|
+
*
|
|
45
|
+
* Note: Performs a deep merge for nested objects. Non-object properties are directly overwritten.
|
|
46
|
+
*/
|
|
47
|
+
export declare const deepMerge: (target: any, source: any) => any;
|
|
48
|
+
/**
|
|
49
|
+
* Combines an array of objects into a single object. If the same key exists in multiple objects, the values are merged.
|
|
50
|
+
*
|
|
51
|
+
* @param arr - Array of objects to be combined.
|
|
52
|
+
* @returns Record<string, any> - A single object with combined keys and values.
|
|
53
|
+
*
|
|
54
|
+
* Note: Handles nested objects by recursively combining them. Non-object values with the same key are merged into arrays.
|
|
55
|
+
*/
|
|
56
|
+
export declare const combineObjects: (arr: Record<string, any>[]) => Record<string, any>;
|
|
57
|
+
/**
|
|
58
|
+
* Type guard function to check if the input is a number.
|
|
59
|
+
*
|
|
60
|
+
* @param input - The value to be checked.
|
|
61
|
+
* @returns boolean - True if the input is a number, false otherwise.
|
|
62
|
+
*
|
|
63
|
+
* Note: Validates that the input can be parsed as a float and that subtracting zero results in a number, ensuring it's a numeric value.
|
|
64
|
+
*/
|
|
65
|
+
export declare const isNumber: (input: any) => input is number;
|
|
66
|
+
/**
|
|
67
|
+
* Checks if the input is a valid email format.
|
|
68
|
+
*
|
|
69
|
+
* @param input - The value to be checked.
|
|
70
|
+
* @returns boolean - True if the input matches the email format, false otherwise.
|
|
71
|
+
*
|
|
72
|
+
* Note: Uses a regular expression to validate the email format, ensuring it has parts separated by '@' and contains a domain with a period.
|
|
73
|
+
*/
|
|
74
|
+
export declare const isEmail: (input: any) => boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Checks if the input is a valid URL format.
|
|
77
|
+
*
|
|
78
|
+
* @param input - The value to be checked.
|
|
79
|
+
* @returns boolean - True if the input matches the URL format, false otherwise.
|
|
80
|
+
*
|
|
81
|
+
* Note: Validates URLs including protocols (http/https), domain names, IP addresses, ports, paths, query strings, and fragments.
|
|
82
|
+
* Also recognizes 'tel:' and 'mailto:' as valid URL formats, as well as strings starting with '#' without spaces.
|
|
83
|
+
*/
|
|
84
|
+
export declare const isURL: (input: any) => boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Checks if the input contains HTML tags or entities.
|
|
87
|
+
*
|
|
88
|
+
* @param input - The value to be checked.
|
|
89
|
+
* @returns boolean - True if the input contains HTML tags or entities, false otherwise.
|
|
90
|
+
*
|
|
91
|
+
* Note: Uses a regular expression to detect HTML tags (like <tag>) and entities (like &entity;).
|
|
92
|
+
* Recognizes both opening and closing tags, as well as self-closing tags.
|
|
93
|
+
*/
|
|
94
|
+
export declare const isHTML: (input: any) => boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Type guard function to check if the input is a string, excluding strings that match specific formats (number, boolean, email, URL, IP).
|
|
97
|
+
*
|
|
98
|
+
* @param input - The value to be checked.
|
|
99
|
+
* @returns boolean - True if the input is a string that doesn't match the specific formats, false otherwise.
|
|
100
|
+
*
|
|
101
|
+
* Note: Validates the input against being a number, boolean, email, URL, or IP address to ensure it's a general string.
|
|
102
|
+
*/
|
|
103
|
+
export declare const isString: (input: any) => input is string;
|
|
104
|
+
/**
|
|
105
|
+
* Checks if the input is a valid IP address format.
|
|
106
|
+
*
|
|
107
|
+
* @param input - The value to be checked.
|
|
108
|
+
* @returns boolean - True if the input matches the IP address format, false otherwise.
|
|
109
|
+
*
|
|
110
|
+
* Note: Uses a regular expression to validate IP addresses, ensuring they consist of four octets, each ranging from 0 to 255.
|
|
111
|
+
*/
|
|
112
|
+
export declare const isIP: (input: any) => boolean;
|
|
113
|
+
/**
|
|
114
|
+
* Type guard function to check if the input is a boolean or a string representation of a boolean.
|
|
115
|
+
*
|
|
116
|
+
* @param input - The value to be checked.
|
|
117
|
+
* @returns boolean - True if the input is a boolean value or 'true'/'false' strings, false otherwise.
|
|
118
|
+
*
|
|
119
|
+
* Note: Recognizes both boolean literals (true, false) and their string representations ("true", "false").
|
|
120
|
+
*/
|
|
121
|
+
export declare const isBoolean: (input: any) => input is boolean;
|
|
122
|
+
/**
|
|
123
|
+
* Type guard function to check if the input is a password based on a specific length criterion.
|
|
124
|
+
*
|
|
125
|
+
* @param input - The value to be checked.
|
|
126
|
+
* @returns boolean - True if the input is a string with a length of 161 characters, false otherwise.
|
|
127
|
+
*
|
|
128
|
+
* Note: Specifically checks for string length to determine if it matches the defined password length criterion.
|
|
129
|
+
*/
|
|
130
|
+
export declare const isPassword: (input: any) => input is string;
|
|
131
|
+
/**
|
|
132
|
+
* Checks if the input can be converted to a valid date.
|
|
133
|
+
*
|
|
134
|
+
* @param input - The input to be checked, can be of any type.
|
|
135
|
+
* @returns A boolean indicating whether the input is a valid date.
|
|
136
|
+
*/
|
|
137
|
+
export declare const isDate: (input: any) => boolean;
|
|
138
|
+
/**
|
|
139
|
+
* Checks if the input is a valid ID.
|
|
140
|
+
*
|
|
141
|
+
* @param input - The input to be checked, can be of any type.
|
|
142
|
+
* @returns A boolean indicating whether the input is a string representing a valid ID of length 32.
|
|
143
|
+
*/
|
|
144
|
+
export declare const isValidID: (input: any) => input is string;
|
|
145
|
+
/**
|
|
146
|
+
* Checks if a given string is a valid JSON.
|
|
147
|
+
*
|
|
148
|
+
* @param {string} str - The string to be checked.
|
|
149
|
+
* @returns {boolean} Returns true if the string is valid JSON, otherwise false.
|
|
150
|
+
*/
|
|
151
|
+
export declare const isJSON: (str: string) => boolean;
|
|
152
|
+
/**
|
|
153
|
+
* Identifies and returns properties that have changed between two objects.
|
|
154
|
+
*
|
|
155
|
+
* @param obj1 - The first object for comparison, with string keys and values.
|
|
156
|
+
* @param obj2 - The second object for comparison, with string keys and values.
|
|
157
|
+
* @returns A record of changed properties with original values from obj1 and new values from obj2, or null if no changes are found.
|
|
158
|
+
*/
|
|
159
|
+
export declare const findChangedProperties: (obj1: Record<string, string>, obj2: Record<string, string>) => Record<string, string> | null;
|
|
160
|
+
/**
|
|
161
|
+
* Detects the field type of an input based on available types.
|
|
162
|
+
*
|
|
163
|
+
* @param input - The input whose field type is to be detected.
|
|
164
|
+
* @param availableTypes - An array of potential field types to consider.
|
|
165
|
+
* @returns The detected field type as a string, or undefined if no matching type is found.
|
|
166
|
+
*/
|
|
167
|
+
export declare const detectFieldType: (input: any, availableTypes: FieldType[]) => FieldType | undefined;
|
|
168
|
+
/**
|
|
169
|
+
* Validates if the given value matches the specified field type(s).
|
|
170
|
+
*
|
|
171
|
+
* @param value - The value to be validated.
|
|
172
|
+
* @param fieldType - The expected field type or an array of possible field types.
|
|
173
|
+
* @param fieldChildrenType - Optional; the expected type(s) of children elements, used if the field type is an array.
|
|
174
|
+
* @returns A boolean indicating whether the value matches the specified field type(s).
|
|
175
|
+
*/
|
|
176
|
+
export declare const validateFieldType: (value: any, fieldType: FieldType | FieldType[], fieldChildrenType?: FieldType | FieldType[]) => boolean;
|
|
177
|
+
export declare function FormatObjectCriteriaValue(value: string, isParentArray?: boolean): [
|
|
178
|
+
ComparisonOperator,
|
|
179
|
+
string | number | boolean | null | (string | number | null)[]
|
|
180
|
+
];
|
|
181
|
+
type ValidKey = number | string;
|
|
182
|
+
export declare const swapKeyValue: <K extends ValidKey, V extends ValidKey>(object: Record<K, V>) => Record<V, K>;
|
|
183
|
+
export default class Utils {
|
|
184
|
+
static isNumber: (input: any) => input is number;
|
|
185
|
+
static isObject: (obj: any) => obj is Record<any, any>;
|
|
186
|
+
static isEmail: (input: any) => boolean;
|
|
187
|
+
static isDate: (input: any) => boolean;
|
|
188
|
+
static isURL: (input: any) => boolean;
|
|
189
|
+
static isValidID: (input: any) => input is string;
|
|
190
|
+
static isPassword: (input: any) => input is string;
|
|
191
|
+
static deepMerge: (target: any, source: any) => any;
|
|
192
|
+
static combineObjects: (arr: Record<string, any>[]) => Record<string, any>;
|
|
193
|
+
static isArrayOfObjects: (input: any) => input is Record<any, any>[];
|
|
194
|
+
static findChangedProperties: (obj1: Record<string, string>, obj2: Record<string, string>) => Record<string, string> | null;
|
|
195
|
+
static detectFieldType: (input: any, availableTypes: FieldType[]) => FieldType | undefined;
|
|
196
|
+
static isArrayOfArrays: (input: any) => input is any[][];
|
|
197
|
+
static isBoolean: (input: any) => input is boolean;
|
|
198
|
+
static isString: (input: any) => input is string;
|
|
199
|
+
static isHTML: (input: any) => boolean;
|
|
200
|
+
static isIP: (input: any) => boolean;
|
|
201
|
+
static validateFieldType: (value: any, fieldType: FieldType | FieldType[], fieldChildrenType?: FieldType | FieldType[] | undefined) => boolean;
|
|
202
|
+
static isArrayOfNulls: (input: any) => input is null[] | null[][];
|
|
203
|
+
static FormatObjectCriteriaValue: typeof FormatObjectCriteriaValue;
|
|
204
|
+
static swapKeyValue: <K extends ValidKey, V extends ValidKey>(object: Record<K, V>) => Record<V, K>;
|
|
205
|
+
}
|
|
206
|
+
export {};
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guard function to check if the input is an array of objects.
|
|
3
|
+
*
|
|
4
|
+
* @param input - The value to be checked.
|
|
5
|
+
* @returns boolean - True if the input is an array of objects, false otherwise.
|
|
6
|
+
*
|
|
7
|
+
* Note: Considers empty arrays and arrays where every element is an object.
|
|
8
|
+
*/
|
|
9
|
+
export const isArrayOfObjects = (input) => Array.isArray(input) && (input.length === 0 || input.every(isObject));
|
|
10
|
+
/**
|
|
11
|
+
* Type guard function to check if the input is an array of arrays.
|
|
12
|
+
*
|
|
13
|
+
* @param input - The value to be checked.
|
|
14
|
+
* @returns boolean - True if the input is an array of arrays, false otherwise.
|
|
15
|
+
*
|
|
16
|
+
* Note: Considers empty arrays and arrays where every element is also an array.
|
|
17
|
+
*/
|
|
18
|
+
export const isArrayOfArrays = (input) => Array.isArray(input) && (input.length === 0 || input.every(Array.isArray));
|
|
19
|
+
/**
|
|
20
|
+
* Type guard function to check if the input is an array of nulls or an array of arrays of nulls.
|
|
21
|
+
*
|
|
22
|
+
* @param input - The value to be checked.
|
|
23
|
+
* @returns boolean - True if the input is an array consisting entirely of nulls or arrays of nulls, false otherwise.
|
|
24
|
+
*
|
|
25
|
+
* Note: Recursively checks each element, allowing for nested arrays of nulls.
|
|
26
|
+
*/
|
|
27
|
+
export const isArrayOfNulls = (input) => input.every((_input) => Array.isArray(_input) ? isArrayOfNulls(_input) : _input === null);
|
|
28
|
+
/**
|
|
29
|
+
* Type guard function to check if the input is an object.
|
|
30
|
+
*
|
|
31
|
+
* @param obj - The value to be checked.
|
|
32
|
+
* @returns boolean - True if the input is an object (excluding arrays), false otherwise.
|
|
33
|
+
*
|
|
34
|
+
* Note: Checks if the input is non-null and either has 'Object' as its constructor name or is of type 'object' without being an array.
|
|
35
|
+
*/
|
|
36
|
+
export const isObject = (obj) => obj != null &&
|
|
37
|
+
(obj.constructor.name === "Object" ||
|
|
38
|
+
(typeof obj === "object" && !Array.isArray(obj)));
|
|
39
|
+
/**
|
|
40
|
+
* Recursively merges properties from a source object into a target object. If a property exists in both, the source's value overwrites the target's.
|
|
41
|
+
*
|
|
42
|
+
* @param target - The target object to merge properties into.
|
|
43
|
+
* @param source - The source object from which properties are merged.
|
|
44
|
+
* @returns any - The modified target object with merged properties.
|
|
45
|
+
*
|
|
46
|
+
* Note: Performs a deep merge for nested objects. Non-object properties are directly overwritten.
|
|
47
|
+
*/
|
|
48
|
+
export const deepMerge = (target, source) => {
|
|
49
|
+
for (const key in source) {
|
|
50
|
+
if (source.hasOwnProperty(key)) {
|
|
51
|
+
if (isObject(source[key]) && isObject(target[key]))
|
|
52
|
+
target[key] = deepMerge(target[key], source[key]);
|
|
53
|
+
else
|
|
54
|
+
target[key] = source[key];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return target;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Combines an array of objects into a single object. If the same key exists in multiple objects, the values are merged.
|
|
61
|
+
*
|
|
62
|
+
* @param arr - Array of objects to be combined.
|
|
63
|
+
* @returns Record<string, any> - A single object with combined keys and values.
|
|
64
|
+
*
|
|
65
|
+
* Note: Handles nested objects by recursively combining them. Non-object values with the same key are merged into arrays.
|
|
66
|
+
*/
|
|
67
|
+
export const combineObjects = (arr) => {
|
|
68
|
+
const result = {};
|
|
69
|
+
for (const obj of arr) {
|
|
70
|
+
for (const key in obj) {
|
|
71
|
+
if (obj.hasOwnProperty(key)) {
|
|
72
|
+
const existingValue = result[key];
|
|
73
|
+
const newValue = obj[key];
|
|
74
|
+
if (isObject(existingValue) &&
|
|
75
|
+
isObject(newValue) &&
|
|
76
|
+
existingValue !== null &&
|
|
77
|
+
existingValue !== undefined &&
|
|
78
|
+
newValue !== null &&
|
|
79
|
+
newValue !== undefined) {
|
|
80
|
+
// If both values are objects, recursively combine them
|
|
81
|
+
result[key] = combineObjects([existingValue, newValue]);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
// If one or both values are not objects, overwrite the existing value
|
|
85
|
+
result[key] =
|
|
86
|
+
existingValue !== null && existingValue !== undefined
|
|
87
|
+
? Array.isArray(existingValue)
|
|
88
|
+
? Array.isArray(newValue)
|
|
89
|
+
? [...existingValue, ...newValue]
|
|
90
|
+
: [...existingValue, newValue]
|
|
91
|
+
: Array.isArray(newValue)
|
|
92
|
+
? [existingValue, ...newValue]
|
|
93
|
+
: [existingValue, newValue]
|
|
94
|
+
: newValue;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return result;
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Type guard function to check if the input is a number.
|
|
103
|
+
*
|
|
104
|
+
* @param input - The value to be checked.
|
|
105
|
+
* @returns boolean - True if the input is a number, false otherwise.
|
|
106
|
+
*
|
|
107
|
+
* Note: Validates that the input can be parsed as a float and that subtracting zero results in a number, ensuring it's a numeric value.
|
|
108
|
+
*/
|
|
109
|
+
export const isNumber = (input) => !isNaN(parseFloat(input)) && !isNaN(input - 0);
|
|
110
|
+
/**
|
|
111
|
+
* Checks if the input is a valid email format.
|
|
112
|
+
*
|
|
113
|
+
* @param input - The value to be checked.
|
|
114
|
+
* @returns boolean - True if the input matches the email format, false otherwise.
|
|
115
|
+
*
|
|
116
|
+
* Note: Uses a regular expression to validate the email format, ensuring it has parts separated by '@' and contains a domain with a period.
|
|
117
|
+
*/
|
|
118
|
+
export const isEmail = (input) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(String(input));
|
|
119
|
+
/**
|
|
120
|
+
* Checks if the input is a valid URL format.
|
|
121
|
+
*
|
|
122
|
+
* @param input - The value to be checked.
|
|
123
|
+
* @returns boolean - True if the input matches the URL format, false otherwise.
|
|
124
|
+
*
|
|
125
|
+
* Note: Validates URLs including protocols (http/https), domain names, IP addresses, ports, paths, query strings, and fragments.
|
|
126
|
+
* Also recognizes 'tel:' and 'mailto:' as valid URL formats, as well as strings starting with '#' without spaces.
|
|
127
|
+
*/
|
|
128
|
+
export const isURL = (input) => {
|
|
129
|
+
if (typeof input !== "string")
|
|
130
|
+
return false;
|
|
131
|
+
if ((input[0] === "#" && !input.includes(" ")) ||
|
|
132
|
+
input.startsWith("tel:") ||
|
|
133
|
+
input.startsWith("mailto:"))
|
|
134
|
+
return true;
|
|
135
|
+
else {
|
|
136
|
+
var pattern = new RegExp("^(https?:\\/\\/)?" + // protocol
|
|
137
|
+
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // domain name
|
|
138
|
+
"localhost|" + // OR localhost
|
|
139
|
+
"((\\d{1,3}\\.){3}\\d{1,3}))" + // OR ip (v4) address
|
|
140
|
+
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // port and path
|
|
141
|
+
"(\\?[;&a-z\\d%_.~+=-]*)?" + // query string
|
|
142
|
+
"(\\#[-a-z\\d_]*)?$", "i"); // fragment locator
|
|
143
|
+
return !!pattern.test(input);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* Checks if the input contains HTML tags or entities.
|
|
148
|
+
*
|
|
149
|
+
* @param input - The value to be checked.
|
|
150
|
+
* @returns boolean - True if the input contains HTML tags or entities, false otherwise.
|
|
151
|
+
*
|
|
152
|
+
* Note: Uses a regular expression to detect HTML tags (like <tag>) and entities (like &entity;).
|
|
153
|
+
* Recognizes both opening and closing tags, as well as self-closing tags.
|
|
154
|
+
*/
|
|
155
|
+
export const isHTML = (input) => /<\/?\s*[a-z-][^>]*\s*>|(\&(?:[\w\d]+|#\d+|#x[a-f\d]+);)/g.test(input);
|
|
156
|
+
/**
|
|
157
|
+
* Type guard function to check if the input is a string, excluding strings that match specific formats (number, boolean, email, URL, IP).
|
|
158
|
+
*
|
|
159
|
+
* @param input - The value to be checked.
|
|
160
|
+
* @returns boolean - True if the input is a string that doesn't match the specific formats, false otherwise.
|
|
161
|
+
*
|
|
162
|
+
* Note: Validates the input against being a number, boolean, email, URL, or IP address to ensure it's a general string.
|
|
163
|
+
*/
|
|
164
|
+
export const isString = (input) => Object.prototype.toString.call(input) === "[object String]" &&
|
|
165
|
+
[isNumber, isBoolean, isEmail, isURL, isIP].every((fn) => !fn(input));
|
|
166
|
+
/**
|
|
167
|
+
* Checks if the input is a valid IP address format.
|
|
168
|
+
*
|
|
169
|
+
* @param input - The value to be checked.
|
|
170
|
+
* @returns boolean - True if the input matches the IP address format, false otherwise.
|
|
171
|
+
*
|
|
172
|
+
* Note: Uses a regular expression to validate IP addresses, ensuring they consist of four octets, each ranging from 0 to 255.
|
|
173
|
+
*/
|
|
174
|
+
export const isIP = (input) => /^(?:(?:^|\.)(?:2(?:5[0-5]|[0-4]\d)|1?\d?\d)){4}$/.test(input);
|
|
175
|
+
/**
|
|
176
|
+
* Type guard function to check if the input is a boolean or a string representation of a boolean.
|
|
177
|
+
*
|
|
178
|
+
* @param input - The value to be checked.
|
|
179
|
+
* @returns boolean - True if the input is a boolean value or 'true'/'false' strings, false otherwise.
|
|
180
|
+
*
|
|
181
|
+
* Note: Recognizes both boolean literals (true, false) and their string representations ("true", "false").
|
|
182
|
+
*/
|
|
183
|
+
export const isBoolean = (input) => typeof input === "boolean" ||
|
|
184
|
+
input === "true" ||
|
|
185
|
+
input === "false" ||
|
|
186
|
+
input === true ||
|
|
187
|
+
input === false;
|
|
188
|
+
/**
|
|
189
|
+
* Type guard function to check if the input is a password based on a specific length criterion.
|
|
190
|
+
*
|
|
191
|
+
* @param input - The value to be checked.
|
|
192
|
+
* @returns boolean - True if the input is a string with a length of 161 characters, false otherwise.
|
|
193
|
+
*
|
|
194
|
+
* Note: Specifically checks for string length to determine if it matches the defined password length criterion.
|
|
195
|
+
*/
|
|
196
|
+
export const isPassword = (input) => typeof input === "string" && input.length === 97;
|
|
197
|
+
/**
|
|
198
|
+
* Checks if the input can be converted to a valid date.
|
|
199
|
+
*
|
|
200
|
+
* @param input - The input to be checked, can be of any type.
|
|
201
|
+
* @returns A boolean indicating whether the input is a valid date.
|
|
202
|
+
*/
|
|
203
|
+
export const isDate = (input) => !isNaN(new Date(input).getTime()) || !isNaN(Date.parse(input));
|
|
204
|
+
/**
|
|
205
|
+
* Checks if the input is a valid ID.
|
|
206
|
+
*
|
|
207
|
+
* @param input - The input to be checked, can be of any type.
|
|
208
|
+
* @returns A boolean indicating whether the input is a string representing a valid ID of length 32.
|
|
209
|
+
*/
|
|
210
|
+
export const isValidID = (input) => {
|
|
211
|
+
return typeof input === "string" && input.length === 32;
|
|
212
|
+
};
|
|
213
|
+
/**
|
|
214
|
+
* Checks if a given string is a valid JSON.
|
|
215
|
+
*
|
|
216
|
+
* @param {string} str - The string to be checked.
|
|
217
|
+
* @returns {boolean} Returns true if the string is valid JSON, otherwise false.
|
|
218
|
+
*/
|
|
219
|
+
export const isJSON = (str) => str[0] === "{" || str[0] === "[";
|
|
220
|
+
/**
|
|
221
|
+
* Identifies and returns properties that have changed between two objects.
|
|
222
|
+
*
|
|
223
|
+
* @param obj1 - The first object for comparison, with string keys and values.
|
|
224
|
+
* @param obj2 - The second object for comparison, with string keys and values.
|
|
225
|
+
* @returns A record of changed properties with original values from obj1 and new values from obj2, or null if no changes are found.
|
|
226
|
+
*/
|
|
227
|
+
export const findChangedProperties = (obj1, obj2) => {
|
|
228
|
+
const result = {};
|
|
229
|
+
for (const key1 in obj1)
|
|
230
|
+
if (obj2.hasOwnProperty(key1) && obj1[key1] !== obj2[key1])
|
|
231
|
+
result[obj1[key1]] = obj2[key1];
|
|
232
|
+
return Object.keys(result).length ? result : null;
|
|
233
|
+
};
|
|
234
|
+
/**
|
|
235
|
+
* Detects the field type of an input based on available types.
|
|
236
|
+
*
|
|
237
|
+
* @param input - The input whose field type is to be detected.
|
|
238
|
+
* @param availableTypes - An array of potential field types to consider.
|
|
239
|
+
* @returns The detected field type as a string, or undefined if no matching type is found.
|
|
240
|
+
*/
|
|
241
|
+
export const detectFieldType = (input, availableTypes) => {
|
|
242
|
+
if (!Array.isArray(input)) {
|
|
243
|
+
if ((input === "0" ||
|
|
244
|
+
input === "1" ||
|
|
245
|
+
input === "true" ||
|
|
246
|
+
input === "false") &&
|
|
247
|
+
availableTypes.includes("boolean"))
|
|
248
|
+
return "boolean";
|
|
249
|
+
else if (isNumber(input)) {
|
|
250
|
+
if (availableTypes.includes("table"))
|
|
251
|
+
return "table";
|
|
252
|
+
else if (availableTypes.includes("date"))
|
|
253
|
+
return "date";
|
|
254
|
+
else if (availableTypes.includes("number"))
|
|
255
|
+
return "number";
|
|
256
|
+
}
|
|
257
|
+
else if (availableTypes.includes("table") && isValidID(input))
|
|
258
|
+
return "table";
|
|
259
|
+
else if (input.includes(",") && availableTypes.includes("array"))
|
|
260
|
+
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
|
+
return undefined;
|
|
279
|
+
};
|
|
280
|
+
/**
|
|
281
|
+
* Validates if the given value matches the specified field type(s).
|
|
282
|
+
*
|
|
283
|
+
* @param value - The value to be validated.
|
|
284
|
+
* @param fieldType - The expected field type or an array of possible field types.
|
|
285
|
+
* @param fieldChildrenType - Optional; the expected type(s) of children elements, used if the field type is an array.
|
|
286
|
+
* @returns A boolean indicating whether the value matches the specified field type(s).
|
|
287
|
+
*/
|
|
288
|
+
export const validateFieldType = (value, fieldType, fieldChildrenType) => {
|
|
289
|
+
if (value === null)
|
|
290
|
+
return true;
|
|
291
|
+
if (Array.isArray(fieldType))
|
|
292
|
+
return detectFieldType(value, fieldType) !== undefined;
|
|
293
|
+
if (fieldType === "array" && fieldChildrenType && Array.isArray(value))
|
|
294
|
+
return value.every((v) => detectFieldType(v, Array.isArray(fieldChildrenType)
|
|
295
|
+
? fieldChildrenType
|
|
296
|
+
: [fieldChildrenType]) !== undefined);
|
|
297
|
+
switch (fieldType) {
|
|
298
|
+
case "string":
|
|
299
|
+
return isString(value);
|
|
300
|
+
case "password":
|
|
301
|
+
return isNumber(value) || isPassword(value) || isString(value);
|
|
302
|
+
case "number":
|
|
303
|
+
return isNumber(value);
|
|
304
|
+
case "html":
|
|
305
|
+
return isHTML(value);
|
|
306
|
+
case "ip":
|
|
307
|
+
return isIP(value);
|
|
308
|
+
case "boolean":
|
|
309
|
+
return isBoolean(value);
|
|
310
|
+
case "date":
|
|
311
|
+
return isDate(value);
|
|
312
|
+
case "object":
|
|
313
|
+
return isObject(value);
|
|
314
|
+
case "array":
|
|
315
|
+
return Array.isArray(value);
|
|
316
|
+
case "email":
|
|
317
|
+
return isEmail(value);
|
|
318
|
+
case "url":
|
|
319
|
+
return isURL(value);
|
|
320
|
+
case "table":
|
|
321
|
+
// feat: check if id exists
|
|
322
|
+
if (Array.isArray(value))
|
|
323
|
+
return ((isArrayOfObjects(value) &&
|
|
324
|
+
value.every((element) => element.hasOwnProperty("id") &&
|
|
325
|
+
(isValidID(element.id) || isNumber(element.id)))) ||
|
|
326
|
+
value.every(isNumber) ||
|
|
327
|
+
isValidID(value));
|
|
328
|
+
else if (isObject(value))
|
|
329
|
+
return (value.hasOwnProperty("id") &&
|
|
330
|
+
(isValidID(value.id) || isNumber(value.id)));
|
|
331
|
+
else
|
|
332
|
+
return isNumber(value) || isValidID(value);
|
|
333
|
+
case "id":
|
|
334
|
+
return isNumber(value) || isValidID(value);
|
|
335
|
+
case "json":
|
|
336
|
+
return isJSON(value) || Array.isArray(value) || isObject(value);
|
|
337
|
+
default:
|
|
338
|
+
return false;
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
export function FormatObjectCriteriaValue(value, isParentArray = false) {
|
|
342
|
+
switch (value[0]) {
|
|
343
|
+
case ">":
|
|
344
|
+
case "<":
|
|
345
|
+
return value[1] === "="
|
|
346
|
+
? [
|
|
347
|
+
value.slice(0, 2),
|
|
348
|
+
value.slice(2),
|
|
349
|
+
]
|
|
350
|
+
: [
|
|
351
|
+
value.slice(0, 1),
|
|
352
|
+
value.slice(1),
|
|
353
|
+
];
|
|
354
|
+
case "[":
|
|
355
|
+
return value[1] === "]"
|
|
356
|
+
? [
|
|
357
|
+
value.slice(0, 2),
|
|
358
|
+
value.slice(2).toString().split(","),
|
|
359
|
+
]
|
|
360
|
+
: ["[]", value.slice(1)];
|
|
361
|
+
case "!":
|
|
362
|
+
return ["=", "*"].includes(value[1])
|
|
363
|
+
? [
|
|
364
|
+
value.slice(0, 2),
|
|
365
|
+
value.slice(2),
|
|
366
|
+
]
|
|
367
|
+
: value[1] === "["
|
|
368
|
+
? [
|
|
369
|
+
value.slice(0, 3),
|
|
370
|
+
value.slice(3),
|
|
371
|
+
]
|
|
372
|
+
: [
|
|
373
|
+
(value.slice(0, 1) + "="),
|
|
374
|
+
value.slice(1),
|
|
375
|
+
];
|
|
376
|
+
case "=":
|
|
377
|
+
return isParentArray
|
|
378
|
+
? [
|
|
379
|
+
value.slice(0, 1),
|
|
380
|
+
value.slice(1),
|
|
381
|
+
]
|
|
382
|
+
: [
|
|
383
|
+
value.slice(0, 1),
|
|
384
|
+
(value.slice(1) + ","),
|
|
385
|
+
];
|
|
386
|
+
case "*":
|
|
387
|
+
return [
|
|
388
|
+
value.slice(0, 1),
|
|
389
|
+
value.slice(1),
|
|
390
|
+
];
|
|
391
|
+
default:
|
|
392
|
+
return ["=", value];
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
export const swapKeyValue = (object) => Object.entries(object).reduce((swapped, [key, value]) => ({ ...swapped, [value]: key }), {});
|
|
396
|
+
export default class Utils {
|
|
397
|
+
static isNumber = isNumber;
|
|
398
|
+
static isObject = isObject;
|
|
399
|
+
static isEmail = isEmail;
|
|
400
|
+
static isDate = isDate;
|
|
401
|
+
static isURL = isURL;
|
|
402
|
+
static isValidID = isValidID;
|
|
403
|
+
static isPassword = isPassword;
|
|
404
|
+
static deepMerge = deepMerge;
|
|
405
|
+
static combineObjects = combineObjects;
|
|
406
|
+
static isArrayOfObjects = isArrayOfObjects;
|
|
407
|
+
static findChangedProperties = findChangedProperties;
|
|
408
|
+
static detectFieldType = detectFieldType;
|
|
409
|
+
static isArrayOfArrays = isArrayOfArrays;
|
|
410
|
+
static isBoolean = isBoolean;
|
|
411
|
+
static isString = isString;
|
|
412
|
+
static isHTML = isHTML;
|
|
413
|
+
static isIP = isIP;
|
|
414
|
+
static validateFieldType = validateFieldType;
|
|
415
|
+
static isArrayOfNulls = isArrayOfNulls;
|
|
416
|
+
static FormatObjectCriteriaValue = FormatObjectCriteriaValue;
|
|
417
|
+
static swapKeyValue = swapKeyValue;
|
|
418
|
+
}
|