Skip to content

Instantly share code, notes, and snippets.

View zanbaldwin's full-sized avatar
🦀

Zan Baldwin zanbaldwin

🦀
View GitHub Profile
<?php
function camelCaseToSnakeCase(string $input): string {
preg_match_all('!(^[a-z\d]+)|(?:_?)([a-z\d]+|[A-Z][a-z\d]+|[A-Z][A-Z\d]*(?=_|$|[A-Z][a-z\d]))!', $input, $matches);
return strtolower(implode('_', array_map(function (string $part): string {
return trim($part, '_');
}, $matches[0])));
}
@zanbaldwin
zanbaldwin / Makefile
Last active June 8, 2023 10:24
Automatic Makefile Usage Generator
SHELL := bash
.SHELLFLAGS := -eu -o pipefail -c
.ONESHELL:
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules
ifeq ($(origin .RECIPEPREFIX), undefined)
$(error This Make does not support .RECIPEPREFIX; Please use GNU Make 4.0 or later)
endif
.RECIPEPREFIX = >
@zanbaldwin
zanbaldwin / string-regex.md
Last active May 24, 2019 00:49
Match strings, allowing for escape characters.

Regular expression to match either a single or double quoted string, taking into account escape characters: /(["'])((?:(?!\1|\\).|\\.)*?)\1/

It's recommended that you remove escape characters from the resulting string (replace \\((?=\%s|\\).) with \1 substituting %s for the matched quote character):

preg_match('/(["\'])((?:(?!\\1|\\\\).|\\\\.)*?)\\1/', $string, $matches);
$extractedStringValue = preg_replace(sprintf('/\\\\((?=\\\\|\\%s).)/', $matches[1]), '\1', $matches[2]);
@zanbaldwin
zanbaldwin / konami.js
Created May 21, 2019 18:08
Konami Code in Javascript/TypeScript
function konami(callback) {
var codes = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65],
position = 0;
document.addEventListener('keydown', function (event) {
if (event.keyCode === codes[position]) {
position++;
if (position === codes.length) {
position = 0;
callback();
}
@zanbaldwin
zanbaldwin / Dockerfile
Last active December 20, 2019 19:50
Compressed Docker Image (~14MiB) for Hugo Site Builder (v0.50)
FROM "golang:1.13.5-alpine3.10" AS builder
ARG HUGO_VERSION="v0.61.0"
RUN apk add --no-cache --virtual .build-deps gcc g++ git musl-dev upx \
&& git clone git://github.com/gohugoio/hugo.git \
&& cd hugo \
&& git checkout "${HUGO_VERSION}" \
&& go build -o "/tmp/hugo" -ldflags "-s -w" -tags "extended" \
&& upx --brute -o "/sbin/hugo" "/tmp/hugo" \
&& apk del .build-deps
@zanbaldwin
zanbaldwin / StreamMode.php
Last active October 1, 2018 13:58
Figuring out file-descriptors in PHP console applications
<?php declare(strict_types=1);
namespace Darsyn\FD;
class StreamMode implements StreamModeInterface
{
/** @var resource $fp */
private $fp;
/** @var integer $mode */
private $mode;
<?php declare(strict_types=1);
if (!\function_exists('array_map_recursive')) {
function array_map_recursive(array $array, callable $function): array
{
$out = [];
foreach ($array as $key => $value) {
$out[$key] = \is_array($value)
? array_map_recursive($value, $function)
: \call_user_func($function, $value, $key);
<?php declare(strict_types=1);
namespace Symfony\Component\Validator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Type;
use Symfony\Component\Validator\Mapping\ClassMetadataInterface;
use Symfony\Component\Validator\Mapping\MetadataInterface;
use Symfony\Component\Validator\Validator\RecursiveValidator;
@zanbaldwin
zanbaldwin / Hierarchal CTE in SQL.md
Last active August 22, 2018 18:08
Playing around with Hierarchal Tree Structures and Common Table Expressions

Accessing Tree Hierarchies using Common Table Expressions

Examples have been tested with MySQL 8.0.12.

Table Structure

CREATE TABLE `nodes` (
  `id`      INT(11)       NOT NULL AUTO_INCREMENT,
  `parent`  INT(11)       DEFAULT NULL,

Keybase proof

I hereby claim:

  • I am zanbaldwin on github.
  • I am zanbaldwin (https://keybase.io/zanbaldwin) on keybase.
  • I have a public key whose fingerprint is F59E C254 A2DA 8BE0 C135 126A 2100 8A6E D04D 2B52

To claim this, I am signing this object: