Skip to content

Instantly share code, notes, and snippets.

@Phrogz
Phrogz / test.js
Created Oct 22, 2019
Rounding to varying precision
View test.js
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 Oct 15, 2019
Delayed Node.js response
View webserver.js
// 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 Nov 19, 2018
Serializing a DOM with an attribute created in an existing namespace
View test.js
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 Nov 13, 2018
A useful JS function for running maps on objects; similar to Ruby's Symbol#to_proc &:foo syntax, but more powerful.
View ƒ.js
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 Nov 11, 2018
Showcasing some of NeatSerializer's features
View example.js
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 Nov 10, 2018
Intercepting JavaScript prototypal inheritance
View intercept.js
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 Nov 5, 2018
Evaluating the same expression in multiple environments, dynamically
View funcenv.lua
-- 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 Oct 31, 2018
Overriding Lua libraries with stub versions, that have access to the originals
View app.lua
-- 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'
View PishTemplate.rb
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
View SimpleParse.rb
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
You can’t perform that action at this time.