Skip to content

Instantly share code, notes, and snippets.

@ShinobiWPS
Last active October 7, 2025 10:13
Show Gist options
  • Save ShinobiWPS/35d17eff5d13d1d787756aeaae7f2a6d to your computer and use it in GitHub Desktop.
Save ShinobiWPS/35d17eff5d13d1d787756aeaae7f2a6d to your computer and use it in GitHub Desktop.
react , vue ,functions
async function catchError<T>(promise: Promise<T>): Promise<[undefined,T] | [Error]> {
try {
const data = await promise
return [undefined, data] as [undefined, T]
} catch (error) {
return [error]
}
}
export const getUrlParams = (params) => {
if (!params) return undefined
const url = new URL(window.location.href)
const search = Array.isArray(params) ? params : [params]
const found = search.map((p) => url.searchParams.get(p))
return found.length === 1 ? found[0] : found
}
function objectKeyMatch() {
const object = {
default: function(){},
key: function(){},
}
const bella = (object[typeOfAction] || object.default)
bella()
}
//fetch ajax url link resources
export const useFetch = url => {
const [data, setData] = useState(null);
async function fetchData() {
const response = await fetch(url);
const json = await response.json();
setData(json);
}
useEffect(() => {fetchData()},[url]);
return data;
}
//valdiate validation programmatically html5
constructor() {
super();
this.form = React.createRef();
this.validate = this.validate.bind(this);
}
validate() {
this.form.current.reportValidity();
}
render() {
return (
<div>
<form ref={this.form} onSubmit={e => e.preventDefault()}>
<input placeholder="I'm required" required />
<input
placeholder="I must match \d{3} (three digits)"
required
pattern="\d{3}"
/>
</form>
<button onClick={this.validate}>Validate me</button>
</div>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment