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 / 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";
/** 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;
/**
* 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) {
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;
}
@basarat
basarat / pre.tsx
Last active August 22, 2018 00:49
Debug JavaScript objects in TypeScript React
export const Pre: React.SFC<{ children: any }> = (props) => {
return <pre>{JSON.stringify(props.children, null, 2)}</pre>
}
@basarat
basarat / insertionsort.py
Created July 31, 2012 13:01
Insertion sort in python
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.
@basarat
basarat / styles.less
Last active December 13, 2017 16:49
Atom Styles.less
// 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;
}
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';
/**