Skip to content

Instantly share code, notes, and snippets.

View tennisonchan's full-sized avatar

Tennison Chan tennisonchan

  • Truewind
  • San Francisco
View GitHub Profile
@tennisonchan
tennisonchan / javascript-java-string-hashCode.js
Last active July 12, 2020 07:22
javascript-hash-function
// http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method/
String.prototype.hashCode = function(){
var hash = 0;
if (this.length == 0) return hash;
for (i = 0; i < this.length; i++) {
char = this.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
hash = hash & hash;
}
function processData(input) {
//Enter your code here
var lines = input.split('\n');
var q = Number(lines.shift());
for(let i = 0; i < q; i++) {
let nm = lines.shift().split(' ').map(Number);
let adj = {};
[...new Array(nm[0])].forEach((a, h) => adj[h + 1] = null);
@tennisonchan
tennisonchan / exclude.sql
Created February 23, 2017 14:52 — forked from fphilipe/exclude.sql
PostgreSQL EXCLUDE constraint
CREATE EXTENSION btree_gist;
CREATE TABLE room_reservations (
room_id integer,
reserved_at timestamptz,
reserved_until timestamptz,
canceled boolean DEFAULT false,
EXCLUDE USING gist (
room_id WITH =, tstzrange(reserved_at, reserved_until) WITH &&
) WHERE (not canceled)
@tennisonchan
tennisonchan / Hash#fetch.rb
Last active February 22, 2017 15:21
Difference between fetch and || with default value #ruby
hash = {
a: 1,
b: nil,
c: false
}
hash.fetch(:a, :default_value) #=> 1
hash.fetch(:b, :default_value) #=> nil
hash.fetch(:c, :default_value) #=> false
hash.fetch(:d, :default_value) #=> :default_value
@tennisonchan
tennisonchan / youtube_video_urls
Created November 5, 2016 02:45
get youtube video stream url in different qualities and formats
let videoUrls = ytplayer.config.args.url_encoded_fmt_stream_map
.split(',')
.map(item => item
.split('&')
.reduce((prev, curr) => (curr = curr.split('='),
Object.assign(prev, {[curr[0]]: decodeURIComponent(curr[1])})
), {})
)
.reduce((prev, curr) => Object.assign(prev, {
[curr.quality + ':' + curr.type.split(';')[0]]: curr
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
@tennisonchan
tennisonchan / ember-serviceworker.md
Created October 12, 2016 15:55 — forked from addyosmani/ember-serviceworker.md
Ember Service Worker support

Ember.js currently doesn't have baked in support for Service Worker. They want this and there's an ember-cli RFCS thread discussing strategies however a number of tooling efforts exist to help fill in this gap today.

Note: you can of course just write vanilla Service Worker code for your Ember.js apps and that will work just fine. This doc tracks tooling and libraries that lower the friction for getting this setup

Service Worker Libraries

These static resource precaching and runtime caching libraries are lower-level than Broccoli, but can be used directly

@tennisonchan
tennisonchan / git-remote-prune.sh
Created October 3, 2016 13:17
Delete merged branches from local on #git
#/bin/bash
git branch --merged | grep -v '\*\|master\|develop' | xargs -n 1 git branch -d
git remote prune origin
@tennisonchan
tennisonchan / github-pull-upstream-repo.sh
Last active September 28, 2016 19:26
Pull the latest branch and tags from the upstream repo
#/bin/bash
UPSTREAM_REPO=https://github.com/torvalds/linux.git
UPSTREAM_BRANCH=master
# pull and rebase the forked upstream repo
git pull ${UPSTREAM_REPO} ${UPSTREAM_BRANCH} --rebase
# fetch all the tags from upstream repo
git fetch ${UPSTREAM_REPO} --tags
@tennisonchan
tennisonchan / ember-render-performance-logger-initializer.js
Last active September 28, 2016 13:35
An Ember render performance initializer to log all the render performance metrics
import Ember from 'ember';
export default {
name: 'ember-render-performance',
initialize() {
Ember.subscribe('render', {
before(_, startTime) { return startTime; },
after(name, endTime, payload, startTime) {
console.log(`${payload.containerKey || name}`, endTime - startTime);