Skip to content

Instantly share code, notes, and snippets.

@nfeldman
nfeldman / gist:1432811
Created December 5, 2011 08:09
JS XMLSS Builder
// too basic atm to be worth setting up a proper repo for, but I think it has potential, assuming it isn't totally wrong
// ... I know I could have used actual xml, but this was more amusing
var hasOwn = Object.prototype.hasOwnProperty,
toString = Object.prototype.toString;
// helpers from Fortinbras library
function ucfirst (str) {
@nfeldman
nfeldman / gist:1432857
Created December 5, 2011 08:29
Using PHP Excel to perform a simple JSON to XSL transformation
<?php
error_reporting(E_ALL);
require realpath(dirname(__FILE__)) . '/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
// DEMO ONLY (potentially unsafe)
$data = json_decode($_POST['json']);
$key = $_POST['key'];
@nfeldman
nfeldman / gist:1868037
Created February 20, 2012 05:53
mimetypes list
// from node-paperboy
{
"aiff": "audio/x-aiff",
"arj": "application/x-arj-compressed",
"asf": "video/x-ms-asf",
"asx": "video/x-ms-asx",
"au": "audio/ulaw",
"avi": "video/x-msvideo",
"bcpio": "application/x-bcpio",
"ccad": "application/clariscad",
@nfeldman
nfeldman / gist:2904580
Created June 10, 2012 08:59
break text into lines of length n
// a bit messy and not optimized, but handy for basic formatting of short blocks of text
function splitToLines (string, leftMarginWidth, maxLineWidth) {
var lines = [],
line = [],
padding = Array(leftMarginWidth).join(' '),
i = 0, // cursor
p = 0, // left anchor
m = 0, // counter for searching 5 char wrap zone
s, // tmpvar
@nfeldman
nfeldman / gist:2934140
Created June 15, 2012 01:39
recursive readdir for node (via stackoverflow)
// http://stackoverflow.com/q/5827612/
function walk(dir, done) {
var results = [];
fs.readdir(dir, function (err, list) {
if (err) {
return done(err);
}
var i = 0;
(function next() {
var file = list[i++];
@nfeldman
nfeldman / gist:2949291
Created June 18, 2012 16:35
constructor for building xml like strings
// extracted from an earlier gist that had a more comprehensive DOM-like implementation
// in this case, I didn't care about namespaces and I didn't require any operation but
// append. This is too basic to be useful for most things, but good enough for what I've
// used it for. Note that the use of ES5 accessors makes this incompatible with IE < 9.
function Tag (name) {
this.name = name;
this.attrs = Object.create(null);
this.children = [];
return this;
@nfeldman
nfeldman / dictionary.js
Created July 7, 2012 23:27
one way to do a dictionary in ES5
// as a nodejs module
module.exports = makeDictionary();
function makeDictionary (dict) {
return function Dictionary () {
!dict && (dict = Object.create(null));
var Dictionary = {
__proto__: null,
get: function (name) {
@nfeldman
nfeldman / gist:10614719
Created April 14, 2014 03:55
an updated html5 template with no old IE support
<!doctype html>
<html lang="en">
<head>
<!-- warning: template assumes modern browser features -->
<title>UNTITLED</title>
<meta charset="utf-8">
<meta name="author" content="AUTHOR NAME">
@nfeldman
nfeldman / firebug-xpath.js
Last active May 11, 2017 16:51
Standalone version of XPath Module from Firebug, assumes strings have a trim method
// AMD, Node flavored CommonJS, and old fashion global
(function (global, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
@nfeldman
nfeldman / find.js
Last active August 29, 2015 14:01
starting point for regexp based page search
// this is just the starting point. you can run it in the console.
// there are numerous problems to solve, like working out whether
// the text it finds is visible or not -- i.e. if a node has display: none,
// or is absolutely positioned offscreen or behind another element, or
// has visiblity: hidden, or has height: 0 and overflow: hidden, and so on.
function find (str, caseSensitive) {
var head = (document.head || document.getElementsByTagName('head')[0]),
body = (document.body || document.getElementsByTagName('body')[0]),
re = RegExp(str, 'g' + (caseSensitive ? '' : 'i')),