Skip to content

Instantly share code, notes, and snippets.

@webbower
webbower / composition.js
Created August 19, 2014 23:34
Functional combinators and composition helpers
// Requires utils.list.js for toList(), first(), and rest()
function tap(callback, val) {
callback(val);
return val;
}
function flip(fn) {
return function() {
return fn.apply(this, toList(arguments).reverse());
@webbower
webbower / abs-center.css
Created October 22, 2014 17:40
CSS absolute center
/* http://frontendbabel.info/articles/css-tricks-by-wargaming/ */
.abs-center {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
@webbower
webbower / text-ellipsis.css
Created October 22, 2014 17:40
CSS ellipsis truncation
/* http://frontendbabel.info/articles/css-tricks-by-wargaming/ */
.text-overflow {
white-space: nowrap; /* No line breaks */
overflow: hidden; /* Hide text which does not fit the block */
text-overflow: ellipsis; /* Cut off with ellipsis */
display: block; /* Works only for block elements */
}
@webbower
webbower / gist:c58c7bc6060be62d3fd2
Last active March 10, 2023 01:40
Dense Array Generation
Array.apply(null, Array(n));
Array.from({length: 10}, (_, i) => i + 1)
@webbower
webbower / gist:27205c412a531b01112d
Created July 10, 2015 04:51
Fun(ctional) squaring
var n = 10;
// Simple squaring function
function square(n) {
return n * n;
}
// Flip the first and second args of a function signature
function flip(fn) {
return function(a, b) {
@webbower
webbower / safeloop.js
Created July 15, 2015 21:22
Safe loop wrapper
function safeLoop(max) {
max = (+max) || 100;
// Track how many times the function has been called
var ctr = 1;
return function(cmp) {
// Loop count test and increment counter. If we’re under the max and the live comparison is truthy, return the live comparison
if ((ctr++) > max) return false;
// Otherwise, return the comparison
@webbower
webbower / bool-helpers.js
Created July 15, 2015 21:26
Boolean helper functions
function existy(value) {
return value !== undefined && value !== null;
}
function falsey(value) {
return !existy(value) || value === false;
}
function truthy(value) {
return !falsey(value);
@webbower
webbower / getProp.php
Created July 15, 2015 21:27
Generic array or object property getter
<?php
function getProp($data, $key) {
if (is_object($data)) {
return $data->$key;
} else if (is_array($data)) {
return $data[$key];
}
return $data;
@webbower
webbower / class-exchange.php
Created July 15, 2015 21:29
PHP Classes that behave like classes from other languages
<?php
/**
* Immutable (uses ->copy() to modify instances), properties are implicitly defined by constructor signature, getting an undefined property
* throws an exception, type check instance properties, iterate over properties?...
*/
class ScalaCaseClass {
public function __construct()
{
// Somehow make it very easy to have all the arguments in the signature map to publicly accessible (faux-)instance properties
@webbower
webbower / friendly-link.js
Last active August 29, 2015 14:25
Allow JS links to respect opening in new window or now