Skip to content

Instantly share code, notes, and snippets.

@juice49
juice49 / IterableUtils.php
Created August 14, 2018 13:24
IterableUtils
<?php
abstract class IterableUtils
{
public static function reduce(Iterable $collection, Callable $accumulator, $result = null)
{
foreach ($collection as $item) {
$result = $accumulator($result, $item);
}
@juice49
juice49 / index.js
Last active June 14, 2018 11:33
React auto render
import React from 'react'
import { render } from 'react-dom'
import Foo from './components/foo'
const components = {
foo: Foo
}
Object.keys(components)
.map(name => {
@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