About the author

Miron Abramson
Me
Software Engineer,
CTO at PixeliT
and .NET addicted for long time.
Open source projects:
MbCompression - Compression library

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2009

Creative Commons License

Blog Flux Directory
Technology Blogs - Blog Top Sites

Some basic but useful C# methods implemented in JavaScript

While I was working on a full Ajax interface based on pure ajax calls and not on any framework,  I implemented some basic and simple methods that we use all the time in the server code, and just not exist in javascript. It's not big deal, but since I wrote them, I use them all the time.

Even it is very simple, I decided to share it here. Hope it will save other coders few minutes:

Cache object:

//
//  Cach object implementation
//
var Cache = new function()
{
    var _cache = new Array();
    this.Insert = function ( key, value ){
        _cache[key] = value;
        }
    this.Get = function ( key ){
        return _cache[key];
        }
    this.Contains = function ( key ){
        if( !_cache[key] || _cache[key] == null ) return false;
        else return true;
        }
};

The use of the cache is as simple as it is in the server side, and it is realy useful to save some calls to the server:

 Cache.Insert("myKey","MyValue");
 if( Cache.Contains("myKey") ) alert( "Yeee the value of my key is in the cache:" + Cache.Get("myKey") );  

String.Format(string,params)  &  String.IsNullOrEmpty(string) :

//
// String.Format implementation
//
String.Format = function(format,args){
    var result = format;
    for(var i = 1 ; i < arguments.length ; i++) {
        result = result.replace(new RegExp( '\\{' + (i-1) + '\\}', 'g' ),arguments[i]);
    }
    return result;
}

//
// String.IsNullOrEmpty implementation
//
String.IsNullOrEmpty = function(value){
    if(value){
        if( typeof( value ) == 'string' ){
             if( value.length > 0 )
                return false;
        }
       if( value != null )
           return false;
    }
    return true;
}

Again, the use is the same as server side:

alert( String.Format("Hello {0}. Yes, hello {0} again. My name is {1}","world","Miron") );
if( String.IsNullOrEmpty('') ) alert('Empty string');

StartsWith(string suffix,bool ignoreCase)EndsWith(string suffix,bool ignoreCase)  and Trim() :

//
// string.StartWith implementation
//
String.prototype.StartsWith = function(prefix,ignoreCase) {
    if( !prefix ) return false;
    if( prefix.length > this.length ) return false;
    if( ignoreCase ) {
        if( ignoreCase == true ) {
            return (this.substr(0, prefix.length).toUpperCase() == prefix.toUpperCase());
        }
    }
    return (this.substr(0, prefix.length) === prefix);
}

 //
// string.EndsWith implementation
//
String.prototype.EndsWith = function(suffix,ignoreCase) {
    if( !suffix ) return false;
    if( suffix.length > this.length ) return false;
    if( ignoreCase ) {
        if( ignoreCase == true ) {
            return (this.substr(this.length - suffix.length).toUpperCase() == suffix.toUpperCase());
        }
    }
    return (this.substr(this.length - suffix.length) === suffix);
}

 //
// string.Trim implementation
//
String.prototype.Trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
}

The last three are working on an istance of a string:

var test = "Hello Words ";
test = test.Trim();
var end = test.EndsWith("ds",true);
var begin = test.BeginsWith("rr",false);

All the code can be downloaded here: 

Utils.js (1.94 kb)

Currently rated 4.7 by 7 people

  • Currently 4.714286/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Client side
Posted by Miron on Sunday, May 04, 2008 4:43 PM
Permalink | Comments (4) | Post RSSRSS comment feed

Related posts

Comments

BarDev us

Wednesday, September 17, 2008 10:26 AM

BarDev

Why is it "String.IsNullOrEmpty = function(value){" instead of "String.prototype.IsNullOrEmpty = function(value){"?



Thanks.

Miron il

Wednesday, September 17, 2008 11:17 PM

Miron

@BarDev, in cases your variable is null or not from type 'String'.
Take the following code:
var hello = '';
hello = null;
If you use String.prototype.IsNullOrEmpty, hello.IsNullOrEmpty(); will not work.
If you use String.IsNullOrEmpty, this will work: String.IsNullOrEmpty(hello);

botas es

Saturday, May 09, 2009 7:16 AM

botas

I tiyed the code but its giving me problems... I think this part its my problem:

