Skip to content

Instantly share code, notes, and snippets.

View mogelbrod's full-sized avatar

Victor Hallberg mogelbrod

View GitHub Profile
@mogelbrod
mogelbrod / gist:4266297
Created December 12, 2012 09:13
Vim fold function which does indentation based folding.
function! IndentationFoldExpr(ln) " {{{
let line = getline(a:ln)
if line =~ '^\s*$'
return '-1' "'='
end
let ind = indent(a:ln) / &sw
let ind_next = indent(nextnonblank(a:ln+1)) / &sw
var data = {
id: trackID(track),
uri: track, // spotify:track:...
artists: [
{ name: 'Unknown', uri: 'spotify:artist:518rTAIFPwQjLUSi4Pdzzn' }
],
title: track, // track name (title)
requests: 1, // number of requests
requesters: [ // length == requests
{ user: 'Anonymous', url: 'http://', time: new Date() }
gbump () {
what=${1:-patch}
echo "Bumping $what version and pushing to origin"
changed=$(git diff-index --name-only HEAD --)
[ -n "$changed" ] && git stash
git pull --rebase && npm version $what && git push && git push --tags
[ -n "$changed" ] && git stash pop
}
@mogelbrod
mogelbrod / path-to-ref.coffee
Last active August 29, 2015 14:01
pathToRef implementation in various JavaScript dialects
###*
* Helper function which returns a reference to a value in an object hierarchy.
* Call with path to parent object if you wish to mutate a value in-place, as
* assignment on reference variables will only set the variable in the local scope.
*
* AngularJS: Available in 'app' module as 'pathToRef' constant.
*
* @param path {Array | String} Path components
* @param root {Object | Array} Root object to start at
* @param initIntermediary {Boolean} Initialize and continue on non-existant values (false = bail)
@mogelbrod
mogelbrod / index.js
Last active March 17, 2019 17:16
Simple apply-loader module for webpack, published @ https://www.npmjs.com/package/apply-loader
var loaderUtils = require('loader-utils');
module.exports = function(source) {
this.cacheable && this.cacheable();
var query = loaderUtils.parseQuery(this.query);
var args = [];
// apply?config=key => sourceFn(require('webpack.config').key)
if (typeof query.config === 'string') {
if (!query.config in this.options)
NAME = main
_DEPS =
_OBJ = $(NAME).o
SDIR = .
ODIR = .
CC = gcc
CFLAGS = -std=c99 -O2 -W -g -I.
@mogelbrod
mogelbrod / weighted-choice.js
Created February 16, 2016 09:39
Weighted random choice in JavaScript, takes an array of non-negative weights
function weightedChoice(weights) {
const weightSum = weights.reduce((sum, w) => sum + w)
let choice = Math.floor(Math.random() * weightSum) + 1
let idx = weights.length - 1
while ((choice -= weights[idx]) > 0) {
idx -= 1
}
return idx
}
@mogelbrod
mogelbrod / relative-path.php
Last active May 19, 2016 13:18
Generate a relative URL from path A to B
<?php
function relativePath($from, $to) {
$fromAry = explode('/', $from);
$toAry = explode('/', $to);
while (count($fromAry) && count($toAry) && $fromAry[0] === $toAry[0]) {
array_shift($fromAry);
array_shift($toAry);
}
$up = count($fromAry);
@mogelbrod
mogelbrod / redirects.php
Last active September 28, 2017 09:32
Simple Kirby redirects plugin (compatible with https://github.com/ivinteractive/kirbycms-redirects)
<?php
// src/plugins/redirects.php
function htmlRedirect($to) {
$to = esc($to);
echo <<<EOF
<html>
<head>
<meta http-equiv="Refresh" content="0; url=$to" />
</head>
@mogelbrod
mogelbrod / caesar-shift.js
Created October 4, 2016 19:56
Caesar cipher in JavaScript (ES6)
/**
* Applies a Caesar shift to all alphabetical characters in a string.
* Characters are converted to uppercase in the returned string.
* @param {int} key - Positions to shift (0-25).
* @param {string} str - The string to shift.
* @return {string} The shifted string.
*/
function caesarShift(str, key) {
return str.toUpperCase().replace(/[A-Z]/g, c => String.fromCharCode((c.charCodeAt(0)-65+key)%26+65))
}