Skip to content

Instantly share code, notes, and snippets.

View dmh2000's full-sized avatar

david h dmh2000

View GitHub Profile
@dmh2000
dmh2000 / antisamy-example.js
Created March 2, 2012 22:38
How to use AntiSamy-java from a node.js application to sanitize HTML
var java = require('java');
var util = require('util');
// -------------------------------------------------
// How to use AntiSamy from Node to sanitize HTML
// -------------------------------------------------
// sanitizing HTML input is a science in itself. better not to reinvent the wheel
// the AntiSamy main website is https://www.owasp.org/index.php/Category:OWASP_AntiSamy_Project
@dmh2000
dmh2000 / uv1.c
Created May 31, 2012 17:54
simple UDP receiver in C using libuv from node.js platform
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "uv.h"
// uv1.c
// compile with : gcc -o uv1 -I libuv/include -Wall uv1.c libuv/uv.a -lpthread -lm -lrt
#define CHECK(r, msg) \
if (r) { \
@dmh2000
dmh2000 / grunt-make
Last active August 29, 2015 13:56
How to invoke external make when building a node.js module with Grunt
// use node 'child_process' to exec make
var exec = require('child_process').exec;
// register a custom task
grunt.registerTask('make','invoke external make',function() {
// set grunt task to async mode
var done = this.async();
// execute make (or any other external executable for that matter)
exec('make',function(error,stdout,stderr) {
@dmh2000
dmh2000 / gruntfile with 'make'
Created February 17, 2014 23:00
sample gruntfile with external make
'use strict';
var exec = require('child_process').exec;
module.exports = function(grunt) {
grunt.initConfig({
jshint: {
// define the files to lint
files: ['gruntfile.js', 'main.js', 'src/**/*.js', 'test/**/*.js'],
// configure JSHint (documented at http://www.jshint.com/docs/)
#include <unistd.h>
#include <node.h>
#include <string.h>
#include <v8.h>
using namespace v8;
unsigned long long count = 0;
// native blocking/compute intensive function
#!/usr/bin/env python
import os
import shlex
import struct
import platform
import subprocess
def get_terminal_size():
""" getTerminalSize()
@dmh2000
dmh2000 / category.js
Last active August 29, 2015 14:17
Simple example of pointfree-fantasy composition and mapping
#!/usr/bin/env node
"use strict";
// tested with io.js 1.5.1 which supports ES6 'const'
// there are no mutable variables in the entire gist
const assert = require('assert');
const util = require('util');
const P = require('pointfree-fantasy');
const map = P.map;
const compose = P.compose;
const Maybe = require('pointfree-fantasy/instances/maybe');
@dmh2000
dmh2000 / time.js
Last active October 11, 2015 14:38
Assaf Arkin Date Challenge
// see results at https://labnotes.org/validate-your-assumptions/
var http=require('http');
var fs=require('fs');
// read the file and make a list of the hostnames
var list=fs.readFileSync('hostnames.txt').toString().split('\r\n');
// get from one host and process it
function get(a,i) {
if (i >= a.length) {
@dmh2000
dmh2000 / traits.c
Last active October 6, 2015 22:51
kind of simple example for how 'traits' work
#include <cstdio>
#include <cstdint>
#include <limits>
/**
USING TAGGED DISPATCH FOR GENERIC IF-THEN-ELSE
Or, sort of a contrived example that illustrates more or less how 'tagged disppatch' in the STL work
*/
@dmh2000
dmh2000 / loop.js
Last active September 17, 2020 12:43
javascript loop with async callback handling
"use strict";
// =================================================
// a common question that pops up is
// 'why do my async functions use the final value of my loop variable instead of the one they are called with'?
// its because they refer directly to the loop variable and its last value
// the solutions are various ways to bind the loop variable in a new scope
//
// this gist shows different ways to handle a loop that spawns an async function that depends on the loop index
// =================================================