Skip to content

Instantly share code, notes, and snippets.

View brianfoshee's full-sized avatar
😎

Brian Foshee brianfoshee

😎
View GitHub Profile
@brianfoshee
brianfoshee / flag-emoji.js
Last active March 24, 2021 20:30
Generate a Country's Flag Emoji given its 2 letter Country Code
// Generate a Country's Flag Emoji given its 2 letter Country Code
// https://emojipedia.org/flags/
function flagEmojiFromCountryCode(code) {
if (code.length != 2) {
// Must be ISO 3166-1 alpha-2
throw new Error(`Country Code "${code}" must be 2 characters`);
}
var base = 0x1F1E6 // hex of unicode code point for 🇦, "Regional Indicator Symbol Letter A"
var a = 'a'.charCodeAt(0) // => 97. Used for calculating unicode offsets from base.

Keybase proof

I hereby claim:

  • I am brianfoshee on github.
  • I am brianfoshee (https://keybase.io/brianfoshee) on keybase.
  • I have a public key whose fingerprint is B1D6 5507 539E 26B2 D0BF 6C8E 0CF2 BD06 C38B 4EF1

To claim this, I am signing this object:

@brianfoshee
brianfoshee / main_test.go
Created June 24, 2016 18:35
string concat vs Sprintf bench
package main
import (
"fmt"
"testing"
)
var result string
func BenchmarkSprintf(b *testing.B) {
// bufPool is a pool of byte buffers meant to used when encoding JSON responses
// out to a client.
// giving this a try over:
// https://github.com/oxtoacart/bpool/blob/master/bufferpool.go
var bufPool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
@brianfoshee
brianfoshee / statusline.vim
Created March 22, 2016 19:43
setting a default status line without airline
set laststatus=2 " Always show status line
set statusline=%f " Path to the file
set statusline+=%< " If status line is too long truncate here
set statusline+=\ %y " File type, e.g. [go]
set statusline+=%r " [RO] if file is read only
set statusline+=%m " [+] if file is modified
set statusline+=%= " Switch to right side
set statusline+=%p%% " Percentage through file in lines
set statusline+=\ %l,%c " Line, Column numbers
@brianfoshee
brianfoshee / payment.go
Created March 21, 2016 16:26
use SQL RETURNING to keep Go object up to date after actions. Lots of the actual file is omitted, the important stuff is here.
type Payment struct {
ID string
Amount uint64
Name string
Description string
Email string
Status PaymentStatus
StripeTokenID string
// compareJSON is a lightweight alternative to
// https://github.com/onsi/gomega/blob/master/matchers/match_json_matcher.go
func compareJSON(actual []byte, expected []byte) error {
var aval interface{}
var eval interface{}
if err := json.Unmarshal(actual, &aval); err != nil {
return err
}
if err := json.Unmarshal(expected, &eval); err != nil {
// This package is an example of using custom context handlers and middleware.
// It is largely based on the final option in this article:
// https://joeshaw.org/net-context-and-http-handler/
// I have added a middleware chaining method and an associated ContextMW type.
//
// The idea is that in the case of this proposal being accepted,
// https://github.com/golang/go/issues/14660#issuecomment-193914014,
// where the context package would be in the standard library and implemented
// on http.Request, that these same handlers and middleware could be used after
// a brief refactoring to remove the custom types.
@brianfoshee
brianfoshee / pool.go
Last active March 8, 2016 18:35
test of a pool
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
func main() {
@brianfoshee
brianfoshee / loop_test.go
Created January 28, 2016 14:30
Benchmark finding a value in a slice
package loop
import "testing"
func BenchmarkFor(b *testing.B) {
for i := 0; i < b.N; i++ {
lang := "de-de"
supported := []string{"en-us", "ja-jp", "de-de"}
found := false
for _, s := range supported {