Skip to content

Instantly share code, notes, and snippets.

View espretto's full-sized avatar

Marius espretto

  • Oldenburg
View GitHub Profile
@espretto
espretto / is.js
Last active June 5, 2016 22:01
JavaScript: type/class/node check functions
/*!
* is, released under MIT licence
* http://mariusrunge.com/mit-licence.html
* inspired by / partially taken from underscorejs.org
*
* how to use:
* - comment the code-sections and/or `classNames` you don't need
* - integrate `is` into your lib via the `.noConflict()` option
* - eventually clean up the global namespace via `delete window.is` (try-catch for IE)
*
javascript:(function(){
this.write('<!DOCTYPE html>\
<html>\
<head>\
<meta charset="UTF-8"/>\
<script src="http://localhost:8000/path/to/your/script.js"></script>\
</head>\
<body></body>\
</html>');
this.close()
var http = require('http');
var server = http.createServer(function(req, res) {
// console.log(req); // debug dump the request
// If they pass in a basic auth credential it'll be in a header called "Authorization" (note NodeJS lowercases the names of headers in its request object)
var auth = req.headers['authorization']; // auth is in base64(username:password) so we need to decode the base64
console.log("Authorization Header is: ", auth);
@espretto
espretto / JavaScript: nextTick.js
Last active October 24, 2016 22:37
progressive shim to defer execution until next run-loop cycle
/**
* progressive shim to defer execution until next run-loop cycle
*
* prefers
* - `process.nextTick` over
* - `process.setImmediate` or `window.setImmediate` over
* - `window.requestAnimationFrame` over
* - `window.postMessage` and `window.addEventListener` over
* - `setTimeout` as the very last resort
*/
#!/usr/bin/env python
def keygen( mac ):
bytes = [int(x, 16) for x in mac.split(':')]
c1 = (bytes[-2] << 8) + bytes[-1]
(s6, s7, s8, s9, s10) = [int(x) for x in '%05d' % (c1)]
(m7, m8, m9, m10, m11, m12) = [int(x, 16) for x in mac.replace(':', '')[6:]]
k1 = (s7 + s8 + m11 + m12) & (0x0F)
mkdir flaskapp && cd flaskapp
touch config.py run.py shell.py app.db requirements.txt
mkdir app
touch app/__init__.py app/constants.py
mkdir app/static app/templates
@espretto
espretto / corsify.py
Last active August 29, 2015 14:03
python: cors headers
# decorators
from functools import wraps
from django.http import HttpRequest
ALLOWED_HEADERS = (
'content-type',
'accept',
'origin',
'authorization',
@espretto
espretto / jq-ajax-wrapper.js
Last active August 29, 2015 14:06
JavaScript: promisified jQuery.ajax
// promisify $.ajax with Q
// =======================
var ns = {};
ns.ajax = (function($, Q){
var defaults = {
type: 'GET',
@espretto
espretto / minify.html.js
Last active June 5, 2016 21:39
JavaScript: minify html
// beware, parser uses regexes
// ===========================
var minifyHtml = (function(){
var rePreserveTags = /<(pre|style|script(?![^>]*?src))[^>]*>[\s\S]*?<\/\1>/gi,
rePreserveStrings = /("|')(?:\\\1|[^\1])*?\1/g,
reInsertStrings = /__str(\d+)__/g,
reInsertTags = /<preserved>/g,
@espretto
espretto / fuzzySearch.ts
Last active May 29, 2024 08:33
ts: fuzzy search
import escapeRegExp from "lodash.escaperegexp";
/**
* used to create regular expressions that match if all needle-chars appear in
* the same order w/ any number of inbetween chars.
*/
export function fuzzyRegExp(needle: string) {
return new RegExp(needle.split("").map(escapeRegExp).join(".*?"), "i");
}