Skip to content

Instantly share code, notes, and snippets.

@pecuchet
pecuchet / capitalize.js
Created May 4, 2021 12:19
Capitalize a string in js
export const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1);
@pecuchet
pecuchet / UniquelySuggable.php
Last active May 4, 2021 08:53
Laravel model trait to create unique slugs
<?php
namespace App\Models\Traits;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
trait UniquelySuggable
{
/**
@pecuchet
pecuchet / regex-collection.js
Created August 3, 2020 13:18
A collection of js regular expressions.
// Password validation
const length = 12,
pattern = `^(?=.*[a-zA-Z])(?=.*\\d)(?=.*[*!$€£#%&|_\\-@"'(§^¨{})\\[\\]´\`~+=:;,.?<>]).{${length},}$`,
regex = new RegExp(pattern);
@pecuchet
pecuchet / array_commas.php
Created August 3, 2020 13:13
Split a comma separated string into an array.
<?php
if (!function_exists('array_commas')) {
/**
* Split a comma separated string into an array.
* @param string $str
* @return array
*/
function array_commas(string $str): array
{
$str = explode(',', $str);
@pecuchet
pecuchet / cast_boolean.php
Last active August 3, 2020 12:38
Returns TRUE for "1", "true", "on" and "yes"; FALSE for "0", "false", "off", "no", and ""; NULL for all other values
<?php
if (!function_exists('cast_boolean')) {
/**
* Cast `'true'`, `'false'` etc. to bool.
* @param mixed $value
* @return bool
*/
function cast_boolean($value): bool
{
return filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
@pecuchet
pecuchet / checkbox-switch.css
Last active August 3, 2020 12:13
HTML input checkbox as a switch button with css variables
/*
Checkbox input as a switch
---------------------------------------------------------- */
.switch {
--width: 60px;
--height: 34px;
--gutter: 4px;
--dot-width: calc(var(--height) - (var(--gutter) * 2));
--dot-color: #fff;
<?php
error_reporting(E_ALL);
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/logs/php-error.log');
@pecuchet
pecuchet / laravel-guzzle-response-cache.php
Last active November 24, 2021 11:46
Simple Laravel 5.5 + Guzzle 6 cache middleware. This is now a composer package: https://github.com/brightfish-be/caching-guzzle.
<?php namespace App\Clients\Middleware;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Promise\FulfilledPromise;
use Illuminate\Contracts\Cache\Repository;
/**
@pecuchet
pecuchet / wp_terms_checklist_args_hierarchical.php
Last active November 19, 2017 09:20
WordPress admin: prevent hierarchical categories/terms lists to be flattened in their meta box
<?php
add_filter( 'wp_terms_checklist_args', function ( $args ) {
$args['checked_ontop'] = false;
return $args;
} );