mirror of
https://github.com/formbricks/formbricks.git
synced 2025-12-30 18:30:32 -06:00
45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
export const shuffle = (array) => {
|
|
array = [...array];
|
|
let currentIndex = array.length,
|
|
randomIndex;
|
|
|
|
while (0 !== currentIndex) {
|
|
randomIndex = Math.floor(Math.random() * currentIndex);
|
|
currentIndex--;
|
|
|
|
[array[currentIndex], array[randomIndex]] = [
|
|
array[randomIndex],
|
|
array[currentIndex],
|
|
];
|
|
}
|
|
|
|
return array;
|
|
};
|
|
|
|
export const classNames = (...classes) => {
|
|
return classes.filter(Boolean).join(" ");
|
|
};
|
|
|
|
export const slugify = (...args: (string | number)[]): string => {
|
|
const value = args.join(" ");
|
|
|
|
return value
|
|
.normalize("NFD") // split an accented letter in the base letter and the acent
|
|
.replace(/[\u0300-\u036f]/g, "") // remove all previously split accents
|
|
.toLowerCase()
|
|
.trim()
|
|
.replace(/[^a-z0-9 ]/g, "") // remove all chars not letters, numbers and spaces (to be replaced)
|
|
.replace(/\s+/g, "-"); // separator
|
|
};
|
|
|
|
export const getFieldSetter = (obj, objSetter) => {
|
|
return (input, field) => setField(obj, objSetter, input, field);
|
|
};
|
|
|
|
export const setField = (obj, objSetter, input, field) => {
|
|
let newData = JSON.parse(JSON.stringify(obj));
|
|
newData[field] = input;
|
|
objSetter(newData, false);
|
|
return newData;
|
|
};
|