JavaScript
If you are using JavaScript instead of TypeScript, you may need to Remove Type Definitions.
Turn (objectWithTypeDefinition?: typeDefinition)
into (objectWithoutTypeDefinition)
.
Code gets more Advanced towards the bottom!
getCurrentPageName
getCurrentPageName.js
export const getCurrentPageName = () => {
return window.location.hash.slice(window.location.hash.lastIndexOf(`/`)).replace(`/`, ``);
};
getNumberFromString
getNumberFromString.js
export const getNumberFromString = (string: string) => {
let result: any = string.match(/\d+/);
let number = parseInt(result[0]);
return number;
};
createHTMLElementFromXMLString
createHTMLElementFromXMLString.js
export const createHTMLElementFromXMLString = (xmlString: string) => {
let div = document.createElement('div');
div.innerHTML = xmlString.trim();
return div.firstChild;
};
cutOffTextAndReplace
cutOffTextAndReplace.js
export const cutOffTextAndReplace = (string: string, end: number, replacement?: string) => {
if (!replacement) {
replacement = `...` || `-`;
}
return string?.length > end ? string?.substring(0, end - 1) + replacement : string;
};
capitalizeAllWordsInString
capitalizeAllWordsInString.js
export const capitalizeAllWordsInString = (string: string) => {
if (string != null || string != undefined) {
return string.replace(` `,` `).split(` `).map((word: any) => {
return word?.charAt(0)?.toUpperCase() + word?.slice(1).toLowerCase()).join();
};
}
};
removeDuplicateObjectFromArray
removeDuplicateObjectFromArray.js
export const removeDuplicateObjectFromArray = (arrayOfObjects: any) => {
const uniqueArray = arrayOfObjects?.filter((value?: any, index?: any) => {
const _value = JSON.stringify(value);
return index === arrayOfObjects?.findIndex((obj?: any) => {
return JSON.stringify(obj) === _value;
});
});
return uniqueArray;
};
getFormValuesFromFormFields
getFormValuesFromFormFields.js
export const getFormValuesFromFormFields = (formFields: any) => {
let formValues = [];
for (let i = 0; i < formFields.length; i++) {
let field = formFields[i];
if (field.type != `submit`) {
formValues.push({
[field.name]: field.value,
});
return formValues;
};
};
};