Skip to content

Instantly share code, notes, and snippets.

View cades's full-sized avatar

hong-jen kao cades

View GitHub Profile
@cades
cades / angularjs-provider-demo.js
Last active December 20, 2015 02:59
示範如何在config()中呼叫provider提供的函式。 這個例子寫了一個pathRootProvider, 注意其中兩個被return的object: 第一個是 provider()回傳的物件,注入config()的就是它。第二個是$get()回傳的物件,注入controller的就是它。 關鍵在於「把函式提出$get, 拉到與$get同一個層級」,放在這裡的函式才能在config()內被呼叫。 順帶一提,factory()是provider()的一個方便使用的包裝,傳給factory的function會被放進$get中,也因此失去在config()中被呼叫的可能性。 Robin Fan提供一個影片,解說的頗清楚:http://www.egghead.io/video/HvTZb…
angular.module('myApp', ['ui.state']).
provider('pathRoot', function() {
return {
partialView: function(filename) {
return '../js/angular/partial/' + filename;
},
$get: function(pathId) {
return {};
}
};
@cades
cades / compile-jade-on-save.el
Created July 28, 2013 16:18
Writing html in jade is nice, but I always forget to compile it. Add this piece of code into your .emacs file, your emacs will compiles jade into html after save. 用 jade 寫 html 是件很棒的事, 但我老是忘記編譯它. 把這段 code 加入 .emacs, emacs 在存檔時會自動替你編譯.
;; auto compile jade file after save
(add-hook
'after-save-hook
(lambda ()
(if (string= (file-name-extension (buffer-file-name)) "jade")
(if (= (shell-command
(concat "jade --pretty " (buffer-file-name)))
0)
(message (concat (buffer-file-name) " just saved and compiled into html"))))))
@cades
cades / feedback.sh
Last active August 29, 2015 13:56
把 lisp-koan 的輸出加上顏色
#!/bin/bash
sbcl --script contemplate.lsp | tail -n 13 \
| sed "s/INCOMPLETE/`tput setaf 6`INCOMPLETE`tput sgr0`/g" \
| sed "s/FAIL/`tput setaf 1`FAIL`tput sgr0`/g" \
| sed "s/PASS/`tput setaf 2`PASS`tput sgr0`/g"
# 未完成=青色
# 失敗=紅色
# 通過=綠色
@cades
cades / scoring-project.lsp
Created February 22, 2014 12:18
Solution to lisp-koan' scoring-project, Greed dice game.
(defun score (dice)
(let ((dice (sort dice #'>)))
(flet ((is-1-or-5 (x)
(or (= x 1) (= x 5)))
(triple-p (dice)
(and (= (length dice) 3) (apply #'= dice)))
(triple-score (dice)
(if (= (first dice) 1)
1000
(* 100 (first dice)))))
@cades
cades / log-form-with-value.lsp
Created February 23, 2014 02:29
Solution to lisp-koan's final koan of macros.lsp
(defmacro log-form-with-value (&body body)
"records the body form, and the form's return value
to the list *log-with-value* and then evalues the body normally"
`(let ((logform nil)
(retval ,@body))
;; 有沒有不需要list的解法? 試不出來QQ
(push (list :form ',@body :value retval) *log-with-value*)
retval))
@cades
cades / transformer.js
Last active June 5, 2020 02:52
讀取 google calendar api v3 / Events: list , 轉換成 FullCalendar 的格式
var util = require('util'),
request = require('request'),
parser = require('xml2json'),
calendar_id = 'fgj3gh1p2chvsb0vge2sjrejag@group.calendar.google.com',
api_key = 'AIzaSyBQal2rNhP5SRkU5hZytY7Yb8nYc5Q1nrc',
url = util.format('https://www.googleapis.com/calendar/v3/calendars/%s/events?key=%s', calendar_id, api_key);
request({url: url}, function(error, response, json){
var data = JSON.parse(json),
transformed = data.items.map(function(event){
@cades
cades / get-battery-percent.sh
Created June 19, 2014 13:33
在 OS X 上取得電池啪數的方法
# method 1
pmset -g batt | sed -n 2p | awk -F "[\t;%]" '{ print $2 }'
# method 2
brew tap Goles/battery
brew install battery
battery | awk -F "[\\\[\\\]%]" '{ print $3 }'
# method 3
wget --no-check-certificate https://raw.githubusercontent.com/richo/battery/master/bin/battery
@cades
cades / ng-google-auth.js
Created October 15, 2014 07:01
登入 Google 帳號的 AngularJS directive。 HackNTU 的產出之一,未完成
'use strict';
angular.module('calenshareApp')
.factory('GoogleAuth', function () {
(function() {
var po = document.createElement('script');
po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/client:plusone.js?onload=render';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(po, s);
@cades
cades / gulpfile.js
Last active August 29, 2015 14:08
static web development setup with livereload
var gulp = require('gulp'),
connect = require('gulp-connect');
gulp.task('webserver', function(){
connect.server({
livereload: true
});
});
gulp.task('watch', function(){
@cades
cades / response.feature
Created March 20, 2015 12:40
a HTTP server testing DSL proposal
Feature: I can test request response features
Scenario Outline: response code
When I request "GET" "http://httpbin.org/status/<code>"
Then the response code should be <code>
Examples:
| code |
| 200 |
| 411 |