Skip to content

Instantly share code, notes, and snippets.

View branneman's full-sized avatar

Bran van der Meer branneman

View GitHub Profile
@branneman
branneman / 0-usage.js
Last active January 25, 2016 18:40
JavaScript Mixins
/**
* Original class
*/
class Todo {
constructor(name) {
this.name = name || 'Untitled';
this.done = false;
}
do() {
this.done = true;
// (c) copyright unscriptable.com / John Hann
// License MIT
// For more robust promises, see https://github.com/briancavalier/when.js.
function Promise () {
this._thens = [];
}
Promise.prototype = {
@branneman
branneman / matchAll.js
Last active July 17, 2019 11:43
JavaScript RegExp: Get all matches and capture groups
'use strict'
const { isNull, isStr, isRegExp } = require('./types')
module.exports = matchAll
/**
* RegExp: Get all matches and capturing groups
* @param {RegExp} re
* @param {String} str
@branneman
branneman / getFormElementValue.js
Last active April 22, 2016 08:36
getFormElementValue() - Returns the value of a form element, given it's name and type
/**
* getFormElementValue()
* Returns the value of a form element, given it's name and type
*
* @param {String} name - An element's `name=""` attribute
* @param {String} type - An element type string, e.g. 'text', 'radio', 'select'
* @returns {String|Boolean}
*/
const getFormElementValue = (function(){
const getElement = name => document.querySelector('[name="' + name + '"]');
@branneman
branneman / invoker.js
Last active June 17, 2016 13:50
Function Invoker - Repeatedly invokes a function until it returns a truthy value
/**
* Invoker
* Repeatedly invokes a function until it returns a truthy value
*
* @see https://gist.github.com/branneman/53b820be519b54bfc30a
* @param {Number} limit - The amount of total calls before we timeout
* @param {Number} interval - The amount of milliseconds between calls
* @param {Function} fn - The function to execute, must return a truthy value to indicate it's finished
* @param {Function} cb - The callback for when we're finished. Recieves 2 arguments: `error` and `result`
*/
/**
* @param {HTMLElement} element - The element's coordinates to calulate
* @param {HTMLElement} relativeElement - An optional relative element, defaults to `document.body`
* @returns {{ offsetY: Number, offsetX: Number }}
*/
const getElementCoordinates = function(element, relativeElement = document.body) {
const relativeRect = relativeElement.getBoundingClientRect();
const elementRect = element.getBoundingClientRect();
const offsetY = elementRect.top - relativeRect.top;
const offsetX = elementRect.left - relativeRect.left;
@branneman
branneman / task-runner.js
Created April 21, 2016 08:32
Streaming task-runner (how gulp works, simplified)
'use strict';
const fs = require('fs');
const glob = require('glob').sync;
const mkdirp = require('mkdirp').sync;
const sass = require('node-sass');
const path = require('path');
const streams = require('stream');
const runner = {};
@branneman
branneman / index.html
Created May 3, 2016 08:39
Asynchronously load fonts and cache them into localStorage
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Asynchronously load fonts and cache them into localStorage</title>
<script>
(function(d, w){
@branneman
branneman / photoshop-script-save-extra-png-on-save.jsx
Last active December 30, 2021 16:22
Photoshop Script: Save an extra png on Document Save
main();
function main(){
var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
var Ext = decodeURI(app.activeDocument.name).replace(/^.*\./,'');
if(Ext.toLowerCase() != 'psd') return;
var Path = app.activeDocument.path;
var saveFile = File(Path + "/" + Name +".png");
if(saveFile.exists) saveFile.remove();
SavePNG(saveFile);
@branneman
branneman / count-lint-errors.js
Last active July 31, 2023 17:59
Group-By-Count ESLint errors
// :: (String, String) => String
const spawn = require('child_process').spawnSync;
// :: String => [String]
const getRules = raw => raw
.split('\n')
.map(line => line.trim())
.filter(line => !!line)
.filter(line => line[0] !== '/' && line[0] !== '✖')
.map(line => line.match(/[a-z-]+$/)[0]);