Skip to content

Instantly share code, notes, and snippets.

@lamchau
lamchau / 0_reuse_code.js
Last active August 29, 2015 14:16
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
// do not use result cache, nor line and column tracking
{ var indentStack = [], indent = ""; }
start
= INDENT? lines:( blank / line )*
{ return lines; }
line
= SAMEDENT line:(!EOL c:. { return c; })+ EOL?
@lamchau
lamchau / add-ip.sh
Last active August 29, 2015 14:17
add IPs to access PostgreSQL remotely (Short URL: http://git.io/hiFi)
#!/bin/sh
# prerequisite: sudo su - postgres
CONFIGURATION_FILE="/etc/postgresql/9.4/main/pg_hba.conf"
CURRENT_IP=$(last -i | grep "still logged" | awk '{ print $3 }')
IPV4_REGEX="\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
if [ ! -f "$CONFIGURATION_FILE" ]; then
echo "File not found: $CONFIGURATION_FILE"
exit 1
var node = {
value: -120,
left: {
value: -2120,
left: { value: -20 },
right: { value: -30 }
},
right: {
value: -20,
left: {value: 20 },
@lamchau
lamchau / controllers__command.js
Created April 2, 2015 20:25
ember.js: command pattern with browser history for undo/redo
/**
* Command Dispatcher for managing {@code App.Command}.
*
* {@link http://en.wikipedia.org/wiki/Command_pattern|Command Pattern}
*/
App.CommandController = Ember.Controller.extend({
commands: null,
result: null,
position: 0,
maxRedo: 0,
(function(History) {
if (!Ember.Object.detectInstance(History)) {
throw new Error("Native `Ember.History` is already defined.");
}
if (Ember.Evented.detect(History)) {
return;
} else {
History.reopen(Ember.Evented);
}
describe('filterObject', function () {
it('should filter out falsey values', function () {
var actual = filterObject({
empty: '',
depth: 0,
nested: {
empty: '',
depth: 1,
nested: {
empty: '',
@lamchau
lamchau / youtube-repeat.js
Created April 19, 2015 09:28
youtube infinite repeater
(function() {
if (!/youtube.com$/i.test(location.host)) {
alert("hostname does not match 'youtube.com'");
return;
}
var timeout;
var resume;
var repeat = function repeat() {
clearTimeout(timeout);
@lamchau
lamchau / logging.js
Created April 21, 2015 00:31
simple pretty log4j-like logging (requires chalk for color)
var util = require('util');
var chalk = require('chalk');
var LOG_TYPE = chalk.bold.black('[') + '%s' + chalk.bold.black(']') + ':';
var ALL = {
type: 'ALL'
};
var TRACE = {
type: 'TRACE',
fn: console.log,
function pad(n, count) {
// http://stackoverflow.com/a/15398371
var length = (Math.log(Math.abs(n + 1)) * 0.43429448190325176 | 0) + 1;
count = Math.max(count - length, 0);
if (n < 10) {
return n;
}
// http://stackoverflow.com/a/5450113