View demo.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type DeepReadonly<T> = { | |
readonly [P in keyof T] | |
: DeepReadonly<T[P]>; | |
} |
View LICENCE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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": [ |
View app.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function App() { | |
const counterOne = useCounter(); | |
const counterTwo = useCounter(); | |
return ( | |
<div> | |
<Counter use={counterOne}/> | |
<Counter use={counterTwo}/> | |
View counter.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
View validators.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
View _app.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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) { |
NewerOlder