Skip to content

Instantly share code, notes, and snippets.

Avatar
🌹
youtube.com/basaratali

Basarat Ali Syed basarat

🌹
youtube.com/basaratali
View GitHub Profile
View demo.ts
import { add } from 'date-fns';
/** Add one day to a given input */
function tomorrow(date: Date) {
return add(date, { days: 1 });
}
const now = new Date('2022-04-03 00:00:00');
const next = tomorrow(now);
console.log(now.toLocaleString(), '--', next.toLocaleString());
View DeepReadonly.ts
type DeepReadonly<T> = {
readonly [P in keyof T]
: DeepReadonly<T[P]>;
}
@basarat
basarat / LICENCE
Created Dec 24, 2020
This license applies to all public gists https://gist.github.com/basarat
View LICENCE
MIT License
Copyright (c) 2020 Basarat Ali Syed
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
View mixin.ts
export type Class = new (...args: any[]) => any;
export function DisposableMixin<Base extends Class>(base: Base) {
return class extends base {
isDisposed: boolean = false;
dispose() {
this.isDisposed = true;
}
};
}
View process.log
// create react app
npx create-react-app my-app --template typescript
cd my-react-app
// install
npm i cypress cypress-react-unit-test
// tsconfig.json
"types": [
@basarat
basarat / app.tsx
Last active Feb 5, 2020
React useReuse pattern 🌹
View app.tsx
export function App() {
const counterOne = useCounter();
const counterTwo = useCounter();
return (
<div>
<Counter use={counterOne}/>
<Counter use={counterTwo}/>
@basarat
basarat / counter.tsx
Created Feb 5, 2020
React useReuse pattern
View counter.tsx
function Counter() {
// Some hooks the component needs
const [count, setCount] = useState(0);
// The rendering of the component
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
@basarat
basarat / validators.tsx
Created Oct 18, 2019
some validators to use with formstate
View validators.tsx
import { Validator, applyValidators } from 'formstate';
/** One of the easy to validate values */
export type SimpleValue = string | boolean | number | null | undefined;
/**
* - Whitespace strings / false / null / undefined are considered invalid
*/
export const required: Validator<SimpleValue> = (value) => {
const error = "Value Required";
@basarat
basarat / _app.tsx
Last active Apr 6, 2022
nextjs-typestyle 🌹
View _app.tsx
import App from 'next/app'
import { setStylesTarget } from 'typestyle'
export default class MyApp extends App {
componentDidMount() {
/**
* Hydrate typestyle
*/
setStylesTarget(document.getElementById('styles-target')!)
}
View retry.ts
/**
* Adds retries to an async function.
*/
async function retry<T>(fn: () => Promise<T>, n: number): Promise<T> {
let lastError: any;
for (let index = 0; index < n; index++) {
try {
return await fn();
}
catch (e) {