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 / 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 / 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 ) {
@rolfen
rolfen / html.js
Created August 20, 2017 12:13
Lean function to generate html from JavaScript and do some of the nice things that React allows.
/**
* Generates html element based on a d-structure
* d-structure is:
* [ string tagname | HtmlElement root_element,
* object attributes,
* [child] | string text,
* funcion callback(el)
* ]
* child is:
* d-structure | string text | HtmlElement element
@rolfen
rolfen / ajaxListener.js
Last active October 10, 2017 15:15 — forked from icodejs/ajaxListener.js
JS: Listen to ajax calls from Javascript
// override XMLHttpRequest
// source: https://gist.github.com/icodejs/3183154
var open = window.XMLHttpRequest.prototype.open,
send = window.XMLHttpRequest.prototype.send;
function openReplacement(method, url, async, user, password) {
return open.apply(this, arguments);
}
@rolfen
rolfen / css.whatswrong.txt
Last active October 31, 2017 09:37
What's wrong with CSS
- Side effects (usually unwanted)
- Sometimes impractical: eg: vertical-align
- Sometimes too high level / opinionated and inflexible (related to all of above)
- Cascading - most of the inhertiance is unwanted
- Global-variable-like issues
- Overriding
__Improvements
I think it would be an important improvement, to be able to compound CSS blocks. A bit like less mixins, but at a native level. At the moment, if we are not using a pre-processor, we have to add multiple classes on elements (eg: <div class="col-2 pad-1 size-3">). That is neither powerful nor semantic.