Skip to content

Instantly share code, notes, and snippets.

View basarat's full-sized avatar
🌹
youtube.com/basaratali

Basarat Ali Syed basarat

🌹
youtube.com/basaratali
View GitHub Profile
@basarat
basarat / script.md
Created March 13, 2023 06:02
Promise Fulfilled vs Resolved

Notice that we’ve been using the term, fulfilled instead of resolved, and the reason is that a promise can be resolved to another promise and which point its fate becomes dependent on the other promise. If the other promise gets fulfilled our promise gets fulfilled, if the other promise is rejected, our promise gets rejected.

Both alpha and beta are resolved, but they settle to different fates. Alpha gets fulfilled, Beta gets rejected.

const delayFulfill =
  () => new Promise(
    res => setTimeout(() => res('fulfilled'), 1000)
 );
@basarat
basarat / demo.ts
Last active February 21, 2022 22:02
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());
type DeepReadonly<T> = {
readonly [P in keyof T]
: DeepReadonly<T[P]>;
}
@basarat
basarat / LICENCE
Created December 24, 2020 02:24
This license applies to all public gists https://gist.github.com/basarat
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:
@basarat
basarat / mixin.ts
Last active August 18, 2023 20:29
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;
}
};
}
// 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 February 5, 2020 22:46
React useReuse pattern 🌹
export function App() {
const counterOne = useCounter();
const counterTwo = useCounter();
return (
<div>
<Counter use={counterOne}/>
<Counter use={counterTwo}/>
@basarat
basarat / counter.tsx
Created February 5, 2020 22:20
React useReuse pattern
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 October 18, 2019 06:19
some validators to use with formstate
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 April 6, 2022 19:27
nextjs-typestyle 🌹
import App from 'next/app'
import { setStylesTarget } from 'typestyle'
export default class MyApp extends App {
componentDidMount() {
/**
* Hydrate typestyle
*/
setStylesTarget(document.getElementById('styles-target')!)
}