Skip to content

Instantly share code, notes, and snippets.

@hashchange
hashchange / precompiled.declarative.handlebars.templates.js
Last active February 11, 2024 22:37
Precompiled.Declarative.Handlebars.Templates – a plug-in for Backbone.Declarative.Views, enabling the use of precompiled Handlebars templates.
/**
* Precompiled.Declarative.Handlebars.Templates
*
* Plug-in for Backbone.Declarative.Views, enabling the use of precompiled Handlebars templates.
*
* For defining `el`-related properties along with a precompiled template, modify the template source. Add a special
* comment to the source, just as you would in a template string. An example:
*
* <!-- data-tag-name="ul" data-class-name="list" -->
*
@hashchange
hashchange / export-custom-packages
Created November 30, 2021 13:51
Creates and maintains a list of all manually-installed packages in a Ubuntu distro.
#!/usr/bin/env bash
# Script name
PROGNAME=$(basename "$0")
if [[ "$1" == '--help' || "$1" == '-h' ]]; then
fmt -s <<- HELP_TEXT
Writes and updates a list of all manually-installed packages in a Ubuntu distro.
@hashchange
hashchange / wsl-linux-path
Last active August 11, 2022 17:51
Safely converts a Windows path to a WSL (Linux) path. Does not suffer from the limitations of `wslpath -u`.
#!/usr/bin/env bash
# Script name
PROGNAME="$(basename "$BASH_SOURCE")"
if [[ "$1" == '--version' || "$1" == '-v' ]]; then
fmt -s <<- VERSION_TEXT
$PROGNAME 1.0.1
(c) 2022 Michael Heim
License: MIT
@hashchange
hashchange / wsl-windows-path
Last active May 10, 2022 23:30
Safely converts a WSL (Linux) path to a Windows path. Does not suffer from the limitations of `wslpath -w`.
#!/usr/bin/env bash
# Script name
PROGNAME="$(basename "$BASH_SOURCE")"
if [[ "$1" == '--version' || "$1" == '-v' ]]; then
fmt -s <<- VERSION_TEXT
$PROGNAME 1.1.2
(c) 2022 Michael Heim
License: MIT
@hashchange
hashchange / README.md
Created March 22, 2018 21:42 — forked from barneycarroll/README.md
Lock and unlock a page's scroll position.

jquery.scrollLock.js

Useful for when a blocking user experience is needed (in my case, didn't want people unwittingly loosing their place by scrolling while a modal required their attention): $.scrollLock() locks the body in place, preventing scroll until it is unlocked.

// Locks the page if it's currently unlocked
$.scrollLock();

// ...or vice versa
@hashchange
hashchange / _map-sort.scss
Created March 16, 2018 11:35 — forked from Jakobud/_map-sort.scss
Sort a SASS map
/// map-sort
/// Sort map by keys
/// @param $map - A SASS map
/// @returns A SASS map sorted by keys
/// @requires function list-sort
/// @author Jake Wilson <jake.e.wilson@gmail.com>
@function map-sort($map) {
$keys: list-sort(map-keys($map));
$sortedMap: ();
@each $key in $keys {
@hashchange
hashchange / scrollTargetPosition.js
Created August 17, 2015 14:16
Returns the scroll target position for scrolling to the top of an element.
/**
* Returns the scroll target position for scrolling to the top of an element.
*
* Pass the result to a scrollTo method:
*
* - $scrollContainer.scrollTop( result ) for instant scrolling
* - $scrollContainer.scrollTo( result ) for animated scrolling with jQuery.scrollable
*
* @param {jQuery} $target the target element
* @param {jQuery} $scrollContainer either $(window), or a scrollable HTML element
@hashchange
hashchange / rounding.js
Created July 26, 2015 19:53
Floating-point versions of Math.round, Math.floor, Math.ceil
/**
* Adjusts a number to a given precision, working around the buggy floating-point math of Javascript. Works for
* round, floor, ceil operations.
*
* Lifted from the Math.round entry of MDN. Minor changes without effect on the algorithm.
*
* @param {string} operation "round", "floor", "ceil"
* @param {number} value
* @param {number} [precision=0] can be negative: round( 104,-1 ) => 100
* @returns {number}
@hashchange
hashchange / custom_error.js
Last active July 4, 2016 15:30
Patterns for creating custom error types.
// Pattern for creating a custom error type.
//
// Based on Zakas, Professional Javascript for Web Developers, 3rd Ed., p. 619,
// See also http://stackoverflow.com/a/5251506/508355, including comments.
function MyCustomError ( message ) {
this.message = message;
if ( Error.captureStackTrace ) {
Error.captureStackTrace( this, this.constructor );
@hashchange
hashchange / debounce.js
Last active June 28, 2016 15:31
Stand-alone, straightforward debounce function. Executes the callback at the end (no option for executing at start).
function debounce( callback, delay, context ) {
var timer;
return function () {
var _this = context || this,
args = arguments;
// Clear a timer which has been created earlier in the sequence of calls.
timer && clearTimeout( timer );