Skip to content

Instantly share code, notes, and snippets.

@oriSomething
oriSomething / curl-animations.js
Created November 26, 2022 07:47
Server that causes animations for curl
/**
* The command to see the animation:
* curl localhost:3000
*
* @see https://github.com/alexeyraspopov/picocolors/blob/main/picocolors.js
* @see https://github.com/sindresorhus/ansi-escapes/blob/main/index.js
* @see https://github.com/sindresorhus/cli-spinners
*/
import { Readable } from "node:stream";
import { fastify } from "fastify";
import Component from '@ember/component';
export default Component.extend({
tagName: '',
key: undefined,
keys: Ember.computed('key', function () {
return [{
key: String(this.get("key"))
}];
}).readOnly(),
@oriSomething
oriSomething / ScopedStorage.ts
Last active August 27, 2020 17:28
ScopedStorage - Storage API with scope
export class ScopedStorage implements Storage {
[name: string]: any;
#storage: Storage;
#prefix: string;
constructor(storage: Storage, prefix: string) {
this.#storage = storage;
this.#prefix = prefix;
@oriSomething
oriSomething / useResizeObserver.ts
Created February 20, 2019 08:54
React hook - useResizeObserver
import ResizeObserver from "resize-observer-polyfill";
import * as React from "react";
import ReactDOM from "react-dom";
type DOMRectReadOnly = ResizeObserverEntry["contentRect"];
const callbacks = new Map<Element, (_: DOMRectReadOnly) => void>();
const ro = new ResizeObserver(entries => {
ReactDOM.unstable_batchedUpdates(() => {
@oriSomething
oriSomething / usePreviousValue.ts
Created February 20, 2019 08:50
React hook - usePreviousValue
import * as React from "react";
export function usePreviousValue<T>(value: T): T {
const refPrevious = React.useRef(value);
const refCurrent = React.useRef(value);
if (refPrevious.current !== refCurrent.current) {
refPrevious.current = refCurrent.current;
}
@oriSomething
oriSomething / asArray.js
Created February 26, 2018 13:11
Make Immutable.List useful for Array consume functions without immediate cast to Array
function asArray(target) {
if (!Immutable.List.isList(target)) {
throw new TypeError("target is not Immutable.List");
}
return new Proxy(new Array(target.size), {
get(__, key, receiver) {
if (typeof key !== "symbol") {
const index = parseInt(key, 10);
@oriSomething
oriSomething / StorybookOneWayBinding.ts
Last active January 19, 2018 10:04
Storybook one way binding updater. So knobs can be used with self update components
import * as React from "react";
type RenderT<State> = (
state: State,
update: <K extends keyof State>(key: K, value: State[K]) => void,
) => React.ReactNode;
interface Props<State> {
state: State;
render: RenderT<State>;
@oriSomething
oriSomething / sokoban.html
Last active November 3, 2017 10:54
Sokoban clone
<body onload='
K = onkeydown = ({ keyCode: k }) => {
if (k > 36 && k < 41) {
ox = (k -= 38)==2?0:k;
oy = k==-1?0:k-1;
A = M[y][x];
B = M[y + oy][x + ox] || "#";
C = (Y=M[y + oy * 2]||[])[x + ox * 2];
a = A == "+" ? "." : " ";
@oriSomething
oriSomething / file.js
Created August 13, 2017 12:58
JavaScript Setters don't effect the expression result as expected
var obj = {
_fooValue: 1,
_fooGetterShouldReturnThis: false,
get foo() {
var value = this._fooGetterShouldReturnThis ? this : this._fooValue;
this._fooGetterShouldReturnThis = false;
return value;
},
@oriSomething
oriSomething / file.ts
Created July 17, 2017 16:23
TypeScript tricks
// Source #1: http://ideasintosoftware.com/typescript-advanced-tricks/
// Source #2: https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-308052919
type Diff<T extends string, U extends string> = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T];
type Omit<T, K extends keyof T> = {[P in Diff<keyof T, K>]: T[P]};
// Example: type TCleanedUser = Omit<IUser, 'privateField1' | 'privateField2'>;