Skip to content

Instantly share code, notes, and snippets.

View Spaxe's full-sized avatar
🏹

Xavier Ho Spaxe

🏹
View GitHub Profile
@Spaxe
Spaxe / loop.js
Created March 16, 2015 00:43
It's 2015 and I still have to write this to iterate through objects in Javascript.
// Util functions
var Util = {
loop: function (obj, callback) {
for (var x in obj) {
if (obj.hasOwnProperty(x)) {
callback(x, obj[x]);
}
}
return obj;
}
@Spaxe
Spaxe / Usage Example
Created March 17, 2015 02:29
Deploy master to GitHub Pages and push both branches
# Step 0: save git-deploy-master somewhere in your $PATH and make sure it has execute permission.
# in master, commit your code
git commit -m "Updated website"
# Deploy to gh-pages
git deploy-master
@Spaxe
Spaxe / split-tsv.py
Created August 7, 2015 00:41
Splits large tab-separated value files into smaller ones.
#!/usr/bin/env python3
'''Breaks large tab-separated value files into smaller files'''
import argparse
def split_tsv(filepath, lines, limit):
header = []
with open(filepath, 'r') as f:
# Assume there is header
header = f.readline().split()
@Spaxe
Spaxe / jquery.promise.js
Created August 31, 2015 23:55
jQuery promise pseudo code
var i = 0;
function makeChartFromList(list) {
function consume ( data ) {
process(data);
i++;
if (i === list.length) {
$.Deferred().resolve();
} else {
PGraphics gStuff;
int w = 400;
int h = 300;
void setup () {
size(w, h);
gStuff = createGraphics(w, h);
}
void draw() {
@Spaxe
Spaxe / callback-promise.js
Created December 16, 2015 22:21
Promise wrapper - turns any async function with callback into a ES6 Promise
let promise = ( func, ...args ) => {
return new Promise( ( resolve, reject ) => {
let callback = ( error, data ) => {
if (error) reject(error);
else resolve(data);
};
args.push(callback);
func.apply(func, args);
});
@Spaxe
Spaxe / search-wikipedia.js
Created January 9, 2016 11:46
Search on wikipedia
const wikipedia = require("node-wikipedia");
const searchWiki = (term) => {
return new Promise( (resolve, reject) => {
wikipedia.page.data(term, { content: true }, (response) => {
if (!response) reject(null);
resolve(response);
});
});
};
@Spaxe
Spaxe / oceanic-next.css
Last active February 16, 2016 10:27
Oceanic Next RainbowJS Theme
/**
* Oceanic Next Theme for RainbowJS (https://github.com/ccampbell/rainbow)
*
* Adaptation of Oceanic Next from
* https://github.com/voronianski/oceanic-next-color-scheme
*
* Usage: include this file in your HTML like this:
* <link rel="stylesheet" type="text/css" href="css/oceanic-next.css">
*
* Anything transformed by RainbowJS will be automatically applied.
@Spaxe
Spaxe / csv-converter.js
Created March 21, 2016 11:34
Simple NodeJS commandline script to convert JSON to CSV
// Usage:
// node csv-converter.js input_path.json > output_path.csv
//
// Dependency: json-2-csv
if (process.argv.length < 3) process.exit(1);
var fs = require('fs');
var json_2_csv = require('json-2-csv');
@Spaxe
Spaxe / colour_sort.py
Created March 24, 2016 00:53
Sorting image colours by RGB in descending order
# Processing 3.0.1 Python mode
# Original artist: https://medium.com/@GroundTruth/in-living-color-antarctica-s-vibrant-wildlife-under-the-sea-54ee6f09d662#.maye8r6ws
'''Sorts an image by rgb colour and draws them in gradient'''
bar_width = 50
w = 1980
h = 1366
def setup():
size(1980, 1366)