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 }); });
i have also this functions:
ReplyDeleteinterface 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);
};
I tested your method but it fails when string variable is null, it throws:
ReplyDeleteCannot call method 'StringIsNullOrEmpty' of null. I think this cannot be implemented in JS.
Hi Peter,
DeleteThanks for the comment, maybe something has change since I wrote this post. I will try play with extension methods in typescript again :-)
Seems wrap the extension methods in a module doesn't work in angular cli environment.
ReplyDeleteAwesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
ReplyDeletepython Training in Pune
python Training in Chennai
python Training in Bangalore
It won't work if you send a NULL
ReplyDeleteThank 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.
ReplyDeleteBest Devops Training in pune
Devops Training in Bangalore
Power bi training in Chennai
Nice post. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated posts…
ReplyDeleteData Science training in chennai
Data Science training in OMR
Data Science training in chennai
Data Science Training in Chennai
Data Science training in Chennai
Data science training in bangalore
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
ReplyDeleteNice 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
ReplyDeleteAttend 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.
ReplyDeleteExcelR Data Analytics Courses in Bangalore
After reading your blog, I was quite interested to learn more about this topic. Thanks. Machine Learning Training In Bangalore
ReplyDeleteReally it was an awesome article about JAVA, very interesting to read.You have provided an nice article,Thanks for sharing.
ReplyDeleteJava training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging. DevOps Training | Certification in Chennai | DevOps Training | Certification in anna nagar | DevOps Training | Certification in omr | DevOps Training | Certification in porur | DevOps Training | Certification in tambaram | DevOps Training | Certification in velachery
ReplyDeleteMmorpg oyunları
ReplyDeleteInstagram takipçi satın al
Tiktok jeton hilesi
TİKTOK JETON HİLESİ
Saç ekim antalya
referans kimliği nedir
İnstagram Takipçi Satın Al
metin2 pvp serverlar
İNSTAGRAM TAKİPÇİ
en son çıkan perde modelleri
ReplyDeleteminecraft premium
nft nasıl alınır
yurtdışı kargo
uc satın al
lisans satın al
özel ambulans
en son çıkan perde modelleri
Good content. You write beautiful things.
ReplyDeletehacklink
korsan taksi
sportsbet
mrbahis
mrbahis
sportsbet
vbet
vbet
taksi
betmatik
ReplyDeletekralbet
betpark
mobil ödeme bahis
tipobet
slot siteleri
kibris bahis siteleri
poker siteleri
bonus veren siteler
1AT
kocaeli
ReplyDeletekonya
kuşadası
kütahya
malatya
E3M7R
manisa
ReplyDeletemaraş
mardin
marmaris
mersin
ZEKYİ
erzurum
ReplyDeletekuşadası
ümraniye
beşiktaş
ataşehir
71XKHW
kağıthane
ReplyDeletemuğla
tuzla
etiler
maltepe
CXULGW