Skip to content

Instantly share code, notes, and snippets.

View andreasvirkus's full-sized avatar
🙊
made you look

andreas andreasvirkus

🙊
made you look
View GitHub Profile
/**
* An export pattern for global objects
* (to avoid pollution)
*/
// File A
var foo = (function (that) {
that.bar = 1;
return that;
}(foo || {}));
var counts = [4, 9, 15, 6, 2],
goal = 5;
var closest = counts.reduce(function (prev, curr) {
return (Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev);
});
/**
* Returns a small symbol with the correct time displayed in the depicted analog clock
*/
(function () {
return "\ud83d"+String.fromCharCode(56656+((h=(n=new Date).getHours()%12)?h-1:11)+(n.getMinutes()>29&&12))
})();
<!-- Digital clock -->
<a id=a>
<script>
setInterval`a.innerHTML=new Date().toLocaleTimeString('fr')`
</script>
<!-- Analog clock -->
<x id=c>
<script>
setInterval(eval(a="c.innerHTML='&#'+(128336+((h=(n=new Date).getHours(c.style.fontSize='20vw')%12)?h-1:11)+(n.getMinutes()>29&&12)),a"),1e3)
// ES5
function getNodeindex( elm ){
var c = elm.parentNode.children, i = 0;
for(; i < c.length; i++ )
if( c[i] == elm ) return i;
}
// ES6
function getNodeindex( elm ){
return [...elm.parentNode.children].findIndex(c => c == elm)
// For those crucial times you can't decide which element to pick
$.fn.random = () => {
return this.eq(Math.floor(Math.random() * this.length));
};
// Usage
$('nav li').random().find('a').click();
/* Medium */
body {
font-family: -apple-system,
BlinkMacSystemFont,
"Segoe UI",
Roboto,
Oxygen,
Ubuntu,
Cantarell,
"Open Sans",
import fs from 'fs';
import request from 'request';
/**
* Download and save a file to the project root
*
* Courtesy of http://stackoverflow.com/a/12751657/2803743
*/
export const download = function(uri, filename, callback) {
request.head(uri, function(err, res, body) {
// This is all it takes to create those cancerous popups.
window.onbeforeunload = function (e) {
e = e || window.event;
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = 'Sure?';
}
// For Safari