Sunday, April 24, 2011

Put prefix to a property

<script>


String.prototype.splice = function( idx, rem, s ) {
    // sourced from: http://stackoverflow.com/questions/4313841/javascript-how-can-i-insert-a-string-at-a-specific-index
    return (this.slice(0,idx) + s + this.slice(idx + Math.abs(rem)));
};

String.prototype.putPrefixToProperty = function(prefix) {
    ndx = -1;
    for(i = this.length - 1; i > 0; --i) {
        if (this[i] == '.') {
            ndx = i;
            break;
        }
    }    
    return this.splice(++ndx,0,prefix)        
}


alert("ProductId".putPrefixToProperty("txt_"));
alert("PurchasedDto.ProductId".putPrefixToProperty("txt_"));
alert("PurchasedDto.LineItem.ProductId".putPrefixToProperty("txt_"));

</script>

Output:
txt_ProductId
PurchasedDto.txt_ProductId
PurchasedDto.LineItem.txt_ProductId

Trivia: Did you know you can copy alert's text(or any dialog for that matter) by pressing Ctrl+C

No comments:

Post a Comment

Post a Comment