Skip to content

Instantly share code, notes, and snippets.

@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 / 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)
@Phrogz
Phrogz / app.lua
Last active October 31, 2018 18:59
Overriding Lua libraries with stub versions, that have access to the originals
-- add my custom lib directory to the path
package.path = 'lib/?.lua;'..package.path
-- add this line only to switch to using the wrappers;
-- do this after making any other path adjustments.
package.real,package.path = package.path,'debug/?.lua;'..package.path
local mylib = require 'mylib'
require 'parslet'
class PishTemplate < Parslet::Parser
attr_accessor :vars
# `vars` is a hash of symbol-to-values for referencing in templates.
# vars can also be safely set using the #assign-values method.
def initialize(vars={})
super()
@vars = vars
class SafeTemplate < Parslet::Parser
# ...
rule(:cond) do
test.as(:test) >> markup.as(:out)
>> (elif.as(:test) >> markup.as(:out)).repeat.as(:elifs)
>> (ells >> markup.as(:elseout)).maybe
>> stop
end
# ...
end