BLOG

standard

I’m never going back to modern editor again.

17.07.2015 Posted in Knowledges No Comments
Vim Cheatsheet

Ok, the title might be a little bit exaggerating. But let me clear this up first. I still use modern editors, TextMate, Atom.io or IntelliJ. They are pros and cons. But here’s my real answer if people ask me. I want to be the cool kid!.

I don’t know if you’re like me but I get asked almost all the time I go to a meetup or conference, “What’s your favourite editor”?. People almost always say Vim|Emacs. I tried both of them in the past and gave up more than I can remember because of all the shortcuts and plugins and I just get too frustrated to use either of them. Finally, I have made a decision that I’m going to stick with Vim for a month and let’s see if at the end of the month I still can’t use it. It’s been almost a month and I’d say I’m never going back to modern editors again (at least when I’m coding scripting languages). Here’s why.

It’s just easier

I know it’s not really easier than TextMate or Sublime. You still have to learn a lot of shortcuts and commands. Also, bare-bone Vim just doesn’t have when you get in TextMate or Sublime (e.g, Find file, Command+T, Directory structure or code completion). However, after you powered through that learning phrase everything is just natural to you.

If you want to clear any trailing whitespaces you just have to type %s/\s\+$// and hit enter. If you want to run some external command you can always do in Vim. For example, if you just want to git status you can install Git wrapper or you can just run :!git status. You don’t even need to go to your terminal which I think it’s faster.

If you’re a good engineer, you will always do TDD. Going back and fourth between the editor and terminal, it’s just too annoying. I remember when I was coding Java I can just hit Cmd+t in IntelliJ or .NET and you see green bars. Isn’t it just awesome? I realised that TextMate and Sublime have plugins to do that as well or you can just write your own plugin or package. I tried a couple of plugins and they never work for me. In Vim, it just works!.

Community

If Vim|Emacs doesn’t do what you want, you will almost always find the plugin that does it for you. Currently, my standard plugins are. NERDTree, CommandT, vim-fugitive, vim-rooter, vim-virtualenv. All these plugins just make my Vim functions the same as Atom.io or Sublime.

Not for everyone

I have to admit that the first few weeks I got really frustrated. I almost banged my keyboard against the monitor. I found myself opening Atom.io every time I couldn’t do some basic editing in Vim. For example, using vimgrep wasn’t what I expected and I ended up googling a lot. Now I get the hang of it and I don’t use Atom anymore. The worst case is I will just use command line instead.

standard

How to start doing TDD for jQuery plugin.

9.07.2015 Posted in Knowledges No Comments
jquery

I’m a big fan of TDD. I get nervous every time when I put some code it without having tests. I’m developing a simple jQuery plugin and I think hey we can TDD this.

What is it?

The plugin is really simple. It turns ul tag to be taggable field. It’s similar to tag-it but with a lot less functionalities and doesn’t depend on jquery-ui

What you need

I decided to use Karma because I’m going to test a lot of behaviours and Karma seems like a good fit as it run on real browser. Here’s how I setup my project.

I chose jasmine-jquery because it’s easier to create some element to test and it’s easy to setup.

This is my gulpfile.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var gulp = require('gulp');
var karma = require('gulp-karma');

var testFiles = [
  'tests/vendors/jquery-1.11.3.min.js',
  'tests/vendors/jasmine-jquery.js',
  'src/**/*.js',
  'tests/spec/**/*.js'
];
/**
 * Run test once and exit
 */

gulp.task('test', function (done) {
  return gulp.src(testFiles)
    .pipe(karma({
      configFile: 'karma.conf.js',
      action: 'run'
    }))
  .on('error', function(err) {
    // Make sure failed tests cause gulp to exit non-zero
    throw err;
  });
});

gulp.task('default', function() {
  gulp.src(testFiles)
    .pipe(karma({
      configFile: 'karma.conf.js',
      action: 'watch'
    }));
});

This is my karma.conf.js

1
2
3
4
5
6
module.exports = function(config) {
  config.set({
    browsers: ['PhantomJS'],
    frameworks: ['jasmine']
  });
};

Here’s my first test

1
2
3
4
5
6
7
8
9
10
11
12
13
describe('Taggify', function() {
  var fixture;
  beforeEach(function() {
    fixture = setFixtures('<ul id="tag"></ul>');
    jQuery('#tag').taggify();
  });
 
  it('should initialize text box', function() {
    var input = fixture.find('input');
    expect(input.length > 0).toBeTruthy();
  });

});

