Skip to content

Instantly share code, notes, and snippets.

{"schemaVersion":1,"label":"coverage","message":"100%","color":"brightgreen"}
@oddvalue
oddvalue / TracksPreviousAttributes.php
Created May 16, 2022 12:37
Track previous attributes on a Laravel model
<?php
namespace App\Concerns;
use Illuminate\Database\Eloquent\Model;
trait TracksPreviousAttributes
{
protected $previous = [];
@oddvalue
oddvalue / ApiSearchQuery.php
Created May 3, 2022 14:34
Laravel rest API support for filtering and sorting as defined by https://jsonapi.org/recommendations/#filtering
<?php
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
abstract class ApiSearchQuery
{
/**
* @var string The model class for the query.
@oddvalue
oddvalue / app.js
Created February 20, 2020 11:20
Open telescope when you type "telescope"
function openInNewTab(url) {
const win = window.open(url, '_blank');
win.focus();
}
const pattern = ['t', 'e', 'l', 'e', 's', 'c', 'o', 'p', 'e'];
let current = 0;
const keyHandler = (event) => {
// If the key isn't in the pattern, or isn't the current key in the pattern, reset
@oddvalue
oddvalue / numbersToRanges.php
Last active January 17, 2019 10:04
Reduce a collection of numbers to groups of ranges
<?php
/**
* Reduce a collection of numbers to groups of ranges
*
* @example dayRange(collect([1,2,3,5,6,10])) = "1 - 3 & 5 - 6 & 10"
*
* @param Collection $days
* @return string
*/
@oddvalue
oddvalue / lipsum.njk
Last active June 11, 2021 09:16
Nunjucks lorem ipsum macro generator
{#
Lorem ipsum generator
Usage:
{{ import 'lipsum.njk' as lipsum }}
{{ lipsum.sentence() }}
#}
@oddvalue
oddvalue / window-resize.js
Created September 21, 2018 10:35
Performant window resize event listener
/**
* Performant window resize event listener
* @return {object}
*
* @usage
* import windowResize from './window-resize';
* windowResize.listen(() => {
* ...
* });
*/
@oddvalue
oddvalue / breakpoint.js
Created September 21, 2018 10:32
Get CSS breakpoints in to JS
/**
* Get current CSS breakpoint from doc body pseudo element
* Fires custom event 'breakpoint-changed' every time a new breakpoint is hit
*
* @type {Object}
* {
* current: {string} current breakpoint,
* previous: {string} previous breakpoint,
* }
*/
@oddvalue
oddvalue / debounce.js
Last active September 17, 2018 14:55
Debounce
/**
* Prevent regularly called functions (scroll/resize event listeners, etc..) from
* getting out of hand.
*
* @param {Function} callback Function to debounce
* @param {integer} wait Delay between function calls in ms
* @param {Objects} context Passed as `this` in debounced function
* @return {Function} debounced function
*
* @usage