0.11.0 • Published 9 months ago
@jasonsbarr/types v0.11.0
@jasonsbarr/types
A set of functional types and type-related conversions to supplement the core package types.
Basic Usage
Just import and use the types you need. For example, doing form field validation in a React app:
import { useState } from "react";
import { Validation, succeed, fail } from "@jasonsbarr/types/lib/Validation";
import { gte } from "@jasonsbarr/functional-core/lib/predicates/gte";
import { length as strLength } from "@jasonsbarr/functional-core/lib/string/length";
import { test } from "@jasonsbarr/functional-core/lib/regexp/test";
import { length } from "@jasonsbarr/functional-core/lib/array/length";
// This is not necessarily how you should handle your form field state - this is not a React tutorial
const [passFieldState, setPassFieldState] = useState("");
const [passFieldErrors, setPassFieldErrors] = useState([]);
const isPasswordLongEnough = (password) =>
gte(strLength(password), 10)
? succeed(password)
: fail(password, "Password must be at least 10 characters long");
const doesPasswordContainSpecialChars = (password) =>
test(/#!\.#\*&\^%\$\?\\\/@/, password)
? succeed(password)
: fail(password, "Password must contain at least one special character");
const isPasswordValid = (password) =>
Validation.empty()
.concat(isPasswordLongEnough(password))
.concat(doesPasswordContainSpecialChars(password));
const handlePasswordFieldChange = (event) => {
isPasswordValid(event.target.value).fold(
({ value, messages }) => {
setPassFieldState(value);
setPassFieldErrors(messages);
},
({ value }) => {
setPassFieldState(value);
setPassFieldErrors([]);
}
);
};
// For demonstration purposes only - this is not a React tutorial
export default function PasswordField() {
return (
<div className="form-control">
{gte(length(passFieldErrors), 1) && (
<p className="error">{passFieldErrors[0]}</p>
)}
<input
type="password"
value={passFieldState}
onChange={handlePasswordFieldChange}
/>
</div>
);
}
Documentation
0.11.0
9 months ago
0.10.1
10 months ago
0.10.0
10 months ago
0.9.5
2 years ago
0.3.4
2 years ago
0.9.4
2 years ago
0.9.3
2 years ago
0.5.4
2 years ago
0.8.0
2 years ago
0.7.1
2 years ago
0.9.2
2 years ago
0.5.5
2 years ago
0.5.0
2 years ago
0.4.1
2 years ago
0.5.2
2 years ago
0.6.0
2 years ago
0.4.2
2 years ago
0.3.0
3 years ago
0.3.2
3 years ago
0.3.1
3 years ago
0.2.2
3 years ago
0.3.3
3 years ago
0.2.0
3 years ago
0.1.5
3 years ago
0.1.4
3 years ago
0.1.3
3 years ago
0.1.2
3 years ago