Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ export function transferNestedDict(dictA: any, dictB: any, ignoreNoneExistingKey

for (let key in dictA) {
if (dictB[key] == null && ignoreNoneExistingKeys) continue;
if (typeof dictA[key] === 'object') {
if (Array.isArray(dictA[key])) {
dictB[key] = dictA[key];
} else if (typeof dictA[key] === 'object') {
if (typeof dictB[key] !== 'object') {
dictB[key] = {};
}
Expand Down Expand Up @@ -211,4 +213,8 @@ export function percentRoundString(value: number | string, decimals: number = 0,

export function isDict(o: any): boolean {
return o === Object(o) && !Array.isArray(o) && typeof o !== 'function';
}
}

export function objectDepth(o) {
return Object(o) === o ? 1 + Math.max(-1, ...Object.values(o).map(objectDepth)) : 0
}
47 changes: 47 additions & 0 deletions validations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export function isNameValidForInput(name: string | undefined | null, regexCheck: boolean = false): boolean {
if (regexCheck) return !!name && name.replace(/\s/g, '') != "";
else return !!name && name.trim() != "";
}



// multiple are considered AND connected
export enum CompareOptions {
IGNORE_CASE, // compares string and string list in lower case
TRIM, // trims string and string list
STARTS_WITH, //some start with string
ENDS_WITH, //some end with string
CONTAINS, //some contain string
EQUAL //some are equal to string
}

export function inStringList(value: string, list: string[], compareOptions?: CompareOptions[]): boolean {
if (!value || !list || list.length === 0) return false;
if (!compareOptions) return list.includes(value);

const [finalValue, finalList] = applyCompareOptions(value, list, compareOptions);
return finalList.some((item) => {
let result = true;
if (compareOptions.includes(CompareOptions.STARTS_WITH)) result = result && finalValue.startsWith(item);
if (compareOptions.includes(CompareOptions.ENDS_WITH)) result = result && finalValue.endsWith(item);
if (compareOptions.includes(CompareOptions.CONTAINS)) result = result && finalValue.includes(item);
if (compareOptions.includes(CompareOptions.EQUAL)) result = result && item === finalValue;

if (result) return true;
});

}

function applyCompareOptions(value: string, list: string[], compareOptions?: CompareOptions[]): [string, string[]] {
if (compareOptions) {
if (compareOptions.includes(CompareOptions.IGNORE_CASE)) {
value = value.toLowerCase();
list = list.map((item) => item.toLowerCase());
}
if (compareOptions.includes(CompareOptions.TRIM)) {
value = value.trim();
list = list.map((item) => item.trim());
}
}
return [value, list];
}