Skip to content

Instantly share code, notes, and snippets.

@ninjascribble
ninjascribble / node-user-agent.js
Last active September 30, 2024 22:28
Fetching the user-agent string from a request using either NodeJS or NodeJS + Express
/** Native NodeJS */
var http = require('http')
, server = http.createServer(function(req) {
console.log(req.headers['user-agent']);
});
server.listen(3000, 'localhost');
/** NodeJS with Express */
var express = require('express')
@ninjascribble
ninjascribble / svg-to-xml.js
Created February 26, 2013 22:37
Saving dynamically created SVG in a WinJS app via the Windows.Data.Xml.Dom API
Windows.Storage.ApplicationData.current.localFolder.createFileAsync("example.svg", Windows.Storage.CreationCollisionOption.replaceExisting)
.then(function(storageFile) {
var serializer = new XMLSerializer()
, xmldoc = new Windows.Data.Xml.Dom.XmlDocument()
, xmlLoadSettings = new Windows.Data.Xml.Dom.XmlLoadSettings()
, svgnode = document.querySelector('.container > svg')
, svgstr = serializer.serializeToString(svgnode)
xmlLoadSettings.prohibitDtd = false;
/**
* Replace any ocurrences of {{dimsum}} with generated text.
*/
dimsum.parse = function(root) {
var reg = /{{dimsum[:]?([pst0-9]*)}}/ig,
root = root || document.getElementsByTagName('body')[0],
command = '';
// If root is just a string, then return a string with replacement
@ninjascribble
ninjascribble / find-palindromes.js
Last active December 12, 2015 05:09
Quick 'n dirty interview question #2
/**
* Given a list of words, write a function that returns only those
* words which are palindromes (written the same forwards and
* backwards).
*
* Ex.
*
* var list = ['racecar', 'balloon', 'moon', 'history'];
* findPalindromes(list) == ['racecar', 'moon'];
*/
@ninjascribble
ninjascribble / flatten-array.js
Last active September 23, 2018 15:32
Quick 'n dirty JavaScript interview question #1
/**
* Given an array of n length, where each item in the array may be a letter,
* number or another array, write a function that will return a flattened
* array containing all the values in the original array and its children.
*
* Ex.
*
* var arr = [ 1, 'b', [ 'c', [ 4 ], 5], 'f'];
* flatten(arr) === [1, 'b', 'c', 4, 5, 'f'];
*/
@ninjascribble
ninjascribble / d3-linear-transform.js
Last active December 12, 2015 01:18
Smooth linear transforms with d3. With much thanks to Mike Bostock: http://bost.ocks.org/mike/path/
;(function() {
'use strict';
var _stage = d3.select('body').append('svg').attr('height', 200)
, _line = _stage.append('path')
, _interval = 400
, _filter = 'hourly'
, _cache = []
, _needsTranslation = false;
@ninjascribble
ninjascribble / gist:4602255
Last active December 11, 2015 12:38
jQuery $.Deferred chaining example. jsfiddle: http://jsfiddle.net/B5eZ9/1/
var a = getPromise(50)
, b = getPromise.bind(document, 1200)
, c = getPromise.bind(document, 400);
a.then(b).then(c).done(finish);
function getPromise(ttl) {
var deferred = $.Deferred();
@ninjascribble
ninjascribble / index.html
Created November 24, 2012 05:05
A CodePen by Scott Grogan. CSS Chronograph - Experimenting with ways to visualize time via CSS
<div class="timer-group">
<div class="timer hour">
<div class="hand"><span></span></div>
<div class="hand"><span></span></div>
</div>
<div class="timer minute">
<div class="hand"><span></span></div>
<div class="hand"><span></span></div>
</div>
<div class="timer second">
@ninjascribble
ninjascribble / dabblet.css
Created October 18, 2012 04:13
CSS Play/Pause button
/* CSS Play/Pause button */
button.playpause {
background: none;
border: 3px solid #ccc;
border-radius: 50%;
cursor: pointer;
display: inline-block;
height: 39px;
overflow: hidden;
position: relative;
button.playpause {
background: none;
border: 3px solid #ccc;
border-radius: 50%;
content: "<span></span>";
display: inline-block;
height: 40px;
overflow: hidden;
position: relative;
text-indent: -2000px;