Skip to content

Instantly share code, notes, and snippets.

🏠
Working from home

簡煒航 (Jian Weihang) tonytonyjan

🏠
Working from home
Block or report user

Report or block tonytonyjan

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@tonytonyjan
tonytonyjan / copyExif.js
Created Sep 23, 2019
copy EXIF from one JPEG blob to another and return a new JPEG blob.
View copyExif.js
// Copyright (c) 2019 Jian Weihang <tonytonyjan@gmail.com>
export default async (src, dest) => {
const exif = await retrieveExif(src);
return new Blob([dest.slice(0, 2), exif, dest.slice(2)], {
type: "image/jpeg"
});
};
export const SOS = 0xffda,
@tonytonyjan
tonytonyjan / chunghwa_post
Created May 21, 2019
中華郵政包裹查詢 API
View chunghwa_post
#!/bin/sh
test $# != 1 && cat <<USAGE && exit 1
Usage: $0 MAIL_ID
== Output Example ==
[
{
"datime": "20190520195647",
View ja-test
#!/usr/bin/env ruby
# frozen-string-literal: true
平仮名 = {
清音: {'' => 'a', '' => 'ka', '' => 'sa', '' => 'ta', '' => 'na', '' => 'ha', '' => 'ma', '' => 'ya', '' => 'ra', '' => 'wa', '' => 'i', '' => 'ki', '' => 'shi', '' => 'chi', '' => 'ni', '' => 'hi', '' => 'mi', '' => 'ri', '' => 'u', '' => 'ku', '' => 'su', '' => 'tsu', '' => 'nu', '' => 'fu', '' => 'mu', '' => 'yu', '' => 'ru', '' => 'n', '' => 'e', '' => 'ke', '' => 'se', '' => 'te', '' => 'ne', '' => 'he', '' => 'me', '' => 're', '' => 'o', '' => 'ko', '' => 'so', '' => 'to', '' => 'no', '' => 'ho', '' => 'mo', '' => 'yo', '' => 'ro', '' => 'o'},
濁音: {'' => 'ga', '' => 'za', '' => 'da', '' => 'ba', '' => 'pa', '' => 'gi', '' => 'ji', '' => 'ji', '' => 'bi', '' => 'pi', '' => 'gu', '' => 'zu', '' => 'zu', '' => 'bu', '' => 'pu', '' => 'ge', '' => 'ze', '' => 'de', '' => 'be', '' => 'pe', '' => 'go', '' => 'zo', '' => 'do', '' => 'bo', '' => 'po'},
拗音: {'きゃ' => 'kya', 'ぎゃ' =>
View keybase.md

Keybase proof

I hereby claim:

  • I am tonytonyjan on github.
  • I am tonytonyjan (https://keybase.io/tonytonyjan) on keybase.
  • I have a public key whose fingerprint is EF91 623D 1477 800D 5701 24B6 294A EB9F 5C88 9A31

To claim this, I am signing this object:

@tonytonyjan
tonytonyjan / autoloader.rb
Created Apr 21, 2018
Autoloader loads ruby files lazily
View autoloader.rb
module AutoLoader
def self.included(mod)
caller_path, = caller(1..1).first.partition(':')
pattern = "#{File.dirname(caller_path)}/#{File.basename(caller_path, '.rb')}/*.rb"
Dir.glob(pattern).each do |path|
class_name = ::Utils.classify(File.basename(path, '.rb')).to_sym
mod.autoload class_name, path
end
end
end
@tonytonyjan
tonytonyjan / benchmark.rb
Last active Sep 20, 2017
`[4, 5].max` is slower than `[a, b].max` with ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16]
View benchmark.rb
require 'benchmark'
n = 100000000
Benchmark.bmbm do |x|
x.report('[4, 5].max') { n.times { [4, 5].max } }
a, b = 4, 5
x.report('[a, b].max') { n.times { [a, b].max } }
end
@tonytonyjan
tonytonyjan / dasherize.sh
Created Apr 23, 2017
Convert any string to to lowercase and dashes fromat
View dasherize.sh
#!/bin/sh
# $ dasherize Hello, world! I am tonytonyjan.
printf "$*" \
| tr '[:upper:]' '[:lower:]' \
| tr -C '[:alnum:]' ' ' \
| tr -s ' ' '-' \
| sed -e 's/^-*//' -e 's/-*$//' \
| tr -d '\n'
@tonytonyjan
tonytonyjan / benchmark.rb
Last active Jun 4, 2017
How fast is `reverse!` technique.
View benchmark.rb
# conversation: https://github.com/rack/rack/commit/734a00c5f4bb46e9a5e6e2677d89a2f285dcc185
#
# user system total real
# reverse: 0.010000 0.010000 0.020000 ( 0.024657)
# join: 0.050000 0.010000 0.060000 ( 0.056571)
# regexp: 0.050000 0.000000 0.050000 ( 0.048339)
require 'benchmark'
def join(session_data)
@tonytonyjan
tonytonyjan / README.md
Last active Dec 3, 2016
rack-protection minimal example
View README.md

Usage

gem install 'rack-protection'
ruby server.rb

There's a page with 2 forms, the one without CSRF token field will get 403 Forbidden response.

Note

@tonytonyjan
tonytonyjan / interactor.rb
Last active Jul 20, 2016
Minimal service object implementation
View interactor.rb
class Interactor
attr_reader :error
def self.perform(*args)
new(*args).tap { |interactor| catch(:fail) { interactor.perform } }
end
def success?
@error.nil?
end
You can’t perform that action at this time.