Skip to content

Instantly share code, notes, and snippets.

@Peeja
Peeja / useObservable.test.ts
Created September 17, 2021 01:47
Read the latest value of an Observable in a React hook (WIP, stashed from another project when it became unnecessary.)
/**
* @jest-environment jsdom
*/
import { useObservable } from "./useObservable";
import { renderHook, act } from "@testing-library/react-hooks";
import { Subject } from "rxjs";
describe("useObservable()", () => {
it("returns each value emitted by the Observable", () => {
type PathSegments<Path extends string, Segments = never> = Path extends `${infer Segment}/${infer Rest}` ? PathSegments<Rest, Segments | Segment> : Segments | Path;
type ParamSegments<S extends string> = S extends `:${infer P}` ? P : never;
let segments: PathSegments<'/posts/:id/comments/:comment_id'>;
interface Routes {
match<Path extends string>(path: Path, cb: (params: Record<ParamSegments<PathSegments<Path>>, string>) => void): void;
}
declare const routes: Routes
@Peeja
Peeja / logged-parser.cljs
Created September 24, 2016 16:28
Om Next parser middleware that adds logging for debugging
(defn logged-parser [parser]
(fn [env query target]
(js/console.groupCollapsed "parser:" (if target (str "(" target ")") ""))
(js/console.trace)
(js/console.debug (with-out-str (pprint query)))
(let [ret (parser env query target)]
(js/console.debug (with-out-str (pprint ret)))
(js/console.groupEnd)
ret)))
@Peeja
Peeja / rerender.cljs
Created September 9, 2016 23:00
What it appears to take to re-render the React tree when Figwheel reloads.
;; Re-render when Figwheel reloads.
(gevents/listen js/document.body
"figwheel.js-reload"
(fn []
(let [root-component (om-next/app-root (compassus/get-reconciler a))]
(letfn [(js-vals [o]
(map #(aget o %) (js-keys o)))
;; Finds the children of a React internal instance of a component.
;; That could be a single _renderedComponent or several
;; _renderedChildren.
@Peeja
Peeja / keybase.md
Last active September 1, 2015 15:28

Keybase proof

I hereby claim:

  • I am peeja on github.
  • I am peeja (https://keybase.io/peeja) on keybase.
  • I have a public key whose fingerprint is B2B0 C79D FC2C 19FD C31D 4E46 960B 8671 0FDE B8BE

To claim this, I am signing this object:

time = Time.mktime(2014, 10, 14, 12, 1)
allowed_ranges = [
[11, 59]..[12, 1],
]
formatted_time = [time.hour, time.min]
p allowed_ranges.any? { |range| range.cover?(formatted_time) }
module AliasMethodChainResilient
# Give us a module which will intercept attempts to alias methods named in
# the keys of method_aliases, and instead alias the methods named in the
# values of method_aliases.
def self.alias_intercepting_module(method_aliases)
Module.new do
define_method :alias_method do |new_name, old_name|
if method_aliases.key?(old_name.to_sym)
super new_name, method_aliases[old_name.to_sym]
else
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
GEMFILE
system 'bundle'
end
@Peeja
Peeja / hash_map.rb
Created July 2, 2013 01:12
A new use for Ruby's `protected`.
# Say you're implementing a hash map, and you've got two
# sets of algorithms which are more efficient at
# different sizes:
class HashMap
class << self
def with_values(values)
if values.size > 64
LargeHashMap.new(values)