String.IsNullOrEmpty = function(value){
if(value){
if( typeof( value ) == 'string' ){
if( value.length > 0 )
return false;
}
if( value != null )
return false;
}
return true;

whe I check the code with the tipical test from testing pages this part appiared in red and I dont know what i can do....

some help?

huangqin cn

Wednesday, July 01, 2009 12:18 PM

huangqin

<p><a href="http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG</a><br>" rel="nofollow">www.uggsale.org/">UGG</a><br>
<a href="http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG sale</a><br>
<a href="www.uggsale.org/ugg-classic-cardy-c-2.html UGG Boots</a><br>
<a href="www.uggsale.org/ugg-classic-cardy-c-2.html UGG Boots</a><br>
<a href="www.uggsale.org/...et-c-10.html">Authentic UGG Boots</a><br>
<a href="http://www.uggsale.org/">Genuine UGG Boots</a><br>
<a href="http://www.uggsale.org/">Origianl UGG Boots</a><br>
<a href="www.uggsale.org/...ghtfall-c-4.html">Women's Ugg</a><br>
<a href="www.uggsale.org/...sic-short-c-1.html">Men's Ugg</a><br>
<a href="www.uggsale.org/...ssic-tall-c-3.html">UGG" rel="nofollow">www.uggsale.org/...ssic-tall-c-3.html">UGG Australia</a><br>
<a href="http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG Store</a><br>
<a href="http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG Official Store</a><br>
<a href="http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG Shop</a> <br>
<a href="http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG" rel="nofollow">http://www.uggsale.org/">UGG Online Store</a><br>
<a href="www.uggsale.org/...-slipper-c-25.html">UGG" rel="nofollow">www.uggsale.org/...-slipper-c-25.html">UGG Boots</a><br>
<a href="www.uggsale.org/...1010309-p-995.html">UGG Boot</a><br>
<a href="www.uggsale.org/...sandal-p-1010.html">UGG Sandal</a><br>
<a href="www.uggsale.org/...-sandal-p-991.html">UGG Sandals</a><br>
<a href="www.uggsale.org/...nd-1704-p-998.html">UGG Slipper</a><br>
<a href="www.uggsale.org/...k-5114-p-1031.html">UGG Slippers</a><br>
<a href="www.uggsale.org/...sic-cardy-c-2.html">UGG Classic Cardy</a><br>
<a href="www.uggsale.org/...-crochet-c-10.html">UGG Classic Crochet</a><br>
<a href="www.uggsale.org/...ssic-mini-c-6.html">UGG Classic Mini</a><br>
<a href="www.uggsale.org/...sic-short-c-1.html">UGG Classic Short</a><br>
<a href="www.uggsale.org/...ssic-tall-c-3.html">UGG" rel="nofollow">www.uggsale.org/...ssic-tall-c-3.html">UGG Classic Tall</a><br>
<a href="www.uggsale.org/...nightfall-c-4.html">UGG Nightfall</a><br>
<a href="www.uggsale.org/...tra-short-c-7.html">UGG Ultra Short</a><br>
<a href="www.uggsale.org/...ltra-tall-c-8.html">UGG Ultra Tall</a><br>
<a href="www.uggsale.org/ugg-tasmina-c-11.html">UGG Tasmina</a><br>
<a href="www.uggsale.org/...ie-suede-c-19.html">UGG Amelie Suede</a><br>
<a href="www.uggsale.org/...y-sandal-c-18.html">UGG Gypsy Sandal</a><br>
<a href="www.uggsale.org/...a-sandal-c-22.html">Ugg Matala Sandal</a><br>
<a href="www.uggsale.org/...i-sandal-c-17.html">UGG Halendi Sandal</a><br>
<a href="www.uggsale.org/...-slipper-c-20.html">UGG Hammond Slipper</a><br>
<a href="www.uggsale.org/...-slipper-c-25.html">UGG" rel="nofollow">www.uggsale.org/...-slipper-c-25.html">UGG Layback Slipper</a><br>
<a href="www.uggsale.org/...e-sandal-c-21.html">Ugg Napoule Sandal</a><br>
<a href="www.uggsale.org/...e-sandal-c-24.html">Ugg Persephone Sandal</a><br>
<a href="www.uggsale.org/ugg-skimmer-c-23.html">Ugg Skimmer
</a></p>

Add comment


(Will show your Gravatar icon)  

  Country flag





Live preview

Friday, July 03, 2009 9:58 PM