Skip to content

Instantly share code, notes, and snippets.

View aidanbon's full-sized avatar

Francis Au-Yeung aidanbon

View GitHub Profile
@aidanbon
aidanbon / shake.js
Created January 12, 2014 05:09
shake.js - Enable shake gesture detection to trigger a callback function. Code based on a gist by iLee @ https://gist.github.com/leecrossley/4078996
/*
* shake.js - Enable shake gesture detection to trigger a callback function.
* Code based on a gist by iLee @ https://gist.github.com/leecrossley/4078996
*
* Sample usage:
* var Shaker = require("./shake");
* var shaker = new Shaker(
* function onShakeSuccess() {
* console.debug("Shake motion detected!");
* },
@aidanbon
aidanbon / isConnected.js
Last active August 29, 2015 13:58
Use Breath-First search to determine if two nodes are connected in a graph.
/*
* isConnected.js
* Determine if a node is connected to another node within a graph.
* The implementation uses the Breath-first search algorithm.
* http://en.wikipedia.org/wiki/Breath-first_search
*
* Download this file to run the program and its test cases:
* % node isConnected.js
*
* Sample output:
@aidanbon
aidanbon / shortBinary.js
Created April 7, 2014 05:26
Short Binary number multiplication
/**
* shortBinary.js
* "Short binary" format uses an array A to represent a binary number B'.
* If A[i] = x then B[x] = 1 where B is a string representing a binary number B'.
* For example:
* Short binary format of [0, 3, 7] represents the binary number 10010001.
*
* This program produces an array in the "short binary" format that represents
* 3 times the the input array (also in short binary format).
*
@aidanbon
aidanbon / myParseInt.js
Last active December 30, 2022 08:31
Implementation of the parseInt function.
/**
* myParseInt.js
* My implementation of the parseInt function.
* The function takes a string and returns an integer. i.e. int myParseInt(string str)
*
* Download this file to run the program and test cases:
* % node myParseInt.js
*
* Sample output:
* Correct: "999.4" --> 999
@aidanbon
aidanbon / fff.js
Last active August 29, 2015 13:58
Efficiently find a huge number of files that contain a specific pattern.
/**
* fff.js:
* fff, Fast File Find, efficiently find a huge number of files (of specific file types,
* html, xml and json) which contain phone numbers in the format (xxx)-xxx-xxxx or xxx-xxx-xxxx
*
* The implementation takes advantage of the asynchronous processing of Node.js.
*
* To run: (expects test files stored under the "data" folder)
* % node fff.js
*
@aidanbon
aidanbon / Tree.js
Last active August 19, 2016 19:59
Tree serialization and deserialization
class Tree {
/*
* Construct an empty tree
*/
constructor () {
this.connections = {}
}
/*
* Return a ';' delimited string with each component
@aidanbon
aidanbon / sample_avro.json
Last active May 19, 2017 04:27
Sample Avro Schema
{
"namespace": "customerManagement.avro",
"type": "record",
"name": "Customer",
"fields": [
{
"name": "userID", "type": "long", "default": 9999
},
{
"name": "userName", "type": "string"
@aidanbon
aidanbon / avro-validation.js
Last active January 4, 2024 20:34
Enhance Avro to support data validation via Regular Expression
/*
* Enhance Avro to support data validation via Regular Expression
*/
const avro = require('avsc')
class ValidatedString extends avro.types.LogicalType {
constructor (attrs, opts) {
super(attrs, opts)
this._pattern = new RegExp(attrs.pattern)
}
@aidanbon
aidanbon / sequentialExecutor.js
Created April 16, 2018 01:31
sequentialExecutor
async function sequentialExecutor(arr) {
const resArr = []
for (const func of arr) {
const res = await func()
resArr.push(res)
}
return resArr
}