Skip to content

Instantly share code, notes, and snippets.

@webbower
webbower / line.sh
Created February 5, 2019 00:53
Bash command to get a line from a file
# https://stackoverflow.com/a/6022431/2684520
function line() {
sed "${1}q;d" "$2"
}
@webbower
webbower / include-once.scss
Created December 19, 2018 22:24 — forked from StefanoRausch/include-once.scss
Import Once Feature Implemented in Sass.
$is-included : () !default;
@mixin once( $name ) {
@if include-once( $name ) {
@content;
}
}
@function include-once( $name ) {
@if index( $is-included, $name ) {
@webbower
webbower / keyboard-map.js
Created March 19, 2018 00:35
JS Keyboard Event values
// https://css-tricks.com/snippets/javascript/javascript-keycodes/
/*Key Code
backspace 8
tab 9
enter 13
shift 16
ctrl 17
alt 18
pause/break 19
caps lock 20
@webbower
webbower / sass-helpers.scss
Last active March 19, 2018 01:04
SASS Helper and Utils
// Functions
// https://css-tricks.com/snippets/sass/px-to-em-functions/
@function emy($pixels, $context: $base-font-size) {
@if (unitless($pixels)) {
$pixels: $pixels * 1px;
}
@if (unitless($context)) {
$context: $context * 1px;
@webbower
webbower / utils.js
Created March 7, 2018 18:45
Simple and common misc JS utils to pick from as needed
function beginsWith(baseStr, targetStr) {
return baseStr.indexOf(targetStr) === 0;
}
function endsWith(baseStr, targetStr) {
return baseStr.substr(-targetStr.length) === targetStr;
}
@webbower
webbower / functional.php
Created February 13, 2018 19:48
Functional utilities a la PHP
<?php
function pipe(...$fns) {
return function($x) use ($fns) {
return array_reduce(
$fns,
function($acc, $fn) { return $fn($acc); },
$x
);
};
// A Unit test template for Tape
// See 5 Questions every unit test must answer:
// https://medium.com/javascript-scene/what-every-unit-test-needs-f6cd34d9836d
import test from 'tape';
test('What are you testing?', assert => {
const msg = 'what should it do?'
const actual = 'what was the output?';
// trace() is a utility to let you easily inspect
// the contents.
const trace = x => {
console.log(x);
return x;
};
const tap = (fn, tapper) => (...args) => {
const result = fn(...args);
tapper(result);
@webbower
webbower / shuffle-durstenfeld.js
Created December 2, 2017 22:02
Durstenfeld shuffle
// http://en.wikipedia.org/wiki/Fisher-Yates_shuffle#The_modern_algorithm
// https://stackoverflow.com/a/12646864/2684520
// Pre-ES6
/**
* Randomize array element order in-place.
* Using Durstenfeld shuffle algorithm.
*/
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
@webbower
webbower / shuffle-fy.js
Last active December 2, 2017 22:00
Fisher-Yates shuffle implementation
// https://stackoverflow.com/a/2450976/2684520
// https://bost.ocks.org/mike/shuffle/
function shuffle(array) {
var m = array.length, t, i;
// While there remain elements to shuffle…
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);