v-ingredients
Version:
Reusable Components
59 lines (46 loc) • 1.99 kB
text/typescript
import {FormInput} from "../../components/Form/types/FormInput";
function deepCopy(obj: any){
return JSON.parse(JSON.stringify(obj))
}
function getInput (name: string, inputs: FormInput[]): FormInput | null {
const input = inputs && inputs.length > 0 ? (inputs as FormInput[]).filter((input: FormInput) => input.name === name) : null
return input && input.length > 0 ? input[0] : null
}
// smooth scrolls to an input id and focuses the input after scroll. to simply scroll to something that is not an input, just use scrollIntoView()
function scrollToForm(id: string): void {
document.addEventListener("scroll", handleScroll, false);
const target = document.getElementById(id);
target!.scrollIntoView({behavior: "smooth", block: "start"});
let isScrolling: any;
function handleScroll() {
window.clearTimeout(isScrolling);
isScrolling = setTimeout(function() {
target!.focus();
document.removeEventListener("scroll", handleScroll);
}, 66);
}
}
function connectSimilarObjectKeys(obj: { [prop: string]: any }): { [prop: string]: any } {
const newObj: { [prop: string]: any } = {}
const keys = Object.keys(obj);
const values = Object.values(obj);
for (let i = 0; i < keys.length; ++i) {
const key = keys[i]
const value = values[i]
const theSameValueIndexes = values.map((e, i) => e === value ? i : '').filter(String)
if (theSameValueIndexes.length > 1) {
let newKey = ''
theSameValueIndexes.forEach((index: any): void => {
const thisKey = keys[parseInt(index)]
newKey += thisKey + ', '
delete obj[thisKey];
})
newKey = newKey.substring(0, newKey.length - 2)
newObj[newKey] = value
} else {
newObj[key] = value
}
}
return newObj;
}
export { connectSimilarObjectKeys, getInput, deepCopy, scrollToForm }