Skip to content

Instantly share code, notes, and snippets.

🌊
Fractalicious

Thom Bakker A1rPun

🌊
Fractalicious
Block or report user

Report or block A1rPun

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
@A1rPun
A1rPun / 0-README.md
Last active May 17, 2019
A function to convert a number to a roman numeral
View 0-README.md

Roman numerals

1..toRoman() // "I"
18..toRoman() // "XVIII"
1910..toRoman() // "MCMX"
9999..toRoman() // "MMMMMMMMMCMXCIX"

Inspiration

@A1rPun
A1rPun / f.js
Last active Apr 15, 2019 — forked from renanpvaz/f.js
JavaScript function pattern matching inspired by F#'s & renanpvaz gist
View f.js
fun = (variations, matchIndex = 0) => (...appliedArgs) => (variations[appliedArgs[matchIndex]] || variations._)(...appliedArgs)
luck = fun({
7: () => "LUCKY NUMBER SEVEN",
_: () => "Sorry, you're out of luck, pal!",
})
luck(7)
// "LUCKY NUMBER SEVEN"
@A1rPun
A1rPun / blink.css
Created Jul 26, 2018
Cross-browser Blink
View blink.css
@A1rPun
A1rPun / ThueMorse.js
Last active Aug 17, 2017
Thue–Morse sequence
View ThueMorse.js
function thueMorse(i) {
var a = '0', b = '1';
for (; i--;)
a += [b, b += a][0];
return a;
}
thueMorse(0); // 0
thueMorse(1); // 01
thueMorse(4); // 0110100110010110
@A1rPun
A1rPun / deburglar.js
Last active Aug 17, 2017
Get duplicate requests in JavaScript
View deburglar.js
// deburglar.getDupes();
var deburglar = (function () {
var dict = {};
var maybeTheOriginalOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function (method, url) {
if (!dict[url])
dict[url] = 0;
dict[url]++;
return maybeTheOriginalOpen.apply(this, arguments);
};
@A1rPun
A1rPun / int32toIP.js
Last active Aug 29, 2017
int32 to IP in JavaScript
View int32toIP.js
function int32toIP(nMask) {
for (var nFlag = 0, nShifted = nMask, sMask = ''; nFlag < 32;
nFlag++, sMask += String(nShifted >>> 31), nShifted <<= 1);
return sMask.match(/.{1,8}/g).map(function (x) { return parseInt(x, 2); }).join('.');
}
int32toIP(255); // "0.0.0.255"
int32toIP(256); // "0.0.1.0"
int32toIP(1333333337); // "79.121.13.89"
int32toIP(7527203073); // "192.168.1.1"
@A1rPun
A1rPun / tests.js
Last active Aug 17, 2017
Waterfall on promise
View tests.js
waterfall(['a', 'bbbb', 'cc'], function(item){
return function(resolve){
setTimeout(function(){
console.log(item);
resolve();
}, item.length * 1000);
};
});
@A1rPun
A1rPun / getHashCode.js
Last active Aug 17, 2017
Javascript value equality
View getHashCode.js
function getHashCode(obj) {
var hashCode = 0;
if (typeof obj === 'object')
for (var prop in obj) {
var code = getHashCode(prop) + getHashCode(obj[prop]);
hashCode += code * code;
}
else
for(var str = "" + obj, i = str.length; i--;)
hashCode += 100 / (i + str.charCodeAt(i));
@A1rPun
A1rPun / Timers.js
Last active Aug 17, 2017
setTimeout setInterval timer handler. Access them via `window.timers`
View Timers.js
(function(w){
var oldST = w.setTimeout;
var oldSI = w.setInterval;
var oldCI = w.clearInterval;
var timers = [];
w.timers = timers;
w.setTimeout = function(fn, delay){
var id = oldST(function(){
fn && fn();
removeTimer(id);
@A1rPun
A1rPun / getParametersFromFunction.js
Last active Aug 17, 2017
Javascript get parameter names from function reference
View getParametersFromFunction.js
function getParameters(func) {
var funcString = func.toString();
var indexStart = funcString.indexOf('(') + 1;
var indexEnd = funcString.indexOf(')');
return funcString
.substring(indexStart, indexEnd)
.trim()
.split(/\s*,\s*/);
}
You can’t perform that action at this time.