Sunday, January 13, 2013

Extension methods in TypeScript

/* 18/1 2013Update Remember Extension methods are code smell, so use it only where it really make sense.
Good blog post about Extension methods why its code smell http://www.daedtech.com/why-i-dont-like-c-extension-methods   
*/

When working with strings it’s likely that you’ve had to check if the string is null or empty, and
Thats no different in TypeScript.

I thougt of making an extension method for StringIsNullOrEmpty, but could not fine any documentation on this.

I know that TypeScript interfaces are open ended, so I extended the String interface like this

    interface String {
        StringIsNullOrEmpty: () => bool;
    }


Then I just need to found out how to inplement it, and used prototype for this.

    String.prototype.StringIsNullOrEmpty = () : bool{
        return this == null || this.length == 0;
    }


This works fine, but I'm not sure this is the correct way of doing this, so if you know a better way please leave a comment.

I have encapsulated the inteface and all my prototypes into a module called StringExtensions

module StringExtensions {
    interface String {
        StringIsNullOrEmpty: () => bool;
    }

    String.prototype.StringIsNullOrEmpty = () : bool{
        return this == null || this.length == 0;
    }
}


Here is a litle jasmine test.

describe("StringExtensions methods tests", () => {

    it("Given an empty string should return true", () => {

        // Fixture setup       

        var sut = "";

        // Exercise system               

        var expected = true;

        // Verify outcome

        expect(sut.StringIsNullOrEmpty()).toEqual(expected);

        // Teardown

    });   
});

22 comments:

  1. i have also this functions:

    interface String {
    formatWith(...args: string[]): string;
    formatWith(...args: any[]): string;
    }

    and JS part:

    String.prototype.formatWith = function () {
    var params = $.makeArray(arguments);
    return $.validator.format(this, params);
    };

    ReplyDelete
  2. I tested your method but it fails when string variable is null, it throws:
    Cannot call method 'StringIsNullOrEmpty' of null. I think this cannot be implemented in JS.

    ReplyDelete
    Replies
    1. Hi Peter,

      Thanks for the comment, maybe something has change since I wrote this post. I will try play with extension methods in typescript again :-)

      Delete
  3. Seems wrap the extension methods in a module doesn't work in angular cli environment.

    ReplyDelete
  4. Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
    python Training in Pune
    python Training in Chennai
    python Training in Bangalore

    ReplyDelete
  5. It won't work if you send a NULL

    ReplyDelete
  6. Thank you for taking the time and sharing this information with us. It was indeed very helpful and insightful while being straight forward and to the point.
    Best Devops Training in pune
    Devops Training in Bangalore
    Power bi training in Chennai

    ReplyDelete
  7. Thanks For Sharing The information The Information Shared Is Very Valuable Please Keep Updating us The Information shared Is Very Valuable Python Online Course Data Science Online Course Aws Online Course

    ReplyDelete
  8. Nice Blog, Thanks For Sharing Your information Please keep updating us. The Information you Shared Is Very valuable, keep it up!! Machine Learning Training in Bangalore

    ReplyDelete
  9. Attend The Data Analytics Courses in Bangalore From ExcelR. Practical Data Analytics Courses in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analytics Courses in Bangalore.
    ExcelR Data Analytics Courses in Bangalore

    ReplyDelete
  10. After reading your blog, I was quite interested to learn more about this topic. Thanks. Machine Learning Training In Bangalore

    ReplyDelete
  11. Really it was an awesome article about JAVA, very interesting to read.You have provided an nice article,Thanks for sharing.
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete