Skip to content

Instantly share code, notes, and snippets.

@rolfen
rolfen / toArray.js
Created December 20, 2015 17:35
Most performant way to cast nodelist to array
/**
* Performant nodelist to array cast. Source: http://stackoverflow.com/a/15144269/370786
* @param nl {NodeList} NodeList to convert. Might work with other data types (Array?) but that needs further testing.
*/
function toArray(nl)
{
var arr = [];
if(isNaN(nl.length)) {
throw "Cannot get length";
} else {
@rolfen
rolfen / draggable.js
Last active December 4, 2017 09:27
Easy and lean drag-move functionality.
/**
* Minimal drag-move code.
* https://jsfiddle.net/rolfen/68bg5ggw/
*
* @param ele {HTMLElement} the container element to drag
* @param triggers {Array} One or more "drag triggers", areas (inside ele) that will trigger the drag functionality when clicked on.
* @param onDragStart {function} Called when dragging starts (trigger is grabbed)
* @param onDragEnd {function} Called when dragging ends (trigger is released)
*
* Issues:
@rolfen
rolfen / queryString.js
Last active December 20, 2015 18:44
Encode/decode URL query strings in JavaScript
'use strict';
/**
* {hello: 1, another: 2} => hello=1&another=2
*/
var encodeQueryString = function (obj)
{
return (Object.keys(obj).reduce(function(acc, k, i){
return acc + (i?'&':'') + encodeURI(k) + "=" + encodeURI(obj[k]);
}, ''));
@rolfen
rolfen / listDir.js
Created February 13, 2016 23:39
node: Gets the list of files in a directory and serves it in JSON.
var http = require('http');
var fs = require('fs');
var originalsPath = 'pics/originals';
var originalFiles = {};
fs.readdir('pics/originals',function(err, files){
originalFiles = files;
})
/**
* Parses a GET-style query string into a key/value object
* example:
* decodeQueryString("?sdf=ABC&xyz=jj%C3%BC");
* Object {sdf: "ABC", xyz: "jjü"}
*
* @param {String} s The query string
*/
function decodeQueryString(s) {
@rolfen
rolfen / index.js
Last active November 19, 2016 14:29
Quick node HTTP server
var http = require('http');
var fs = require('fs');
var localIP = "127.0.0.1";
var port = 8080;
var indexFile = './index.html';
var process = function(req, res) {
fs.readFile(indexFile, function(err, data) {
@rolfen
rolfen / gist:17bf252d9fbf6d8e0d771269d9690ca3
Created September 3, 2016 18:37
Deconstruct URL (quick regex)
window.location.href.match(/^([^/]+):\/\/([^/:]+):?([1-9]*)\/([^?]*)\??(.*)$/)
Example output:
["http://127.0.0.1:1337/projects/sd?sdflkj&jh", "http", "127.0.0.1", "1337", "projects/sd", "sdflkj&jh"]
That is: full match, protocol, host, port, path (relative to root), query string
Todo: test for bugs.

Various commands

@rolfen
rolfen / ie_hacks.css
Last active December 8, 2016 14:25
IE CSS hacks
/* Source:
http://stackoverflow.com/a/30743013/370786
*/
/***** Attribute Hacks ******/
/* IE6 */
#once { _color: blue }
/* IE6, IE7 */
@rolfen
rolfen / parseForm.js
Last active June 27, 2017 19:45
Serialize form to JSON - the simple way
// credit: https://codepen.io/gabrieleromanato/pen/LpLVeQ
function parseInputs( elements ) {
var obj = {};
for( var i = 0; i < elements.length; ++i ) {
var element = elements[i];
var name = element.name;
var value = element.value;
if( name ) {