Skip to content

Instantly share code, notes, and snippets.

Chris Veness chrisveness

Block or report user

Report or block chrisveness

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
@chrisveness
chrisveness / getqueryarg-splitfor.js
Created Aug 30, 2014
Get query string argument (using split/for)
View getqueryarg-splitfor.js
/**
* Returns specified argument from query string.
*
* @params {string} key - Argument to be returned.
* @returns {string} Value of key ('' for ?arg=, null for ?arg, undefined if not present).
*/
function getQueryArg(key) {
var srch = location.search.substring(1); // lose the initial '?'
var args = srch.split(/[&;]/); // list of field=value pairs
for (var i=0; i<args.length; i++) { // for each arg
@chrisveness
chrisveness / getqueryarg-regexp.js
Created Aug 30, 2014
Get query string argument (using regexp)
View getqueryarg-regexp.js
/**
* Returns specified argument from query string.
*
* @params {string} key - Argument to be returned.
* @returns {string} Value of key ('' for ?arg=, null for ?arg, undefined if not present).
*/
function getQueryArg(key) {
// look for key prefixed by ?/&/;, (optionally) suffixed
// by =val (using lazy match), followed by &/;/# or EOS
var re = new RegExp('[?&;]'+key+'(=(.*?))?([&;#]|$)');
@chrisveness
chrisveness / hyphen-camel.js
Last active Aug 29, 2015
Convert between camel-cased & hyphenated strings (eg SomeResourceName <=> some-resource-name)
View hyphen-camel.js
/**
* Returns camel-cased equivalent of (lower-case) hyphenated string.
*
* To enable round-tripping, hyphens not followed by a-z are left intact
* (can be checked for and/or removed manually if required).
*
* Only transforms ASCII capitals (lack of JavaScript Unicode regexp).
*/
function hyphenToCamel(str) {
// for Unicode transforms, replace [a-z] with \p{Ll} if available
@chrisveness
chrisveness / app.js
Last active Aug 29, 2015
koa-flash fails when session domain opts set
View app.js
var koa = require('koa');
var session = require('koa-session');
var flash = require('koa-flash');
var router = require('koa-router');
var handlebars = require("koa-handlebars");
var app = koa();
app.keys = ['foo'];
app.use(session({ domain: '.localhost' }, app)); // THIS FAILS
@chrisveness
chrisveness / app.js
Created Feb 20, 2015
supertest fails when using cookie domain / header host
View app.js
var koa = require('koa');
var session = require('koa-session');
var app = module.exports = koa();
app.keys = ['some secret hurr'];
app.use(session({ domain: '.app.localhost' }, app)); // THIS WORKS IN BROWSER BUT FAILS IN SUPERTEST
//app.use(session(app)); // THIS WORKS EITHER WAY
app.use(function*() {
@chrisveness
chrisveness / index.html
Last active Oct 23, 2019
JavaScript AES client/server interoperability test
View index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AES client/server test</title>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/normalize/3.0.3/normalize.min.css">
<style>
body { font-size: 80%; padding: 1em; }
form { margin-top: 2em; }
label { display: inline-block; width: 6em; }
@chrisveness
chrisveness / mongodb-objectid.js
Created Sep 14, 2016
Generates a MongoDB-style ObjectId in Node.js
View mongodb-objectid.js
/**
* Generates a MongoDB-style ObjectId in Node.js. Uses nanosecond timestamp in place of counter;
* should be impossible for same process to generate multiple objectId in same nanosecond? (clock
* drift can result in an *extremely* remote possibility of id conflicts).
*
* @returns {string} Id in same format as MongoDB ObjectId.
*/
function objectId() {
const os = require('os');
const crypto = require('crypto');
@chrisveness
chrisveness / standard-deviation.js
Created Sep 21, 2016
Standard deviation of set of values
View standard-deviation.js
/**
* Returns standard deviation of set of values.
*
* @param {number[]} values - Array of values.
* @returns {number} Standard devation of values.
*/
function stdDeviation(values) {
const avgOfValues = average(values);
const squaresOfDiffs = values.map(value => (value-avgOfValues)**2);
const avgOfSquaresOfDiffs = average(squaresOfDiffs);
@chrisveness
chrisveness / crypto-sha.js
Last active Sep 7, 2019
Uses the SubtleCrypto interface of the Web Cryptography API to hash a message using SHA-256.
View crypto-sha.js
/**
* Returns SHA-256 hash from supplied message.
*
* @param {String} message.
* @returns {String} hash as hex string.
*
* @example
* sha256('abc').then(hash => console.log(hash));
* const hash = await sha256('abc');
*/
@chrisveness
chrisveness / crypto-pbkdf2.js
Last active Feb 20, 2019
Uses the SubtleCrypto interface of the Web Cryptography API to hash a password using PBKDF2, and validate a stored password hash against a subsequently supplied password. Note that both bcrypt and scrypt offer better defence against ASIC/GPU attacks, but are not available within WebCrypto.
View crypto-pbkdf2.js
/**
* Returns PBKDF2 derived key from supplied password.
*
* Stored key can subsequently be used to verify that a password matches the original password used
* to derive the key, using pbkdf2Verify().
*
* @param {String} password - Password to be hashed using key derivation function.
* @param {Number} [iterations=1e6] - Number of iterations of HMAC function to apply.
* @returns {String} Derived key as base64 string.
*
You can’t perform that action at this time.