Skip to content

Instantly share code, notes, and snippets.

:octocat:
Nyaning in O(1)

Jiayi Hu jiayihu

:octocat:
Nyaning in O(1)
Block or report user

Report or block jiayihu

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
@jiayihu
jiayihu / utils.ts
Last active May 22, 2019
Cross-project utilities
View utils.ts
export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
export type ValueOf<T> = T[keyof T];
export function assertNever(_: never): never {
throw new Error('Unreachable code');
}
export function debounce<F extends (...params: Array<any>) => void>(fn: F, delay: number): F {
let timer: number;
@jiayihu
jiayihu / intersect.ts
Created Sep 23, 2018
Determine the intersection of a segment, having ad edge in the center of circle, with the circle itself
View intersect.ts
/**
* Determine the intersection of a segment, having ad edge in the center of circle,
* with the circle itself
*/
function intersect(
xc: number,
yc: number,
radius: number,
x1: number,
y1: number
@jiayihu
jiayihu / segment-intersection.js
Created Sep 23, 2018
Determine the intersection point of two line segments, but considering also colinear segments as intersection
View segment-intersection.js
/**
* Determine the intersection point of two line segments, but considering also
* colinear segments as intersection
* @see {@link http://paulbourke.net/geometry/pointlineplane/}
*/
function intersect(
x1, y1, x2, y2,
x3, y3, x4, y4
) {
@jiayihu
jiayihu / getNearestPointInPerimeter.js
Last active Sep 23, 2018
Find the nearest point in the perimeter of a rectangle to a given point. JavaScript/TypeScript porting
View getNearestPointInPerimeter.js
/**
* TS porting of https://stackoverflow.com/questions/20453545/how-to-find-the-nearest-point-in-the-perimeter-of-a-rectangle-to-a-given-point
*/
function clamp(x: number, lower: number, upper: number): number {
return Math.max(lower, Math.min(upper, x));
}
export function getNearestPointInPerimeter(
l: number,
@jiayihu
jiayihu / streams.hs
Last active May 26, 2018
Infinite stream in Haskell
View streams.hs
module Stream where
import Stream.Internal
import Debug.Trace
import Control.Arrow
import Control.Applicative
-- | Get the first element of a stream.
headS :: Stream a -> a
View copyStyles.js
function copyStyles(sourceDoc, targetDoc) {
Array.from(sourceDoc.styleSheets).forEach(styleSheet => {
if (styleSheet.cssRules) { // for <style> elements
const newStyleEl = sourceDoc.createElement('style');
Array.from(styleSheet.cssRules).forEach(cssRule => {
// write the text of each rule into the body of the style element
newStyleEl.appendChild(sourceDoc.createTextNode(cssRule.cssText));
});
@jiayihu
jiayihu / scroll.ts
Created Dec 8, 2017
Pure JS animated scrollTop using requestAnimationFrame
View scroll.ts
export function animateScrollTop(element: HTMLElement, value: number, timing: number) {
let start: number = null;
let animationToken: number;
const valueSign = value >= 0 ? 1 : -1;
const currentScroll = element.scrollTop;
const step = (timestamp: number) => {
if (!start) {
start = timestamp;
}
@jiayihu
jiayihu / action-lifecycle.middleware.ts
Created Mar 9, 2017
Allow subscription, as Promise, to redux-saga and redux-observable POJO actions
View action-lifecycle.middleware.ts
import { Middleware } from 'redux';
export interface IActionLifecycle {
resolveType: string;
rejectType: string;
}
/**
* Middleware which allows to chain async actions as Promises.
* @see https://github.com/redux-observable/redux-observable/issues/90
@jiayihu
jiayihu / actionCreator.ts
Last active Jul 9, 2019
Minimal action creator factory for redux-observable and redux-saga
View actionCreator.ts
import { IAction } from '../types/redux.types';
import { actionTypes as errorTypes, showError } from './errors.actions';
interface IPayloadCreators {
request(...args: any[]): any;
success(...args: any[]): any;
failure?(errorMsg: string): any;
}
interface IActionCreator {
View iTerm-profile.json
{
"Right Option Key Sends" : 0,
"Tags" : [
],
"Ansi 12 Color" : {
"Red Component" : 0.1268280595541,
"Color Space" : "Calibrated",
"Blue Component" : 0.998526930809021,
"Alpha Component" : 1,
You can’t perform that action at this time.