You will see that the test failed now we implement some code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(function($) {
  $.fn.taggify = function(options) {
    create(this);
    return this;
  };

  function create($theElement) {
    var $input = $('<input class="tag-input"></input>')
      .attr('type', 'text')
      .attr('autocomplete', 'off')
      .wrap('<li></li>');

    $theElement.append($input.parent());
  }
})(jQuery);

Now the test passed.

Now let’s add some event so when you hit enter the tag is added. So, I added one more test

1
2
3
4
5
6
7
8
9
10
11
  it('should add a tag', function() {
    var input = fixture.find('input');
    input.val('tag');
    input.trigger(jQuery.Event('keyup', { keyCode: 13 }));

    var tags = fixture.find('.tag-label');
    var tag = jQuery(tags[0]);
   
    expect(tag.html()).toBe('tag');
    expect(tags.length > 0).toBeTruthy();
  });

Now the test failed.

I’ll fix the test by doing this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
(function($) {
  $.fn.taggify = function(options) {
    create(this);
    return this;
  };

  function create($theElement) {
    var $input = $('<input class="tag-input"></input>')
      .attr('type', 'text')
      .attr('autocomplete', 'off')
      .wrap('<li></li>');

    $input.on('keyup', function(e) {
      if (e.keyCode === 13) {
        var tagText = $input.val();

        var $span = $('<span class="tag-label"></span>');

        $span.text(tagText).wrap('<li class="tag-choice"></li>');
        $theElement.prepend($span.parent());
        $input.val('');
      }
    });

    $theElement.append($input.parent());
  }
})(jQuery);

Now I want to add some negative test case.

1
2
3
4
5
6
7
  it('should not add a tag', function() {
    var input = fixture.find('input');
    input.trigger(jQuery.Event('keyup', { keyCode: 13 }));
    var tags = fixture.find('.tag-label');
   
    expect(tags.length > 0).toBeFalsy();
  });

Oops the test failed, looks like I missed something

I will fix the test by

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
(function($) {
  $.fn.taggify = function(options) {
    create(this);
    return this;
  };

  function create($theElement) {
    var $input = $('<input class="tag-input"></input>')
      .attr('type', 'text')
      .attr('autocomplete', 'off')
      .wrap('<li></li>');

    $input.on('keyup', function(e) {
      if (e.keyCode === 13) {
        var tagText = $input.val();
        if(tagText !== '') {
          var $span = $('<span class="tag-label"></span>');

          $span.text(tagText).wrap('<li class="tag-choice"></li>');
          $theElement.prepend($span.parent());
          $input.val('');
        }
      }
    });

    $theElement.append($input.parent());
  }
})(jQuery);

That’s it. I hope you enjoy and love TDD more. And here’s the github repo

standard

The most basic example of Linear Regression

7.07.2015 Posted in Knowledges No Comments

Just for fun, I wanted to learn how to do linear regression and here’s the example I come up with.

Let’s say you have a historical data of 1000 people who dined in your restaurant and left a tip. This is going to be perfect data because I generated. In the real world you will not find something like this.

If you don’t understand Linear Regression like me before I wrote this post, I recommend you to read this basic linear regression..

The idea is that you have two variables. In this case, it’s tips and total amount of bill. You should explore the data by plotting the graph of these two variables. From my generated data you will get something like this.

Linear Regression

You can clearly see that there’s a strong correlation between the amount of tip and meal.

Now if you can find the slope of the graph and intercept you should be able to use the formula.

1
2
3
4
Y = MX + C

M = slope of the graph
C = Intercept

If you’re lazy to look at my notebook.

Then you can run this code.

1
2
3
4
5
6
7
8
9
10
11
12
13
import pandas as pd
import numpy as np
from scipy import stats

total_bills = np.random.randint(100, size=1000)
tips = total_bills * 0.10

x = pd.Series(tips, name='tips')
y = pd.Series(total_bills, name='total_bills')
df = pd.concat([x, y], axis=1)

slope, intercept, r_value, p_value, std_err = stats.linregress(x=total_bills, y=tips)
predicted_tips = (slope * 70) + intercept

The result is $7 which corresponds to the 10% tip.