Created
May 27, 2024 05:39
-
-
Save huzefamehidpurwala/d1209d7c9c4b1ae8e40cf1edb71d949b to your computer and use it in GitHub Desktop.
helpful typescript functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable */ | |
export function toTitleCase(str: string): string { | |
// Convert underscores, hyphens, and camelCase to spaces | |
str = str | |
.replace(/_/g, " ") | |
.replace(/-/g, " ") | |
.replace(/([a-z])([A-Z])/g, "$1 $2"); | |
// Add a space before a capital letter if it's preceded by a lowercase letter | |
str = str.replace(/([a-z])([A-Z])/g, "$1 $2"); | |
str = str | |
.replace(/([a-zA-Z])([0-9])/g, "$1 $2") | |
.replace(/([0-9])([a-zA-Z])/g, "$1 $2"); | |
return str.replace( | |
/\w\S*/g, | |
(txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase() | |
); | |
} | |
export const strHasDate = (str: string): boolean => | |
!!str.match(/[dD]ate/g)?.length; | |
export function ConvertDate(str: string): string { | |
const date = new Date(str), | |
mnth = ("0" + (date.getMonth() + 1)).slice(-2), | |
day = ("0" + date.getDate()).slice(-2); | |
return [mnth, day, date.getFullYear()].join("-"); | |
} | |
export function ConvertDateFromObj(date: Date): string { | |
const mnth = ("0" + (date.getMonth() + 1)).slice(-2), | |
day = ("0" + date.getDate()).slice(-2); | |
return [mnth, day, date.getFullYear()].join("-"); | |
} | |
export const checkDate = (val: string | number): boolean => { | |
const check = val && new Date(val); | |
try { | |
return check.toString() === "Invalid Date"; | |
} catch (error) { | |
console.error("error in checkDate", val, "error==", error); | |
return true; | |
} | |
}; | |
/** | |
* Checks if value is empty. A value is considered empty unless it’s an arguments object, array, string, or | |
* jQuery-like collection with a length greater than 0 or an object with own enumerable properties. | |
* | |
* @param args The array of values to inspect. | |
* @return Returns true if any value is empty, else false. | |
*/ | |
export const itemIsEmpty = (...args: any[]): boolean => { | |
for (const val of args) { | |
if ( | |
val === "" || | |
val === null || | |
val === "null" || // helpful when an item is to be checked after JSON.stringify | |
val === "undefined" || // helpful when an item is to be checked after JSON.stringify | |
val === undefined || | |
(typeof val === "object" && !Object.keys(val).length) | |
) { | |
return true; // If any argument is empty, return true | |
} | |
} | |
return false; // If none of the arguments are empty, return false | |
}; | |
export const checkObjHasVal = (objArr: any, colNameArr: any): boolean => { | |
for (let i = 0; i < objArr.length; i++) { | |
const obj = objArr[i]; | |
for (let j = 0; j < colNameArr.length; j++) { | |
const col = colNameArr[j]; | |
if (itemIsEmpty(obj[col])) return false; | |
} | |
} | |
return true; | |
}; | |
export const delayFunc = (milliSec: number): Promise<void> => { | |
return new Promise((resolve) => setTimeout(resolve, milliSec)); | |
}; | |
export const emptyFunc = (): void => { | |
// | |
}; | |
export const findObjById = ( | |
colArr: any[], | |
keyToSearch: string, | |
keyVal: string | |
): any => { | |
for (const colObj of colArr) { | |
if (colObj[keyToSearch] === keyVal) { | |
return colObj; | |
} | |
} | |
}; | |
// Function to extract the value after "...sites/" from a URL | |
export function extractSiteName(absoluteSiteURL: string): string { | |
const startIndex = absoluteSiteURL.indexOf("sites/") + "sites/".length; | |
if (startIndex === -1) { | |
return ""; // "sites/" not found in the URL | |
} | |
const endIndex = absoluteSiteURL.indexOf("/", startIndex); | |
if (endIndex === -1) { | |
return absoluteSiteURL.substring(startIndex); // Return the substring after "sites/" | |
} else { | |
return absoluteSiteURL.substring(startIndex, endIndex); // Return the substring between "sites/" and the next "/" | |
} | |
} | |
export const getInitials = (fullName: string): string => { | |
// Split the full name into individual words | |
const words = fullName.split(" "); | |
// Map over the words array and extract the first letter of each word | |
const initials = words.map((word) => word.charAt(0)); | |
// Join the extracted initials into a single string and convert to uppercase | |
return [initials.shift(), initials.pop()].join("").toUpperCase(); | |
}; | |
export enum ShortForms { | |
"businessPhones" = "Ph. No.", | |
"directReports" = "Subord.", | |
"displayName" = "Full Name", | |
"givenName" = "First Name", | |
"id" = "ID", | |
"imageUrl" = "Img", | |
"jobTitle" = "Job Title", // "Position", | |
"mail" = "Mail ID", | |
"manager" = "Manager", | |
"mobilePhone" = "Mob No.", | |
"officeLocation" = "Works at", // "Address" | |
"preferredLanguage" = "Language", | |
"surname" = "Surname", | |
"userPrincipalName" = "Mail ID", | |
"faxNumber" = "Fax No.", | |
"companyName" = "Company", | |
"employeeHireDate" = "Hire Date", | |
} | |
export enum FullForms { | |
"businessPhones" = "Business Phone Numbers", | |
"directReports" = "Subordinates", | |
"displayName" = "Full Name", | |
"givenName" = "First Name", | |
"id" = "ID", | |
"imageUrl" = "Avatar", | |
"jobTitle" = "Job Title", | |
"mail" = "Mail ID", | |
"manager" = "Manager", | |
"mobilePhone" = "Mobile Number", | |
"officeLocation" = "Office Location", | |
"preferredLanguage" = "Language", | |
"surname" = "Surname", | |
"userPrincipalName" = "Mail ID", | |
} | |
export const handleStringSort = (a, b, desc = false): -1 | 0 | 1 => { | |
const x = a.toLowerCase(); | |
const y = b.toLowerCase(); | |
if (x < y) return desc ? 1 : -1; | |
if (x > y) return desc ? -1 : 1; | |
return 0; | |
}; | |
export const getDataURL = async ( | |
file: Blob | |
): Promise<string | ArrayBuffer | null> => { | |
let dataURL: string | ArrayBuffer | null; | |
try { | |
dataURL = await new Promise<string | ArrayBuffer | null>((resolve) => { | |
const reader = new FileReader(); | |
reader.onload = () => resolve(reader.result); | |
reader.readAsDataURL(file); | |
}); | |
} catch (error) { | |
dataURL = null; | |
} | |
return dataURL; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment