Skip to content

Instantly share code, notes, and snippets.

@noel9999
noel9999 / gist:df833096354591a240d1
Created July 8, 2014 10:50
記錄一下自己寫的function,希望能做出像是ruby的inspect
function my_inspect(object,isExclusive){
isExclusive = typeof(isExclusive)!= "undefined" ? isExclusive : false
var result=[];
for(var member in object){
var type = typeof object[member];
if(isExclusive){
if (object.hasOwnProperty(member))
result.push(member+"["+type+"] : "+object[member]);
}
else{
@noel9999
noel9999 / gist:0297dfb2bbb4cb7c8072
Last active August 29, 2015 14:03
Javascript:抓query_string,可能還是有缺陷,但自己寫的就先這樣吧,有問題的話儘管提出討論吧~
//運用正規表示法
function getParameterByName(name) {
var results = RegExp("[\\?&]" + name + "=([^&#]*)").exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
//運用一般陣列
function getParameterByName2(name){
var object = {};
var pairs = location.search.substring(1).split("&");
@noel9999
noel9999 / gist:837913f065351b04c805
Created July 9, 2014 17:09
Javascript:練習實作自己的foreach
Array.prototype.myEach = function(callback){
for(i=0;i<this.length;i++){
callback(this[i]);
}
};//這邊是先定義myEach並且透過prototype指定給Array,讓之後所有的Array都可以用此方法
[1,2,3,4,5].myEach(function(element){
console.log(element);
});
@noel9999
noel9999 / gist:a86b973e2fd9a8a277bc
Created July 10, 2014 08:56
Javascript: 取亂算數, 練習寫一支js版的猜字遊戲
//JS取亂數可以靠random()這方法,回傳是0~1以內的隨機小數
Math.random();
//設定可以取回指定範圍內亂數
function useFloor(min,max){
return Math.floor(Math.random()*(max-min+1)+min);
}
function useCeil(min,max){
@noel9999
noel9999 / gist:e78296ae57dd10882a1c
Last active August 29, 2015 14:03
Javascript: 自己動手做shuffle,隨機置換陣列內的成員
function shuffle(array){
var myLength = array.length, temp, index
while(myLength > 0){
index = Math.floor(Math.random()*myLength);
myLength--; //範圍不斷縮減,以防止已改變的成員又做更動
temp = array[myLength];
array[myLength] = array[index];
array[index] = temp;
}
return array;
@noel9999
noel9999 / gist:c53cae062974c44d897c
Created July 11, 2014 10:45
Javascript: 練習實作select,模仿ruby的select
Array.prototype.select = function(callback){
count = this.length;
results = new Array();
for(i=0;i<count;i++){
if(callback(this[i]))
results.push(this[i]);
}
return results;
};
@noel9999
noel9999 / gist:b200847503fe5879c48d
Last active August 29, 2015 14:05
The onbeforeunload way with turbolinks.
// Without turbolinks, it works fine!
$(document).ready(function(){
formmodified=0;
$('form *').change(function(){
formmodified=1;
});
window.onbeforeunload = confirmExit;
function confirmExit() {
if (formmodified == 1) {
return "資料有做修改,確定直接離開頁面嗎?";
@noel9999
noel9999 / gist:ee70493606edee7d0520
Created September 21, 2014 08:51
Chrome載入jquery
var jq = document.createElement('script');
jq.src = "http://code.jquery.com/jquery-latest.min.js";
document.getElementByTagName('head')[0].appendChild(jq);
//Done! Really simple as you imagine!
@noel9999
noel9999 / gist:45c081f96c308f65c11e
Last active August 29, 2015 14:13
js regExp 練習
var text = 'there are a lot of great websites like www.missingmanuals.com and http://www.noelsaga.com'
var urlRegex = /((\bhttps?:\/\/)|(\bwww\.))\S*/g
var url = text.match(urlRegex)
console.log(url[0])
console.log(url[1]);
//看看兩者的差異
var text2 = 'my website is www.noelsaga.com'
var urlRegex2 = /((\bhttps?:\/\/)|(\bwww\.))\S*/
@noel9999
noel9999 / checkout.rb
Created April 7, 2017 07:07
refactor & test demo
def checkout
m = @merchant_data
seller_id = m["_id"]
unfulfillable_items = get_invalid_cart_items
if unfulfillable_items.present?
render status: :unprocessable_entity, json: { message: I18n.t('orders.error.fulfillment_error'), data: unfulfillable_items}
return
end
delivery_option_id = params.fetchpath("order/delivery_option/_id")