Skip to content

Instantly share code, notes, and snippets.

@psema4
psema4 / roughSizeOfObject.js
Created January 14, 2016 21:07
Rough Size of a JavaScript Object
// SOURCE: http://stackoverflow.com/a/11900218/773209
function roughSizeOfObject( object ) {
var objectList = [];
var stack = [ object ];
var bytes = 0;
while ( stack.length ) {
var value = stack.pop();
@psema4
psema4 / mersenne-twister.js
Created November 21, 2015 03:23 — forked from banksean/mersenne-twister.js
a Mersenne Twister implementation in javascript. Makes up for Math.random() not letting you specify a seed value.
/*
I've wrapped Makoto Matsumoto and Takuji Nishimura's code in a namespace
so it's better encapsulated. Now you can have multiple random number generators
and they won't stomp all over eachother's state.
If you want to use this as a substitute for Math.random(), use the random()
method like so:
var m = new MersenneTwister();
@psema4
psema4 / basicGraph.js
Last active October 24, 2015 02:17
Basic JS Graph
#!/usr/bin/env node
/* Creates a simple property graph using the adjacency-list strategy, based on
https://www.khanacademy.org/computing/computer-science/algorithms/graph-representation/a/representing-graphs
*/
var nodes = [
{ id: 0, label: 'Top' }
, { id: 1, label: 'Bottom Right' }
, { id: 2, label: 'Bottom Left' }
// store application state and/or data in an applications' location hash
//
// inspired by: urlHosted http://urlhosted.graphicore.de/about.html
// depends on: jszip https://github.com/Stuk/jszip
window.addEventListener('load', function() {
console.log('starting up...');
var zip = new JSZip();
if (window.location.hash === '') {
@psema4
psema4 / datauri-generator.js
Created August 21, 2015 02:15
Generate Data URI's on the fly
// SOURCE: http://davidwalsh.name/convert-image-data-uri-javascript
function getDataUri(url, callback) {
var image = new Image();
image.onload = function () {
var canvas = document.createElement('canvas');
canvas.width = this.naturalWidth; // or 'width' if you want a special/scaled size
canvas.height = this.naturalHeight; // or 'height' if you want a special/scaled size
@psema4
psema4 / git_branch_parent.sh
Last active May 30, 2016 13:49 — forked from intel352/git_branch_parent.sh
Determine "parent" of the active git branch (Ubuntu)
#!/usr/bin/env bash
# via http://stackoverflow.com/a/17843908/773209
#
# Ubuntu variant
#
# if ack-grep isn't found:
# $ sudo apt-get install ack-grep
branch=`git rev-parse --abbrev-ref HEAD`
#!/usr/bin/env bash
# Use this one-liner to produce a JSON literal from the Git log:
git log \
--pretty=format:'{%n "commit": "%H",%n "author": "%an <%ae>",%n "date": "%ad",%n "message": "%f"%n},' \
$@ | \
perl -pe 'BEGIN{print "["}; END{print "]\n"}' | \
perl -pe 's/},]/}]/'

Grunt and Cordova 3

The advantages of using Grunt with Cordova:

  1. It simplifies working with the cordova cli.
  2. It provides a mechanism to copy platform customization to the platforms directory without having to commit the generated plugins and platforms directories to version control.
  3. It provides a way to watch resources and automatically run cordova commands.

Stack Overflow: .gitignore for PhoneGap/Cordova 3.0 projects - what should I commit?

@psema4
psema4 / DateGenerator.js
Created November 11, 2014 17:20
Convert non-relative date.js-like string to a js Date object
// see also https://github.com/MatthewMueller/date/issues/57
// usage: var someDateObj = DateGenerator("Jan 3 at 4 pm").dt;
function DateGenerator(str) {
function lookupMonth(str) {
var monthsFull = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ],
monthsShort = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ],
result = -1
;
@psema4
psema4 / oisc.html
Last active August 29, 2015 14:09
Barebones "One-Instruction Set Computer" in a web page
<!doctype html>
<html lang="en">
<head>
<title>OISC in a web page</title>
</head>
<body>
<script>
/* pseudocode source: http://en.wikipedia.org/wiki/One_instruction_set_computer