Skip to content

Instantly share code, notes, and snippets.

💾
Writing codes

Jed Mao jedmao

💾
Writing codes
Block or report user

Report or block jedmao

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@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 Jun 12, 2019
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>,
}
View config.ts
import { createNinja, Katana, Shuriken } from './entities';
export default {
Warrior: createNinja,
Weapon: () => new Katana(),
ThrowableWeapon: () => new Shuriken(),
};
@jedmao
jedmao / actions.ts
Last active Feb 21, 2019
TypeScript React Thunk Action Creator
View actions.ts
import { Action } from 'redux';
import { AppActionCreator } from '.';
interface FooAction extends Action<'FOO'> {
payload: string;
}
export const foo: AppActionCreator<FooAction> = (
bar: string,
View ResponsePayload.ts
export default interface ResponsePayload {
errors?: ResponsePayloadError[];
// tslint:disable-next-line:no-any
[key: string]: any;
}
/**
* https://jsonapi.org/format/#error-objects
*/
export interface ResponsePayloadError {
View transpile.test.ts
import transpile, { Writer } from './transpile';
describe('transpile', () => {
describe('given a reader that always reads "foobarbaz"', () => {
const reader = { read: () => 'foobarbaz' };
describe('given a default writer', () => {
@jedmao
jedmao / EventEmitter.test.ts
Last active Apr 13, 2019
TypeScript Polling / Event Emitter
View EventEmitter.test.ts
import { noop } from 'lodash';
import EventEmitter from './EventEmitter';
describe('EventEmitter class', () => {
describe('on()', () => {
it('subscribes to foo event with listener', () => {
const e = new EventEmitter();
e.on('foo', noop);
You can’t perform that action at this time.