Posts

Showing posts with the label String

About JavaScript apply arguments limit

Just a quick one from ECMAScript ml ... it is true that browsers may have a limited number of arguments per function. This may be actually a problem, specially when we use apply to invoke a generic function that accepts arbitrary number of arguments. String.fromCharCode This is a classic example that could fail with truly big collection of char codes and here my suggestion to avoid such limit: var fromCharCode = (function ($fromCharCode, MAX_LENGTH) { // (C) WebReflection - DO THE FUCK YOU WANT LICENSE return function fromCharCode(code) { typeof code == "number" && (code = [code]); for (var result = [], i = 0, length = code.length; i ) { result.push($fromCharCode.apply(null, code.slice(i, i + MAX_LENGTH))); } return result.join(""); }; }(String.fromCharCode, 2048)); // example alert(fromCharCode(80)); // P alert(fromCharCode([80, 81, 82, 83, 84])...

String Escape Safe Regular Expression

I should have probably investigated more but apparently I did it ... the most problematic I've encountered so far with JavaScript RegExp seems to be solved! Update Indeed, I should have investigated ... I just like to find solutions by my own. I am not surprised somebody already investigated this classic parsing problem. Steve talked about it a year ago, using the lookbehind missed feature I talked in this post. Above post has much more details than mine (and much more Edits as well). The good part I am happy about is that both me and Steve came out with basically the same solution, but His one is definitively more compact: // Steves Levithan compact solution /(["'])(?:(?=(\\?))\2.)*?\1/g The assumption of above regexp is that if there is a char followed by an escape one, there must be another char that cannot be the initial single or double quote, being the latter one outside the second un captured part, and after a non greedy operation. If the second condition, \2, does...

ECMAScript 5 Full Specs String trim, trimLeft, and trimRight

During last evenings I have updated a little bit my vice-versa project . Since vice-versa aim is to bring in every browser what is possible to implement and, in most of the cases, already defined as standard (from W3 or MSDN when it is worthy) I decided to get rid of the Ariel Flesler fast trim proposal to introduce my lightweight full specs String.prototype.trim, trimLeft, and trimRight. For full specs I mean that vice-versa String.prototype.trim replace exactly same characters replaced by native Firefox 3.5 implementation, rather than only characters which code is less than 33 as is for Ariel proposal. The good part of vice-versa ( to be honest I cannot find bad parts so far ;) ) is that every single file is stand-alone, so if you do not like benefits the entire " lib " could bring, you can always adopt only one of its files, for example the String one, the Array one, or the last full specs ECMAScript 5 Date constructor , compatible with ISO strings, new Date("2009-0...

After the Array, subclassed String

As I wrote months ago in this documentation about JavaScript prototypal inheritance , subclass native data type is not that simple (almost not possible at all) but since JS is loads of weird bits and bops, I often try to break its documented limits. It was the Array , some time ago, now it is about time for String. The concept is similar, if you subclass a native data type its methods will return that native data type like instance, so concat, charAt, toString, etc etc, will return a String unless we do not override every inherited method (so performances wont be that good). Subclassed String (function(Function, slice, push){ // from WebReflection: Subclassed String function String(String){ if(arguments.length) // clever constructor, accepts more than a string as argument push.apply(this, slice.call(arguments).join("").split("")); }; String.prototype = new Function; try{ (new String) + ""; // exception in FireFox var join = Array.proto...

Subclassing JavaScript Native String

Just a quick post about subclassing native String constructor. All we need is to redefine valueOf and toString methods. function $String(__value__){ this.length = (this.__value__ = __value__ || "").length; }; with($String.prototype = new String) toString = valueOf = function(){return this.__value__}; With incoming V8, TraceMonkey, Squirrelfish, and other (if any ...) advanced engines that transforms repeated code into machine one, performances wont be a problem anymore and everybdy could create its own implementation of the String. Of course, these statements will be preserved: var s = new $String("abc"); s instanceof String; // true s.constructor === String; // true while typeof will return an object but we can easily use another method such: $String.prototype.type = function(){ return "string"; }; alert(typeof s); // object alert(s && s.type()); // string concat as other native methods willl work, but returned object, unfortunatly, won...