Skip to content

Instantly share code, notes, and snippets.

View SamuelDavis's full-sized avatar

Samuel Davis SamuelDavis

  • Stallings, NC, USA
View GitHub Profile
@SamuelDavis
SamuelDavis / debounce
Created May 13, 2014 01:16
David Walsh's minified Debounce JS method: http://davidwalsh.name/javascript-debounce-function
function debounce(a,b,c){var d;return function(){var e=this,f=arguments;clearTimeout(d),d=setTimeout(function(){d=null,c||a.apply(e,f)},b),c&&!d&&a.apply(e,f)}}
@SamuelDavis
SamuelDavis / phpInNotPHP
Created May 25, 2014 01:41
Include this in .htaccess to allow parsing of PHP inside of .js, .css, etc. files
AddType application/x-httpd-php .js .css
AddHandler x-httpd-php5 .js .css
<FilesMatch "\.(js|php|css)$">
SetHandler application/x-httpd-php
</FilesMatch>
@SamuelDavis
SamuelDavis / Add Git Submodules
Created December 10, 2014 06:18
Parses a .gitmodules file (named 'gitmodules') and runs git submodule add for every submodule in the file.
<?php
$submodules = array_filter(explode("\n", file_get_contents('./gitmodules')));
for($i = 0; $i < count($submodules); $i += 3) {
$path = str_replace('path = ', '', trim($submodules[$i + 1]));
$url = str_replace('url = ', '', trim($submodules[$i + 2]));
if($path && $url) {
$path = "./$path";
@SamuelDavis
SamuelDavis / git-cookbooks
Last active August 29, 2015 14:11
A PHP script, run from the command line, which parses cookbooks' metadata.rb files for dependencies and git clones them. Just drop it next to the cookbooks/ folder and run php git-cookbooks.php
<?php
$gitCookbookUrls = 'https://gist.githubusercontent.com/SamuelDavis/40f12c099f2c6ce19848/raw/36a3e32e4771b09f08cf5b91404ac85e68d21f5a/git-cookbook-urls.json';
$cookbookUrls = json_decode(file_get_contents($gitCookbookUrls), TRUE);
$existingCookbooks = scandir('./cookbooks');
if(!$existingCookbooks) {
die("No existing cookbooks found.\n");
}
{
"apt": "git://github.com/opscode-cookbooks/apt.git",
"ark": "git://github.com/opscode-cookbooks/ark.git",
"build-essential": "git://github.com/opscode-cookbooks/build-essential.git",
"curl": "git://github.com/phlipper/chef-curl.git",
"php": "git://github.com/opscode-cookbooks/php.git",
"xml": "git://github.com/opscode-cookbooks/xml.git",
"chef-sugar": "git://github.com/sethvargo/chef-sugar.git",
"iis": "git://github.com/opscode-cookbooks/iis.git",
"mysql": "git://github.com/opscode-cookbooks/mysql.git",
(function(){
var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
// The base Class implementation (does nothing)
Object.Class = function(){};
// Create a new Class that inherits from this class
Class.extend = function(prop) {
var _super = this.prototype;
@SamuelDavis
SamuelDavis / Arr.php
Last active February 18, 2017 09:02
Array helper class just like https://github.com/illuminate/support/blob/master/Arr.php except hilarious.
<?php
namespace Lib;
class Arr
{
public static function set(string $key, $value, array $container = []): array
{
list($keys, $valueKey) = static::breakKey($key);
$end = &static::seekEnd($keys, $container);
@SamuelDavis
SamuelDavis / clock.js
Last active November 12, 2018 02:56
A JavaScript timer which triggers a callback at a constant interval.
async function clock(actions, timeout) {
start = performance.now()
for (let i = 0; i < actions.length; i++) {
await actions[i]()
}
return setTimeout(clock.bind(clock, actions, timeout), timeout - (performance.now() - start))
}
clock(new Array(3).fill(undefined).map((_, i) => () => {
console.log(i, performance.now())
return new Promise(res => setTimeout(res, 500))
@SamuelDavis
SamuelDavis / pretty.php
Created August 16, 2017 19:23
Pretty-print something into a php 5.6-parsable array
<?php
function pretty($thing)
{
return str_replace([
'{', '}', ':',
], [
'[', ']', ' =>',
], json_encode($thing, JSON_PRETTY_PRINT));
}
@SamuelDavis
SamuelDavis / Debug.php
Last active September 3, 2017 04:58
A module of helpful methods
<?php
class Debug
{
public static $TIMESTAMPS = [];
public static function timestamp(string $key = null)
{
$key = $key ?: count(static::$TIMESTAMPS);
static::$TIMESTAMPS[$key] = microtime(true);