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}/> | |
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 |
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"; |
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
/** If true is returned it is used to prevent a nav away */ | |
export type Check = () => true | false | undefined; | |
/** Checks we need to do before leave */ | |
let checks: Check[] = []; | |
/** Only alert in browsers */ | |
if (typeof window !== 'undefined') { | |
const getMessageIfAny = () => { | |
let message: ReturnType<Check> = false; |
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) { |
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 } from 'formstate'; | |
export const email: Validator<string | null | undefined> = (value: string | null | undefined) => { | |
if (value == null || value == '') return null; | |
value = value.trim(); | |
// src : http://emailregex.com/ | |
if (!/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/g.exec(value)) { | |
return "Not a valid email address"; | |
} | |
return null; | |
} |
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 const Pre: React.SFC<{ children: any }> = (props) => { | |
return <pre>{JSON.stringify(props.children, null, 2)}</pre> | |
} |
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
def insertionsort(A): | |
#we start loop at second element (index 1) since the first item is already sorted | |
for j in range(1,len(A)): | |
key = A[j] #The next item we are going to insert into the sorted section of the array | |
i = j-1 #the last item we are going to compare to | |
#now we keep moving the key back as long as it is smaller than the last item in the array | |
while (i > -1) and key < A[i]: #if i == -1 means that this key belongs at the start | |
A[i+1]=A[i] #move the last object compared one step ahead to make room for key | |
i=i-1 #observe the next item for next time. |
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
// Style the find results | |
atom-text-editor::shadow .highlight.find-result .region { | |
background: rgb(163,163,0); | |
transition: background-color 0.2s, border-color 0.2s; | |
} | |
atom-text-editor::shadow .highlight.current-result .region, | |
atom-text-editor::shadow .highlight.current-result ~ .highlight.selection .region { | |
background: orange; | |
border-color: red; | |
} |
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 { observer } from 'mobx-react'; | |
import * as field from './field'; | |
import * as React from 'react'; | |
import { InputStyles } from '../base/inputs'; | |
import { classes, cssRaw } from 'typestyle'; | |
import { FocusClassNames } from '../utils/dom'; | |
import { Dates } from '../utils/validation'; | |
import { Colors } from '../base/styles'; | |
/** |