first commit
This commit is contained in:
11
src/Utils/Array.js
Normal file
11
src/Utils/Array.js
Normal file
@@ -0,0 +1,11 @@
|
||||
export const splitEvery = (inputArray, perChunk) => {
|
||||
const chunks = inputArray.reduce((resultArray, item, index) => {
|
||||
const chunkIndex = Math.floor(index / perChunk)
|
||||
if (!resultArray[chunkIndex]) {
|
||||
resultArray[chunkIndex] = [];
|
||||
}
|
||||
resultArray[chunkIndex].push(item);
|
||||
return resultArray;
|
||||
}, []);
|
||||
return chunks;
|
||||
};
|
||||
103
src/Utils/Form.js
Normal file
103
src/Utils/Form.js
Normal file
@@ -0,0 +1,103 @@
|
||||
import { objectMap } from "./Object";
|
||||
import {
|
||||
isValidDateString,
|
||||
isValidEmailString,
|
||||
isValidTelephoneString,
|
||||
isValidZipCodeString,
|
||||
} from "./String";
|
||||
|
||||
export const getFormDataDefaults = (fields, defaults) =>
|
||||
objectMap(
|
||||
(field, key) => ({
|
||||
value:
|
||||
(typeof defaults === "undefined" ? field.default : defaults[key]) || "",
|
||||
error: false,
|
||||
message: "",
|
||||
}),
|
||||
fields
|
||||
);
|
||||
|
||||
export const getValidatedFormData = (data, fields) =>
|
||||
objectMap((state, key) => {
|
||||
const field = fields[key];
|
||||
const value = state.value.trim();
|
||||
let error = false;
|
||||
let message = "";
|
||||
if (!error && field.required && value === "") {
|
||||
error = true;
|
||||
message = "This field is required";
|
||||
}
|
||||
if (
|
||||
!error &&
|
||||
typeof field.minLength !== "undefined" &&
|
||||
((!field.required && value === "") || value.length < field.minLength)
|
||||
) {
|
||||
error = true;
|
||||
message = `This field require at least ${field.minLength} symbols`;
|
||||
}
|
||||
if (
|
||||
!error &&
|
||||
field.type === "date" &&
|
||||
((!field.required && value === "") || !isValidDateString(value))
|
||||
) {
|
||||
error = true;
|
||||
message = "Date format must be MM/DD/YYYY";
|
||||
}
|
||||
if (
|
||||
!error &&
|
||||
field.type === "email" &&
|
||||
((!field.required && value === "") || !isValidEmailString(value))
|
||||
) {
|
||||
error = true;
|
||||
message = "Invalid email format";
|
||||
}
|
||||
if (
|
||||
!error &&
|
||||
field.type === "phone" &&
|
||||
((!field.required && value === "") || !isValidTelephoneString(value))
|
||||
) {
|
||||
error = true;
|
||||
message = "Telephone format must be (###) ###-####";
|
||||
}
|
||||
if (
|
||||
!error &&
|
||||
field.type === "zip" &&
|
||||
((!field.required && value === "") || !isValidZipCodeString(value))
|
||||
) {
|
||||
error = true;
|
||||
message = "Zip Code must be 5 Digit";
|
||||
}
|
||||
if (
|
||||
!error &&
|
||||
field.type === "confirmPassword" &&
|
||||
data[field.compareTo].value !== value
|
||||
) {
|
||||
error = true;
|
||||
message = "Passwords do not match";
|
||||
}
|
||||
return {
|
||||
...state,
|
||||
value,
|
||||
error,
|
||||
message,
|
||||
};
|
||||
}, data);
|
||||
|
||||
export const handleFormDataChange = (e, name, data, fields) => {
|
||||
const value =
|
||||
(typeof e?.target?.value === "undefined" ? e.value : e.target.value) || "";
|
||||
const field = fields[name];
|
||||
if (
|
||||
typeof field.maxLength === "undefined" ||
|
||||
value.length <= field.maxLength
|
||||
) {
|
||||
return { ...data, [name]: { ...data[name], value } };
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
export const isFormDataHasErrors = (data) =>
|
||||
Object.values(data).reduce(
|
||||
(hasErrors, { error }) => hasErrors || error,
|
||||
false
|
||||
);
|
||||
7
src/Utils/Object.js
Normal file
7
src/Utils/Object.js
Normal file
@@ -0,0 +1,7 @@
|
||||
export const objectMap = (callback, inputObject) => Object.keys(inputObject).reduce(
|
||||
(acc, key) => {
|
||||
acc[key] = callback(inputObject[key], key);
|
||||
return acc
|
||||
},
|
||||
{}
|
||||
);
|
||||
103
src/Utils/String.js
Normal file
103
src/Utils/String.js
Normal file
@@ -0,0 +1,103 @@
|
||||
export const isValidDateString = (str) => {
|
||||
const strVal = String(str);
|
||||
const re = /^\d{2}\/\d{2}\/\d{4}$/;
|
||||
if (!re.test(strVal)) {
|
||||
return false;
|
||||
}
|
||||
return !isNaN(new Date(strVal));
|
||||
};
|
||||
|
||||
export const isValidEmailString = (str) => {
|
||||
const re =
|
||||
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
||||
return re.test(String(str).toLowerCase());
|
||||
};
|
||||
|
||||
export const isValidZipCodeString = (str) => {
|
||||
const re = /^\d{5}/;
|
||||
return re.test(String(str));
|
||||
};
|
||||
|
||||
export const isValidTelephoneString = (str) => {
|
||||
const re = /^(\+0?1\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/;
|
||||
return re.test(String(str));
|
||||
};
|
||||
|
||||
export const capFirstChar = (string) => {
|
||||
return string?.charAt(0).toUpperCase() + string?.slice(1);
|
||||
};
|
||||
|
||||
export const remArtifacts = (string) => {
|
||||
string.replace("request no.", "");
|
||||
};
|
||||
|
||||
export const capMonths = (string) => {
|
||||
if (!string) {
|
||||
return;
|
||||
}
|
||||
const months = [
|
||||
"january",
|
||||
"february",
|
||||
"march",
|
||||
"april",
|
||||
"may",
|
||||
"june",
|
||||
"july",
|
||||
"august",
|
||||
"september",
|
||||
"october",
|
||||
"november",
|
||||
"december",
|
||||
];
|
||||
let pre = null;
|
||||
let post = null;
|
||||
months.forEach((month) => {
|
||||
if (string?.includes(month)) {
|
||||
pre = string.substring(0, string?.indexOf(month));
|
||||
const temp = string.slice(string.indexOf(month));
|
||||
post = temp?.charAt(0).toUpperCase() + temp?.slice(1);
|
||||
}
|
||||
});
|
||||
if (pre) {
|
||||
if (post) {
|
||||
return pre + post;
|
||||
}
|
||||
} else {
|
||||
return string;
|
||||
}
|
||||
};
|
||||
|
||||
export const remSig = (string) => {
|
||||
if (!string) {
|
||||
return;
|
||||
}
|
||||
if (!string?.includes("respectfully")) {
|
||||
return string;
|
||||
}
|
||||
const temp = string?.substring(0, string.indexOf("respectfully"));
|
||||
return temp;
|
||||
};
|
||||
|
||||
export const scrubAll = (string) => {
|
||||
if (!string) {
|
||||
return;
|
||||
}
|
||||
const all = capFirstChar(remSig(capMonths(string)));
|
||||
const final = all.replace("first set of admissions", "");
|
||||
const final2 = final.replace("request", "");
|
||||
return final2;
|
||||
};
|
||||
|
||||
export const insertAt = (string) => {
|
||||
if (!string) {
|
||||
return;
|
||||
}
|
||||
const toFind = "RESPONDENT";
|
||||
const char = "\n";
|
||||
function findPosition(string, toFind) {
|
||||
return string?.lastIndexOf(toFind);
|
||||
}
|
||||
const pos = findPosition(string, toFind);
|
||||
|
||||
return string.slice(0, pos) + char + string.slice(pos);
|
||||
};
|
||||
Reference in New Issue
Block a user