Skip to content

Instantly share code, notes, and snippets.

@Phrogz
Phrogz / SVG Path to Polygon.js
Created February 27, 2011 04:28
Convert a SVG path to a Polygon by sampling the path, but also including all control points in the result. See http://phrogz.net/SVG/convert_path_to_polygon.xhtml
// http://phrogz.net/SVG/convert_path_to_polygon.xhtml
function pathToPolygon(path,samples){
if (!samples) samples = 0;
var doc = path.ownerDocument;
var poly = doc.createElementNS('http://www.w3.org/2000/svg','polygon');
// Put all path segments in a queue
for (var segs=[],s=path.pathSegList,i=s.numberOfItems-1;i>=0;--i) segs[i] = s.getItem(i);
var segments = segs.concat();
# Benchmarking answers to http://stackoverflow.com/questions/9333952/in-ruby-how-to-make-the-string-include-method-ignore-case/9334066
# http://www.gutenberg.org/cache/epub/1661/pg1661.txt
strings = IO.read('sherlock.txt').scan(/\w+/) # 109,222 words
known = 500.times.map do
strings.sample.downcase.chars.map{ |c| rand<0.5 ? c.upcase : c }.join
end
words = known.flat_map{ |s| [s, s+"-"] } # \w does not include -
@Phrogz
Phrogz / test.js
Created October 22, 2019 02:35
Rounding to varying precision
n = [
[1,'1'],
[6,'6'],
[9,'9'],
[10,'10'],
[11,'11'],
[19,'19'],
[90,'90'],
[99,'99'],
[100,'100'],
@Phrogz
Phrogz / webserver.js
Last active October 15, 2019 02:21
Delayed Node.js response
// Storing response objects for delayed responses
const responseByMessage = {}
// A forked process that does some necessary heavy lifting
const worker = require('child_process').fork(
require('path').resolve('worker.js'), [],
{stdio:['inherit', 'inherit', 'inherit', 'ipc']}
)
require('http').createServer(function (req, res) {
@Phrogz
Phrogz / gist:1290420
Created October 16, 2011 01:59
Minecraft service for Ubuntu (/etc/init.d/minecraft)
#!/bin/bash
# /etc/init.d/minecraft
# version 0.3.4 2011-06-12 (YYYY-MM-DD)
### BEGIN INIT INFO
# Provides: minecraft
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
@Phrogz
Phrogz / test.js
Created November 19, 2018 21:31
Serializing a DOM with an attribute created in an existing namespace
doc = (new DOMParser).parseFromString('<r xmlns="uri1" xmlns:xx="uri2"><b/></r>', 'text/xml');
doc.querySelector('b').setAttributeNS('uri2', 'foo', 'bar');
(new XMLSerializer).serializeToString(doc);
// "<r xmlns="uri1" xmlns:xx="uri2"><b xmlns:ns14641696="uri2" ns14641696:foo="bar"/></r>"
doc = (new DOMParser).parseFromString('<r xmlns="uri1" xmlns:xx="uri2"><b/></r>', 'text/xml');
doc.querySelector('b').setAttributeNS('uri2', 'xx:foo', 'bar');
(new XMLSerializer).serializeToString(doc);
// "<r xmlns="uri1" xmlns:xx="uri2"><b xx:foo="bar"/></r>"
@Phrogz
Phrogz / ƒ.js
Last active November 13, 2018 14:30
A useful JS function for running maps on objects; similar to Ruby's Symbol#to_proc &:foo syntax, but more powerful.
function ƒ(name){
let v,params=Array.prototype.slice.call(arguments,1);
return o=>(typeof (v=o[name])==='function' ? v.apply(o,params) : v);
}
a = ["foo", "barmitzvah"]
a.map(ƒ('length')) // [3, 10]
a.map(ƒ('toUpperCase')) // ["FOO", "BARMITZVAH"]
a.map(ƒ('slice',1)) // ["oo", "armitzvah"]
@Phrogz
Phrogz / example.js
Created November 11, 2018 20:31
Showcasing some of NeatSerializer's features
let xml =
`<r a="42" xmlns:blah="MYURI">
<e> <!-- my house --> </e>
<f>9&lt;42</f>
<blah:dumb>go away</blah:dumb>
<g><![CDATA[5<7]]></g>
</r>`;
let doc = (new DOMParser).parseFromString(xml, 'application/xml');
console.log(NeatSerializer(doc));
@Phrogz
Phrogz / intercept.js
Created November 10, 2018 02:17
Intercepting JavaScript prototypal inheritance
let p = console.log;
let Interceptor = { hi:_=>"I am "+this.name }
function Person(name) {
this.name = name;
}
Person.prototype.hi = function() {
return "Hello, I'm "+this.name
}
@Phrogz
Phrogz / funcenv.lua
Last active November 5, 2018 16:48
Evaluating the same expression in multiple environments, dynamically
-- An expression to evaluate
local expr = 'a + (b or 0)'
-- Set up two different environments we want our expression to be evaluated against
local env1,env2 = {a=17}, {a=1,b=25}
-- Create a wrapper environment with a metatable
local envmeta = {}
local envwrap = setmetatable({},envmeta)