Skip to content

Instantly share code, notes, and snippets.

View CatTail's full-sized avatar

Chiyu Zhong CatTail

View GitHub Profile
@CatTail
CatTail / htmlentity.js
Created November 30, 2012 08:27
Javascript: encode(decode) html text into html entity
// encode(decode) html text into html entity
var decodeHtmlEntity = function(str) {
return str.replace(/&#(\d+);/g, function(match, dec) {
return String.fromCharCode(dec);
});
};
var encodeHtmlEntity = function(str) {
var buf = [];
for (var i=str.length-1;i>=0;i--) {
@CatTail
CatTail / LengauerTarjan.scala
Created July 17, 2024 17:15 — forked from yuzeh/LengauerTarjan.scala
Lengauer-Tarjan Dominator Tree Algorithm
object LengauerTarjan {
// Implement these three yourself
def successors(v: Int): Iterable[Int] = ???
def predecessors(v: Int): Iterable[Int] = ???
def numNodes: Int = ???
// Lifted from "Modern Compiler Implementation in Java", 2nd ed. chapter 19.2
def computeDominatorTree(): Array[Int] = {
var N = 0
@CatTail
CatTail / proto.js
Last active February 1, 2024 15:59
Javascript prototype in a nutshell
var assert = require('assert')
var util = require('util')
function test (inherits) {
function Fruit () {
}
Fruit.prototype.round = false
Fruit.prototype.sweet = true
Fruit.prototype.eat = function () {}
@CatTail
CatTail / typal.js
Last active April 3, 2023 20:37
Javascript prototypal/classical/mixins
/*
* Introduces a typal object to make classical/prototypal patterns easier
* Plus some AOP sugar
*
* By Zachary Carter <zach@carter.name>
* MIT Licensed
* */
var typal = (function () {
@CatTail
CatTail / validate_email.js
Last active July 10, 2020 07:17
Javascript: validate email address
/**
* Using regular expression to validate email address.
* Exactly email address format refer to http://en.wikipedia.org/wiki/Email_address.
* @author zhongchiyu@gmail.com
*/
var validateEmail = (function() {
// ATTENSION: escape is really mess because you have to escape in string and
// in regular expression.
var normal = "0-9a-zA-Z\\!#\\$%&'\\*\\+\\-\\/\\=\\?\\^_`\\{\\|\\}~";
// mix contain normal character and special character
@CatTail
CatTail / charlist.ex
Created September 30, 2018 02:01
string to charlist
to_charlist "hello world"
@CatTail
CatTail / read-kinesis.py
Created August 4, 2018 07:34
Simple CLI script read kinesis record and log to console
#! /usr/bin/env python
import sys
import boto3
import json
from datetime import datetime
import time
if len(sys.argv) != 3:
print "Usage: read-kinesis.py <region name> <stream name>"
exit(1)
@CatTail
CatTail / client.html
Created February 9, 2017 10:17
Simple Req/Rep pattern for socket.io (which is Pub/Sub in nature)
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.2/socket.io.js"></script>
<script>
var socket = io('http://localhost:8088');
var route = createRouter((reply) => {
socket.on('rep', reply)
})
socket.emit('req', route('hello world', (data) => {
console.log(data)
}))
@CatTail
CatTail / input.json
Last active March 22, 2018 06:20
JSON to Graphql schema generator
{"title":"A New Hope","episode_id":4,"opening_crawl":"It is a period of civil war.\r\nRebel spaceships, striking\r\nfrom a hidden base, have won\r\ntheir first victory against\r\nthe evil Galactic Empire.\r\n\r\nDuring the battle, Rebel\r\nspies managed to steal secret\r\nplans to the Empire's\r\nultimate weapon, the DEATH\r\nSTAR, an armored space\r\nstation with enough power\r\nto destroy an entire planet.\r\n\r\nPursued by the Empire's\r\nsinister agents, Princess\r\nLeia races home aboard her\r\nstarship, custodian of the\r\nstolen plans that can save her\r\npeople and restore\r\nfreedom to the galaxy....","director":"George Lucas","producer":"Gary Kurtz, Rick McCallum","release_date":"1977-05-25","characters":["http://swapi.co/api/people/1/","http://swapi.co/api/people/2/","http://swapi.co/api/people/3/","http://swapi.co/api/people/4/","http://swapi.co/api/people/5/","http://swapi.co/api/people/6/","http://swapi.co/api/people/7/","http://swapi.co/api/people/8/","http://swapi.co/api/people/9/","http://swa
@CatTail
CatTail / migrate-redis.py
Last active March 20, 2018 11:34 — forked from thomasst/migrate-redis.py
Migrate Redis data on Amazon ElastiCache
"""
Copies all keys from the source Redis host to the destination Redis host.
Useful to migrate Redis instances where commands like SLAVEOF and MIGRATE are
restricted (e.g. on Amazon ElastiCache).
The script scans through the keyspace of the given database number and uses
a pipeline of DUMP and RESTORE commands to migrate the keys.
Requires Redis 2.8.0 or higher.