Skip to content

Instantly share code, notes, and snippets.

View karolk's full-sized avatar
💭
cooking on gas

Karol K karolk

💭
cooking on gas
View GitHub Profile
const randomise = word => word.split('').sort(() => Math.round(Math.random() * 2) -1).join('')
const dyslexify = word => word.substr(0,1) + randomise(word.substring(1, word.length-1)) + word.substr(-1,1)
setInterval(() => {
console.clear()
console.log(dyslexify('elementary'))
}, 1000)
@karolk
karolk / camelcase-proxy.js
Last active July 12, 2017 21:10
camelcasing proxy (toy)
const toSnakeCase = str => str.replace(/([A-Z])/g, match => '_' + match.toLowerCase())
const handler = {
get(target, name) {
const snakeName = toSnakeCase(name)
const value = target[snakeName]
if (value === Object(value) && !Array.isArray(value)) {
return new Proxy(value, handler)
} else {
return Reflect.get(target, snakeName);
@karolk
karolk / snapshot-testing.md
Last active July 27, 2017 14:12
Snapshot testing

Snapshot testing

Snapshot tests are a very useful tool whenever you want to make sure your UI does not change unexpectedly.

Instead of rendering the graphical UI, which would require building the entire app, you can use a test renderer to quickly generate a serializable value for your React tree.

Oftentimes when we not using snapshot testing you would end up writing tests like this:

// BuyNow.js
@karolk
karolk / range.map.js
Created February 10, 2017 19:55
Non iterative range (inclusive)
const range = (from, to) =>
[from]
.concat(Array(to-from).fill(from))
.map((n,index) => n+index)
@karolk
karolk / diamond.js
Last active February 15, 2017 00:00
Diamond kata
// Simple use (from letter A, space as filler):
// diamond()("F")
// Specify beginning of alphabet and different filler character:
// diamond("F", "_")("K")
const diamond = (startChar="A", whiteSpace=" ") => {
const range = (from, to) => [from]
.concat(Array(to-from).fill(from))
.map((n,index) => n+index)
@karolk
karolk / leftpad.es5.js
Last active April 1, 2016 11:50
leftpad
function leftpad(str, length, char) {
if (typeof str === "undefined") throw new Error("Cound't find string to leftpad")
str = String(str)
length = length || 0
if (typeof char === "undefined") char = " "
char = String(char)
var padding = leftpad._repeat(char, Math.max(length - str.length, 0))
return padding + str
@karolk
karolk / memo.coffee
Last active October 20, 2015 16:18
simple, but production ready implementation of memoisation in js
do () ->
getStorage = () ->
storage =
values: {}
isStored: (key) ->
key of @values
getValue: (key) ->
@values[key]
store: (key, value) ->
@values[key] = value
@karolk
karolk / index.html
Created July 27, 2015 22:36
mini spa
<!doctype html>
<script>
function $(selector) {
return document.querySelectorAll(selector)
}
function onClick(selector, callback) {
$("html")[0].addEventListener("click", function(event) {
@karolk
karolk / limited-array.js
Last active August 29, 2015 14:05
good enough Limited Array (containing not more than n elements)
//limitations
//you can still use other methods like arr[index], Array.prototype.push(arr) and others to avoid limits
Array.prototype.trim = function(max) {
return this.splice(max, this.length-max)
}
Array.prototype.trimRight = function(max) {
return this.splice(0, this.length-max)
}
@karolk
karolk / node-recorder.js
Last active August 29, 2015 13:57
Walks the DOM and records it as a JS object. Very useful for skimwords!
var generateNodeRecorer = function(forEach) {
function parseArgs(nodes, collecionOrNodeCallback, nodeCallback) {
//make sure single objects can be handled
if (!nodes.length) nodes = [nodes];
if (arguments.length === 0) {
throw new Error('Couldn\'t find nodes')
}