Skip to content

Instantly share code, notes, and snippets.

💾
Writing codes

Jed Mao jedmao

💾
Writing codes
View GitHub Profile
View keybase.md

Keybase proof

I hereby claim:

  • I am jedmao on github.
  • I am jedmao (https://keybase.io/jedmao) on keybase.
  • I have a public key ASA7BpF6w9OoKvLe-RIf_anWdjJFBXsYAEPV9yw8cSgctgo

To claim this, I am signing this object:

@jedmao
jedmao / transform.ts
Created Apr 8, 2020
TypeScript Transformer
View transform.ts
import { readFile as _readFile } from 'fs';
import * as ts from 'typescript';
import { promisify } from 'util';
const readFile = promisify(_readFile);
transform('input.ts');
async function transform(filename: string) {
const sourceFile = ts.createSourceFile(
@jedmao
jedmao / grammar.pegjs
Last active Oct 28, 2019
EditorConfig Grammar
View grammar.pegjs
Document = _ children:(Newline / Comment / Rule)* _ sections:Section* _ {
return {
type: 'EditorConfig',
version: '15.0.0',
children: children.concat(sections),
}
}
_ 'whitespace' = [ \t\n\r]*
View redux.ts
/** Essentials */
export type Primitive = string | number | boolean | bigint | symbol | undefined | null;
/** Like Readonly but recursive */
export type DeepReadonly<T> = T extends Primitive
? T
: T extends Function
? T
: T extends Date
? T
@jedmao
jedmao / onAddToCart.js
Last active Aug 27, 2019
TypeScript vs. JavaScript
View onAddToCart.js
// @ts-check
/**
* @typedef {{ items: Array<CartItem> }} Cart
* @typedef {{
* sku: string,
* name: string,
* price: number,
* color?: Color,
* size?: Size,
@jedmao
jedmao / extensions-to-language.yml
Created Aug 17, 2019
File extensions to languages
View extensions-to-language.yml
abap: ABAP
bat: Windows Bat
bib: BibTex
c: C
clj: Clojure
coffee: CoffeeScript
cpp: C++
cs: C#
cshtml: Razor page/view
css: CSS
View language_identifiers.yml
abap: ABAP
bat: Windows Bat
bibtex: BibTeX
clojure: Clojure
coffeescript: Coffeescript
c: C
cpp: C++
csharp: C#
css: CSS
diff: Diff
@jedmao
jedmao / 01-Labeled.tsx
Last active Jun 25, 2019
Example of an extensible and themeable TypeScript React component.
View 01-Labeled.tsx
import React from 'react'
export interface LabeledProps {
label: string
/**
* @default 'input'
*/
children?: React.ReactNode
/**
* @default 'label'
@jedmao
jedmao / objectFromEntries.ts
Last active Mar 1, 2019
Object.fromEntries in TypeScript
View objectFromEntries.ts
type ObjectPairs<T, X = keyof T, Y = string> = Array<[X, Y]> | Map<X, Y>
export function objectFromEntries<
T extends { [key: string]: any } = { [key: string]: any },
P extends ObjectPairs<T> = ObjectPairs<T>
>(pairs: P) {
const result = {} as T
for (const [key, value] of pairs.entries()) {
result[key] = value
}
return result
@jedmao
jedmao / AddToCartFormWithHooks.tsx
Last active Apr 8, 2020
React Hooks and Render Props in TypeScript
View AddToCartFormWithHooks.tsx
import { FC, useCallback } from 'react'
import { connect } from 'react-redux';
import addToCart from '../actions/cart'
import useAddToCart, { UseAddToCartOptions } from './useAddToCart'
interface DispatchProps {
onSubmit(options: UseAddToCartOptions): Promise<void>,
}
You can’t perform that action at this time.