Skip to content

Instantly share code, notes, and snippets.

@gavr123456789
Last active November 22, 2021 16:08
Show Gist options
  • Save gavr123456789/f5a0b5b741b0ef67753753488b3d4db9 to your computer and use it in GitHub Desktop.
Save gavr123456789/f5a0b5b741b0ef67753753488b3d4db9 to your computer and use it in GitHub Desktop.
functional form rules ts
type Customer = {
name: string;
age: number;
}
type Rule<T> = (x: T) => null | string;
const ageOfMajority: Rule<number> =
x => x < 18 ? "Too young" : null;
const notTooOld: Rule<number> =
x => x > 90 ? "Too young" : null;
const required: Rule<any> =
x => !x ? "Required field" : null;
const firstCapitalLetter: Rule<string> =
x => x[0] === x[0].toLocaleUpperCase() ? "First letter must be capital" : null;
const noNumbersAllowed: Rule<string> =
x => /[^a-zA-Z]/g.test(x) ? "No Numbers Allowed" : null;
// mapped type
type FormRules<T> = {
[K in keyof T]: Rule<T[K]>
};
// Combine two rules with Higher Order functions
const compose =
<T>(a: Rule<T>, b: Rule<T>): Rule<T> =>
x => a(x) || b(x);
const rulesSimple: FormRules<Customer> = {
name: required,
age: ageOfMajority,
}
const rulesOnly2: FormRules<Customer> = {
name: compose(required, firstCapitalLetter),
age: compose(ageOfMajority, notTooOld)
}
const rules: FormRules<Customer> = {
name:
[
required,
firstCapitalLetter,
noNumbersAllowed
].reduce(compose),
age: compose(ageOfMajority, notTooOld)
}
const customer: Customer = { age: 2, name: "sas" }
function valudate(customer: Customer, rule: FormRules<Customer>) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment