Skip to content

Instantly share code, notes, and snippets.

View nrkn's full-sized avatar

Nik nrkn

View GitHub Profile
@nrkn
nrkn / index.ts
Last active May 25, 2020 08:52
TypeScript pattern matching with predicates
import { Shape } from './shapes.types'
import { area, perimeter } from './shapes.lib'
const shapes: Shape[] = [
{ radius: 4 }, { side: 5 }, { width: 6, height: 7 }
]
const totalArea = shapes.reduce(
( sum, shape ) => sum + area( shape ),
0
// it's array-like, but it's not an array
const notAnArray = {
length: 3,
0: 'a',
1: 'b',
2: 'c'
}
console.log( Array.isArray( notAnArray ) ) // false
export const createRobot = ( name, job ) => ({ name, job })
export const isRobot = value =>
value && typeof value.name === 'string' && typeof value.job === 'string'
export const introduceRobot = ({ name, job }) =>
console.log( `Hi, I'm ${ name }. My job is ${ job }.` )
export const createPoint = ( x, y ) => ({ x, y })
export const isPoint = value =>
value && typeof value.x === 'number' && typeof value.y === 'number'
export const translatePoint = ( { x: x0, y: y0 }, { x: x1, y: y1 } ) =>
createPoint( x0 + x1, y0 + y1 )
@nrkn
nrkn / hedgehog.js
Last active May 27, 2019 23:30
Encapsulation with ES6 modules
export const Hedgehog = () => {
const speed = 10000
const name = 'Sonic'
const hedgehog = {
name,
zoom: () => zoom( hedgehog.name, speed )
}
return hedgehog
@nrkn
nrkn / index.html
Created January 22, 2019 04:14
WebGL CRT Neon Matrix Effect
<div><canvas width="640" height="480" /></div>
<!--
WebGL CRT monitor effect demo with a tiny surprise. Uses offscreen 2D canvas as buffer source.
Libraries and sources:
- ReGL (WebGL helper): http://regl.party/
- glMatrix (math): http://glmatrix.net/
@nrkn
nrkn / pokémon-strong-against.md
Created October 11, 2018 05:54
Mnemonics for remembering what Pokémon are strong against

Fighting is strong against Normal because:

  • A trained fighter will beat a normal person in combat

Fighting is strong against Rock because:

  • Fighting hits so hard it shatters rock

Fighting is strong against Steel because:

@nrkn
nrkn / GAME_MASTER_v0_1.protobuf
Created October 10, 2018 02:18 — forked from anonymous/GAME_MASTER_v0_1.protobuf
Pokemon Go decoded GAME_MASTER protobuf file v0.1
Result: 1
Items {
TemplateId: "BADGE_BATTLE_ATTACK_WON"
Badge {
BadgeType: BADGE_BATTLE_ATTACK_WON
BadgeRanks: 4
Targets: "\nd\350\007"
}
}
Items {
const createElement = ( type, props = {}, ...children ) => ({ type, props, children })