Skip to content

Instantly share code, notes, and snippets.

@devsnek
devsnek / applyEachSeries.js
Created December 11, 2016 04:17
asyncawait's applyEachSeries but with each shitty dependency replaced with native code
function applyEachSeries(array, ...args) {
const iterator = array[Symbol.iterator]();
const final = args.pop(); // performance?
(function cb() {
const x = iterator.next();
if (x.done) return final();
x.value(...args, cb);
})();
}
@devsnek
devsnek / promisify.js
Created December 12, 2016 23:41
also here's a promisify that blows bluebird out of the water.
function promisify (fn, thisArg = null) {
return (...args) => {
return new Promise((resolve, reject) => {
const callback = (err, res) => {
if (err) return reject(err);
return resolve(res);
}
fn.apply(thisArg, [...args, callback]);
});
}
'use strict';
/**
* @class EventEmitter
* @example
* var e = new EventEmitter();
*
* e.on('hello', console.log); // 'hi how are you'
*
* e.emit('hello', 'hi', 'how are you');
const leftpad = (v, n, c = '0') => String(v).length >= n ? '' + v : (String(c).repeat(n) + v).slice(-n);
class Snowflake {
constructor ({ epoch, workerId, datacenterId } = { 1288834974657, 1, process.pid }) {
this.epoch = epoch;
this.workerId = workerId;
this.dataCenterId = datacenterId;
this.incId = 0;
}
-----BEGIN PGP MESSAGE-----
Version: Keybase OpenPGP v2.0.61
Comment: https://keybase.io/crypto
wcFMA80pP6OGefUcAQ//eSm8iqTYlRwKjPFwIdSzi9phcT1fC2trfQ8/Pg3bEaST
O0F2N2WBR+jsbSarTFFQjS5s2xp8fEDjGSmTT8MntS3ZPai5DtN7OAYHxmGNSHx6
3DNrCCE05k2UOe3hcONY5o3hZUh6BCcmispn/a68HetohEJqd9ZYfFi5ouHHAMK7
BjuXwYV/5jHihiP/Vxe9gRFm0Hd5doVZNcTF8XbTsSDMkn9nORNUYWUirwnBVW3U
/sQmX3IjH2PskH70YAS/WVRm/U6ibB/XuQRkJ0SrSFq4N7Ld5MGDyDTr/EoAHVyg
scFqa41jawHyEBwHhGdWg0hTA7q+RvQpQwpHuC7sFkaewPVKghwLqDH2tDvNAtox
@devsnek
devsnek / callbackify.js
Last active December 21, 2016 02:17
with all this hype surrounding promisifying callbacks, i thought i would spice things up, and offer a function that callbackifies promises.
function callbackify (promise) {
return function execute() {
const callback = arguments[arguments.length - 1];
const args = Array.prototype.slice.call(arguments, 0, arguments.length - 1);
if (promise instanceof Promise) promise.then(r => callback(null, r)).catch(callback);
else if (typeof promise === 'function') promise(...args).then(r => callback(null, r)).catch(callback);
else throw new TypeError('`promise` must be a Promise or Function which returns a Promise!');
}
}

Keybase proof

I hereby claim:

  • I am guscaplan on github.
  • I am snek (https://keybase.io/snek) on keybase.
  • I have a public key whose fingerprint is 24E8 BBD2 DA10 B312 CF16 4CA2 58CC E2A2 4E2D 41DC

To claim this, I am signing this object:

exports.Endpoints = {
APPLICATIONS: '/applications',
APPLICATION_ICON: (n, t) => `/applications/${n}/icons/${t}.jpg`,
APPLICATION_RPC: (n) => `/oauth2/applications/${n}/rpc`,
AUTHORIZE_IP: '/auth/authorize-ip',
AVATAR: (n, t, e = t.startsWith('a_') ? 'gif' : 'webp') => `/users/${n}/avatars/${t}.${e}`,
BILLING: '/users/@me/billing',
BILLING_HISTORY: '/users/@me/billing/payments',
BILLING_PAYMENT_SOURCE: '/users/@me/billing/payment-source',
BILLING_PREMIUM_SUBSCRIPTION: '/users/@me/billing/premium-subscription',
@devsnek
devsnek / sneksort.js
Created February 28, 2017 04:37
slowest sort algorithm ever made
const unsorted = [2, 3, 4, 5, 1, 7, 10, 20];
function snekSort(arr) {
const out = [];
for (let i in arr) {
const current = arr[i];
const next = arr[parseInt(i) + 1];
if (i == arr.length - 1) out.splice(1, 0, arr[0]); // out.unshift(arr[0]);
else if (current < next) out.push(next);
else out.unshift(next);
@devsnek
devsnek / typer.js
Created March 26, 2017 09:30
a better typer.js
function eek(element) {
const span = document.createElement('SPAN');
element.appendChild(span);
return span;
}
class Typer {
constructor(element, words = [], options = {}) {
this.element = eek(element);
this.words = words;