Skip to content

Instantly share code, notes, and snippets.

@juice49
juice49 / promise-once-sort-of.js
Created March 21, 2018 13:49
Promise once (sort of)
function promiseOnce () {
let promise = null
return function (createPromise) {
if (promise) {
return promise
}
promise = createPromise()
@juice49
juice49 / u-ratio.styl
Created February 23, 2018 11:24
CSS intrinsic ratio with custom properties
.u-ratio
--ratioX 1
--ratioY 1
position relative
&:before
display block
padding-top calc(var(--ratioY) / var(--ratioX) * 100%)
content ''
@juice49
juice49 / index.js
Created January 22, 2018 10:37
Console log prefix
const log = (...message) =>
console.log.apply(console, [ '[PREFIX]', ...message ])
log('foo', 'bar', 'bat', 1, 2, 3)
// [PREFIX] foo bar bat 1 2 3
@juice49
juice49 / l.php
Created November 6, 2017 15:39
PHP log
<?php
function l ($name) {
return function ($line) use ($name) {
file_put_contents($name, $line . PHP_EOL, FILE_APPEND);
};
}
@juice49
juice49 / b.php
Created September 8, 2017 09:41
PHP benchmark
<?php
function b () {
$start = microtime(true);
return function () use ($start) {
$end = microtime(true);
return $end - $start;
};
}
@juice49
juice49 / index.js
Created October 6, 2016 15:20
Timeout a JS promise
'use strict';
const timeoutPromise = ms => promise => new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new PromiseTimeout());
}, ms);
promise.then(
res => {
clearTimeout(timeout);
resolve(res);
@juice49
juice49 / example.js
Last active October 6, 2016 15:19
Delay a JS promise
const delay = delayPromise(1000)
delay(() => fetch('/'))
.then(console.log)
@juice49
juice49 / append-around.js
Last active March 4, 2016 10:37
AppendAround with onRemove and onAppend callbacks
/*
* !!! IMPORTANT !!!
* This has been modified to accept "onRemove" and "onAppend" callbacks.
* See comments 1, 2, and 3.
*/
/*! appendAround markup pattern. [c]2012, @scottjehl, Filament Group, Inc. MIT/GPL
how-to:
1. Insert potential element containers throughout the DOM
2. give each container a data-set attribute with a value that matches all other containers' values
3. Place your appendAround content in one of the potential containers
@juice49
juice49 / index.js
Created February 18, 2016 21:29
Websocket router
function routeWebsocket(server, controllers = []) {
server.on('connection', client => {
client.on('message', ({ event, data }) => {
const controller = controllers[event];
if(controller) {
controller(data);
}
});
});
}
@juice49
juice49 / npm.md
Last active February 17, 2016 13:26
Useful npm scripts

HashMark is a small utility which takes a file (or sdtin) as input, and writes the contents of the input to a file named with a hash digest of the file.

#npm-run-all A CLI tool to run multiple npm-scripts in parallel or sequential.