Last active
August 22, 2024 21:43
-
-
Save pedrovasconcellos/ad706f1e1cb5c68d95ad3cfd4bd18c8d to your computer and use it in GitHub Desktop.
Utilities TypeScript
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
class Utilities { | |
public static isValidStringHeaders(headers: string): boolean { | |
const regex = /^[a-zA-Z0-9,_]+$/; | |
return regex.test(headers); | |
} | |
public static isValidStringHeaders(headers: string): boolean { | |
const allowedCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_,'; | |
for (let i = 0; i < headers.length; i++) { | |
if (!allowedCharacters.includes(headers[i])) { | |
return false; | |
} | |
} | |
return true; | |
} | |
public static getCaseInsensitiveValue(obj: { [key: string]: unknown }, key: string): any { | |
const foundKey = Object.keys(obj) | |
.find(currentKey => currentKey.toLowerCase() === key.toLowerCase()); | |
return foundKey ? obj[foundKey] : null; | |
} | |
public static replacePlaceholders(text: string, object: { [key: string]: string }): string { | |
Object.keys(object).forEach(key => { | |
const regex = new RegExp(`\\[\\[${key}\\]\\]`, 'gi'); | |
text = text.replace(regex, object[key]); | |
}); | |
return text; | |
} | |
public static replaceReplaceAllOccurrences( | |
text: string, | |
before: string, | |
after: string): string { | |
const pattern = new RegExp(`${before}`, 'g'); | |
return text.replace(pattern, after); | |
} | |
public static replacePlaceholders(text: string, object: { [key: string]: string }): string { | |
Object.keys(object).forEach(key => { | |
const placeholder = `[[${key}]]`; | |
let index = text.toLowerCase().indexOf(placeholder.toLowerCase()); | |
console.log('key', {key, placeholder}); | |
while (index !== -1) { | |
text = text.substring(0, index) + object[key] + text.substring(index + placeholder.length); | |
index = text.toLowerCase().indexOf(placeholder.toLowerCase(), index + object[key].length); | |
} | |
}); | |
return text; | |
} | |
public static ObjToDictionary(obj: object) : { [key: string]: string } { | |
let entries = Object.entries(obj); | |
let swappedEntries = entries.map(([key, value]) => [String(value), key]); | |
let dic = swappedEntries.reduce((obj : { [key: string]: string }, [value, key]) => { | |
obj[key] = value; | |
return obj; | |
}, {} as { [key: string]: string }); | |
return dic; | |
} | |
public static getListWithCSVHeaders(csvString: string): Array<string> { | |
const lines = csvString.split('\n'); | |
const headers = lines[0].trim(); | |
if(!headers) | |
return new Array<string>(); | |
return headers.split(','); | |
} | |
public static getStringWithCSVHeaders(csvString: string): string { | |
const lines = csvString.split('\n'); | |
const headers = lines[0].trim(); | |
if(!headers) | |
return ''; | |
return headers; | |
} | |
} | |
export default Util; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment