Skip to content

Instantly share code, notes, and snippets.

Avatar

Tim Ruffles timruffles

View GitHub Profile
@timruffles
timruffles / count_functions_loc.sh
Created Jun 12, 2020
A bash script that counts go function and method lines of code (including whitespace and comments)
View count_functions_loc.sh
#!/bin/bash
#
# Usage: bash count_functions_loc.sh some/directory
set -euo pipefail
main() {
for f in $(find $1 -name '*.go' -not -name 'test_*.go'); do
count_funcs < "$f" | awk "{ print \"$f\", \$1, \$2 }"
done
@timruffles
timruffles / ban_random_int.sh
Created Jul 11, 2019
A bad way to generate a random init in a range using only bash built-ins. Useful in a pinch when you aren't sure what external programs are available.
View ban_random_int.sh
# Gets an int between min max inclusive very inefficiently, but
# only using bash built-ins. More inefficient the smaller the gap
#
# usage: n=$( bad_random_int 1000 2000 )
bad_random_int() {
local min=$1
local max=$2
local n=0
while [[ "$n" -lt "$min" ]] || [[ "$n" -gt "$max" ]]; do
@timruffles
timruffles / go-quiz.go
Last active Aug 19, 2019
What does this program output, and why? Reason it out gophers! Answers in a spoiler block - https://github.com/dear-github/dear-github/issues/166#issuecomment-236342209
View go-quiz.go
package main
import "fmt"
func main() {
type person struct {
nickname string
}
ppl := []person{
@timruffles
timruffles / demo.go
Last active Mar 7, 2019
Go - cost of change for methods in application code. App code has many types that work together, requiring mocking.
View demo.go
// Let's define a type with a method that needs a fair few arguments
type Thing struct {}
func (c *Thing) Verb(a A,b B,c C,d D) {}
// We have things that depend on Thing and we want to test them, so to mock it we'll need to
// define an interface and generate a mock
type Thinger interface {
Verb(a A,b B,c C,d D)
}
@timruffles
timruffles / add.sh
Last active Feb 25, 2019
Go dep - adding a fork via `source`. Useful when you want to use a private fork of a public repo for instance, without updating all package paths.
View add.sh
SOURCE="github.com/you/thing"
FORK="github.com/other/thing"
PACKAGE="some-package"
CONSTRAINT="aabbcc"
dep ensure -add ${SOURCE}/${PACKAGE}:${FORK}.git@${CONSTRAINT}
@timruffles
timruffles / next.md
Last active Jul 22, 2020
Next.js page request handling
View next.md

How Next.js responds to a page request with HTML

We create the next request handler function via app.getRequestHandler(). This returns a standard express handler, so we use it like expressApp.get('*', middlewareA(), middlewareB(), nextApp.getRequestHandler()).

When the handler is invoked:

  • Server#handleRequest (next-server/server/lib/next-server.js)
    • Parses URL + query string if not already done
  • Server#run
    • Searches for matching route
@timruffles
timruffles / js-values-sketch.c
Created Jul 13, 2018
Representing JsValues for JS -> C compiler
View js-values-sketch.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdint.h>
#include <string.h>
/**
* So, plan is to store the values for the JS compiler
* in a union, nicking Lua's idea. That way immediate
* values like numbers live inside the JSValue wrapper,
@timruffles
timruffles / enums.ts
Last active Jun 15, 2017
string enums (ish, with some caveats) before ts 2.4
View enums.ts
// cast the strings as any
enum StringEnum {
a = 'a' as any,
b = 'b' as any,
}
const a = StringEnum.a;
const x = 'a';
const y = 1;
@timruffles
timruffles / story.md
Last active Jun 14, 2017
A story, wherein I discover 'share()' in RxJS is a tricky blighter
View story.md

The docs for .share() state: 'Returns a new Observable that multicasts (shares) the original Observable.'.

This sounds like share() is a lovely simple method that'll make your life easier. However share() is really a trap, for if you have the hubris to use .share() the angry complexity bees that live inside RxJS will swarm out and sting you in the eye (in a rate-limited fashion).

Let's see why. Our goal is to take a cold observable, take only the first item from it, do something to it, and share the resulting observable between 2 subscribers. They should both share the values from our new observable. Easy huh?

{
const coldObservable = Rx.Observable.create(function(observer) {
  console.log('observable created')
@timruffles
timruffles / troll_table.sql
Last active Jun 8, 2017
I tried to find a limit to Posgres's table+column names. There is none! 👍
View troll_table.sql
scratch=# create table "delete from account;" ("""id""" text primary key, "sutff %\n" text, "&" text, "1" text, U&"\+01F600" text);
CREATE TABLE
scratch=# \d "delete from account;"
Table "public.delete from account;"
Column | Type | Modifiers
-----------+------+-----------
"id" | text | not null
sutff %\n | text |
& | text |
1 | text |
You can’t perform that action at this time.