Skip to content

Instantly share code, notes, and snippets.

View jiayihu's full-sized avatar
:octocat:
Nyaning in O(1)

Jiayi Hu jiayihu

:octocat:
Nyaning in O(1)
View GitHub Profile
{
"Right Option Key Sends" : 0,
"Tags" : [
],
"Ansi 12 Color" : {
"Red Component" : 0.1268280595541,
"Color Space" : "Calibrated",
"Blue Component" : 0.998526930809021,
"Alpha Component" : 1,
@jiayihu
jiayihu / action-lifecycle.middleware.ts
Created March 9, 2017 14:32
Allow subscription, as Promise, to redux-saga and redux-observable POJO actions
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
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 December 8, 2017 20:01
Pure JS animated scrollTop using requestAnimationFrame
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 / segment-intersection.js
Created September 23, 2018 20:30
Determine the intersection point of two line segments, but considering also colinear segments as intersection
/**
* 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 / intersect.ts
Created September 23, 2018 20:31
Determine the intersection of a segment, having ad edge in the center of circle, with the circle itself
/**
* 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 / getNearestPointInPerimeter.js
Last active September 23, 2018 21:10
Find the nearest point in the perimeter of a rectangle to a given point. JavaScript/TypeScript porting
/**
* 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 / Preview.md
Last active October 31, 2018 21:54
Chrome snippet to console log your localStorage in a pretty format

Preview

@jiayihu
jiayihu / actionCreator.ts
Last active September 20, 2019 13:10
Minimal action creator factory for redux-observable and redux-saga
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 {
@jiayihu
jiayihu / machine.js
Created October 23, 2019 09:07
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions