Debug ile ilgili node-inspector çok iş görüyor ve hele webstorm'unuz varsa başka bir araca ihtiyacınız yok demektir ama öğrenmenin sonu yok. Bir videoda debug modülünü gördüm ve merak ettim nasıl çalışıyor diye. Aslında webstorm içinde çıktıları filitreleyebilmek isterdim ama buna debug modülü bir nebze hizmet ettiği için bilmekte fayda var.
Şöyle çalışıyormuş:
Önce paketi ekleyin, var Debug = require('debug');
sonra çıktı görmek istediğini fonksiyonların içine bir değişken tanımlayın Debug'dan var debug = Debug('fonksiyonu_filitrelemek_icin_bir_isim_yazin');
çıktılarınızı görmek için için, debug('main fonksiyonu içinden!', degisken, degisken1);
var Debug = require('debug');
/* ekranda sadece app:main
fonksiyonuna ait çıktıları görebilmek için */
Debug.enable('app:main');
/* ekranda hem app:main hem de app:main1
fonksiyonlarına ait çıktıları görebilmek için */
Debug.enable('app:main,app:main1');
/* ekranda tüm debug değişkeni içeren fonskiyonları
fonksiyonlara ait çıktıları görebilmek için */
Debug.enable('*');
var main = function() {
var degisken = [
{ adi:'Cenk', soyadi:'Topkaya' },
{ adi:'Cem', soyadi:'Topkaya' }
];
var degisken1 = { adi:'Cem', soyadi:'Topkaya' }
/* debug değikeni yaratılırken
fonksiyonun eşsiz adını tanımlayalım ki
tüm çalışanların içinde filitre edebilelim */
var debug = Debug('app:main');
debug('main fonksiyonu içinden!');
debug('Tek bir değişken: ', degisken);
debug('Birden fazla değişken: ', degisken, degisken1);
}
var main1 = function() {
var debug = Debug('app:main1');
debug('main1 fonksiyonu içinden!');
}
main();
main1();
Debug.Assert için stackoverflow.com da güzel bir soru ve tonla cevap var.
Bu da beğendiğim bir cevap:
You should use Debug.Assert to test for logical errors in your programs. The complier can only inform you of syntax errors. So you should definetely use Assert statements to test for logical errors. Like say testing a program that sells cars that only BMWs that are blue should get a 15% discount. The complier could tell you nothing about if your program is logically correct in performing this but an assert statement could.
This assumes that your anchors will always be in the form <a href="...">...</a> i.e. it won't work if there are any other attributes (for example, target). The regular expression can be improved to accommodate this.
To break down the regular expression:
/ -> start regular expression [^<]* -> skip all characters until the first <
( -> start capturing first token <a href=" -> capture first bit of anchor ( -> start capturing second token [^"]+ -> capture all characters until a " ) -> end capturing second token "> -> capture more of the anchor ( -> start capturing third token [^<]+ -> capture all characters until a <
) -> end capturing third token <\/a> -> capture last bit of anchor ) -> end capturing first token /g -> end regular expression, add global flag to match all anchors in string
Each call to our anonymous function will receive three tokens as the second, third and fourth arguments, namely arguments[1], arguments[2], arguments[3]:
arguments[1] is the entire anchor
arguments[2] is the href part
arguments[3] is the text inside
We'll use a hack to push these three arguments as a new array into our main matches array. The arguments built-in variable is not a true JavaScript Array, so we'll have to apply the split Array method on it to extract the items we want:
Array.prototype.slice.call(arguments, 1, 4)
This will extract items from arguments starting at index 1 and ending (not inclusive) at index 4.