Skip to content

Instantly share code, notes, and snippets.

<?php
/**
* Shuffles an array using mt_rand() instead of rand().
* CAUTION: Unlike shuffle(), returns a new array, leaving the source untouched. It's better that way.
* CAUTION: Won't work with associative arrays. Who would shuffle an associative array anyways?
*
* @param array $deck The index-based array to be shuffled.
* @return array A new array containing exactly the same elements in a random order.
*
* @see http://stackoverflow.com/questions/5694319/how-random-is-phps-shuffle-function
// Cartesian product of arrays
// @takes N arrays -- arguments *must* be arrays
// @returns an array of X arrays of N elements, X being the product of the input arrays' lengths.
function cartesianProduct(...arrays) {
function _inner(...args) {
if (arguments.length > 1) {
let arr2 = args.pop(); // arr of arrs of elems
let arr1 = args.pop(); // arr of elems
return _inner(...args,
////////
// CustomEase for GSAP (ES5)
////////
// Written using code from greensock/GreenSock-JS :
// https://github.com/greensock/GreenSock-JS/blob/master/src/uncompressed/easing/EasePack.js
var _gsScope = (typeof(module) !== "undefined" && module.exports && typeof(global) !== "undefined") ? global : this || window; //helps ensure compatibility with AMD/RequireJS and CommonJS/Node
var gs = (_gsScope.GreenSockGlobals || _gsScope).com.greensock;
var CustomEase = gs._class("easing.CustomEase", function(func) {
@hu9o
hu9o / drawRoundedRect.js
Created October 6, 2016 13:10
Draws a rectangle with rounded corners on a canvas.
function drawRoundedRect(context, x, y, width, height, borderRadius) {
if (!borderRadius) {
context.rect(x, y, width, height);
} else {
var widthMinusRad = width - borderRadius;
var heightMinusRad = height - borderRadius;
context.translate(x, y);
context.arc(borderRadius, borderRadius, borderRadius, Math.PI, Math.PI * 1.5);
context.lineTo(widthMinusRad, 0);
context.arc(widthMinusRad, borderRadius, borderRadius, Math.PI * 1.5, Math.PI * 2);
@hu9o
hu9o / handlebars-helper-each-paginate.js
Last active January 23, 2017 15:19
Handlebars helper: #each-paginate
var Handlebars = require('handlebars');
// with `ctx` = [a, b, c, d, e, f, g, h] and `count` = 3
// will iterate over [[a, b, c], [d, e, f], [g, h]]
Handlebars.registerHelper('each-paginate', function(context, opts) {
// similar to Python's range
var range = n => Array.from(new Array(n).keys());
// items per page and page count - defaults to a single page with all the items
var perPage = opts.hash.perPage || context.length;