Skip to content

Instantly share code, notes, and snippets.

View medikoo's full-sized avatar

Mariusz Nowak medikoo

View GitHub Profile
//
// ### function randomString (bits)
// #### @bits {integer} The number of bits for the random base64 string returned to contain
// randomString returns a pseude-random ASCII string which contains at least the specified number of bits of entropy
// the return value is a string of length ⌈bits/6⌉ of characters from the base64 alphabet
//
helpers.randomString = exports.randomString = function (bits) {
var chars, rand, i, ret;
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-';
@jed
jed / after.js
Created October 19, 2011 20:54
crushing 1000 booleans
a="[                                      true]";for(b in c="         true,false,false,true,".split(""))a=a.replace(RegExp(c[b][0],"g"),c[b].slice(1));eval(a)
@domenic
domenic / oauth2-restify.js
Created June 2, 2012 06:25
OAuth2 with Restify
"use strict";
var restify = require("restify");
var users = require("./users");
// The users module will have a getAuthorizationFromAccessTokenAsync promise-returning export. (Convert to callbacks if you wish).
// It rejects in cause of not authorized, or fulfills with a { scope, customerId } object if the user is authorized.
// The scope property indicates which scopes the user corresponding to a given access token has.
module.exports = function authPlugin(serverRequest, serverResponse, next) {
@domenic
domenic / promises.md
Last active June 13, 2025 14:15
You're Missing the Point of Promises

This article has been given a more permanent home on my blog. Also, since it was first written, the development of the Promises/A+ specification has made the original emphasis on Promises/A seem somewhat outdated.

You're Missing the Point of Promises

Promises are a software abstraction that makes working with asynchronous operations much more pleasant. In the most basic definition, your code will move from continuation-passing style:

getTweetsFor("domenic", function (err, results) {
 // the rest of your code goes here.
// Accepts a map function, return a push-filter function.
function mapToPush(map) {
return function (emit) {
return function (err, item) {
if (item === undefined) return emit(err);
emit(null, map(item));
};
};
}
@WebReflection
WebReflection / Object.setPrototypeOf.js
Last active February 24, 2018 04:13
Object.setPrototypeOf(O, proto) full/complete polyfill with boolean flag for partial implementation.
/*jslint devel: true, indent: 2 */
// 15.2.3.2
if (!Object.setPrototypeOf) {
Object.setPrototypeOf = (function(Object, magic) {
'use strict';
var set;
function checkArgs(O, proto) {
if (typeof O !== 'object' || O === null) {
throw new TypeError('can not set prototype on a non-object');
}

This is an attempt at getting a promise library to run as fast as native callbacks while still offering Promises/A+ compatability. It was sparked by these tweets:

@izs

We would not give up perf in core. You can already do this in userland if you have diff priorities.

@forbeslindesay:

It would take some work to optimise and you couldn’t use something like Q, but I’d be surprised if it couldn’t be done.

@WebReflection
WebReflection / gist:6225242
Created August 13, 2013 20:16
serializing RegExp
/o/.toJSON || (RegExp.prototype.toJSON = function () {
return [
this.source,
(this.global ? "g" : "") +
(this.ignoreCase ? "i" : "") +
(this.multiline ? "m" : "")
];
});
var re = /test/g;
alert(RegExp.apply(null, JSON.parse(JSON.stringify(re))));
@domenic
domenic / es6-modules.md
Last active December 26, 2015 00:19
Authoritative ES6 Modules Resources

This is an initial attempt at gathering together up to date resources on ES6 modules.

  • The ES6 draft spec has an up-to-date grammar and list of early errors. It's not the easiest thing to read, but is the most authoritative.
  • This informal grammar summary is pretty good, and starts to explain how default exports work.
  • These two comments in sequence give examples of all the import and export forms available.
  • jorendorff/js-loaders is a polyfill for the module loader API and pipeline, and is where current work is taking place.
  • This essay is out of date in many ways, but I believe its explanation of the loader pipeline is still more or less accurate. To be sure you'd have to check it agai
@dherman
dherman / loader-api.md
Last active January 31, 2019 03:02
Complete ES6 Loader API

Notational Conventions

This section describes the conventions used here to describe type signatures.

A [T] is an array-like value (only ever used read-only in this API), i.e., one with an integer length and whose indexed properties from 0 to length - 1 are of type T.

A type T? should be read as T | undefined -- that is, an optional value that may be undefined.

Loaders