Skip to content

Instantly share code, notes, and snippets.

qntm qntm

Block or report user

Report or block qntm

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@qntm
qntm / trend.js
Created May 8, 2020
Parsing highly ambiguous Twitter trend strings
View trend.js
const { fixed, or, plus, seq, unicode, wplus, Parser } = require('green-parse')
const trend = plus(unicode).map(chars => chars.join(''))
const comma = fixed(', ')
const and = fixed(' and ')
// match e.g. "trend1, trend2, trend3" and return ["trend1", "trend2", "trend3"]
const trends = wplus(trend, comma)
// match e.g. "trend1, trend2 and trend3" and return ["trend1", "trend2", "trend3"]
@qntm
qntm / defeat-appscan-heuristics.js
Last active Mar 28, 2020
Defeating AppScan false positives in JavaScript
View defeat-appscan-heuristics.js
// AppScan may flag up a call like this as "Insecure Use of Document.Write"
// because it isn't intelligent enough to know that this a call to a totally
// unrelated method also named `write`
const diffBuffer = PNG.sync.write(diffPng)
// Replace with:
// eslint-disable-next-line no-useless-call
const diffBuffer = PNG.sync.write.call(PNG.sync, diffPng)
///////////////////////////////
@qntm
qntm / coverage.js
Last active Dec 21, 2019
Some gross techniques for cheating on code coverage metrics
View coverage.js
// Suppose one branch of this ternary isn't covered?
const x = useT ? 'T' : ' '
// Try refactoring to one of these:
const x = [' ', 'T'][+useT]
const x = { true: 'T', false: ' ' }[useT]
// Both expressions are always evaluated, so I hope they aren't
@qntm
qntm / everyfloat.js
Last active Feb 6, 2020
Get in, loser, we're iterating over every 64-bit float
View everyfloat.js
const nextafter = require('nextafter')
for (let i = -Infinity; i !== Infinity; i = nextafter(i, Infinity)) {
console.log(i)
}
// Whoops, forgot a few the first time I ran this
console.log(Infinity)
console.log(NaN)
@qntm
qntm / high-water-mark-roman.js
Last active Jan 18, 2020
What is the smallest number which, when expressed in Roman numerals, won't fit in a 280-character Tweet?
View high-water-mark-roman.js
// <https://twitter.com/Revolvolutionry/status/1165616879009816576>
// <https://oeis.org/A036746>
const { arabicToRoman, romanToArabic } = require('big-roman')
// The entry at index `i` in this array is the smallest Roman numeral
// of length `i`
const results = ['']
const stopAt = 281
let powerOfTen = 0
@qntm
qntm / roman.js
Last active Jun 17, 2019
An extremely spare Roman numerals converter
View roman.js
// No bounds/type checking
// No support for the overbar extension for numbers past 3999
const banks = [
['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'],
['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'],
['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'],
['', 'M', 'MM', 'MMM']
]
@qntm
qntm / quine.js
Last active Jul 5, 2019
There are others like it, but this one is quine
View quine.js
(s => console.log(s + '\n(' + JSON.stringify(s) + ')'))
("(s => console.log(s + '\\n(' + JSON.stringify(s) + ')'))")
@qntm
qntm / index.js
Last active Sep 12, 2019
Promises/A+ implementation in 300 bytes
View index.js
let P=exports.P=(t,e,r=t=>typeof t==typeof P,c,o=t=>c=t,h=(t,e)=>{try{e=t(e),l==e?c():e===Object(e)&&r(t=e.then)?t.call(e,t=>e&&h(e=>t,e=0),t=>e&&o([t],e=0)):o([e,1])}catch(t){o([t])}},l={r:(c,P)=>setTimeout(l=>r(P=c[1]?t:e)?h(P,c[0]):o(c)),then:(t,e,r=P(t,e),h=o)=>(c?r.r(c):o=t=>r.r(t,h(t)),r)})=>l
@qntm
qntm / doubleEquals.js
Last active Nov 18, 2017
An implementation of the JavaScript `==` operator without using `==` itself.
View doubleEquals.js
'use strict'
// <http://www.ecma-international.org/ecma-262/5.1/#sec-8> Type
var type = function (x) {
// The `typeof` operator gives us almost what we want
return (
typeof x === 'function' ? 'object'
: x === null ? 'null'
: typeof x
)
@qntm
qntm / variadic.js
Last active Jun 5, 2017
Variadic fixed point combinators? They're more likely than you think
View variadic.js
/**
Analogous to Array.prototype.map but for objects. If you
REALLY don't want to modify Object.prototype you can
modify this into a regular function `objectMap(obj, f)` I GUESS
but it doesn't affect the basic idea of what happens below
*/
Object.prototype.map = function(f) {
const mapped = {};
Object.keys(this).forEach(key => {
mapped[key] = f(this[key], key, this);
You can’t perform that action at this time.