Posts for: #Javascript

Faster JavaScript Trim

I’ve recently discovered the very interesting Steven Levithan post about the JavaScript’s trim function of the String class.

So, here my version:
function trim13 (str) { var ws = /\s/, _start = 0, end = str.length; while(ws.test(str.charAt(_start++))); while(ws.test(str.charAt(--end))); return str.slice(_start - 1, end + 1); }

More numbers, please!

I tested my function against the benchmarking page of the original post. Note: times are expressed in MS and are the average of ten executions per browser. Update [2008-05-29]: I re-runned all the tests and updated results, because Steven noticed that test suite was wrong (few whitespaces).

[]

Make your elements draggable and resizeable with Resizeable.js

Hello, I’m still alive and I want to share a useful JavaScript class: Resizeable.js. It allows to make draggable and resizeable your DOM elements!

How it works?

new Resizeable('element');

Your element has now two sensible areas: the border area and the central area. If you click on the central area and move the mouse around, keeping the left button pressed, your element will be dragged. If you are on the border area, and perform the same gesture, your element will be resized.

[]

ActiveForm 0.2.0 released

ActiveForm is a JavaScript library, based on Prototype, useful to validate your forms in an automagically and pluggable way.

Automagically? Simply, You have to declare your fields, eventually your error messages, and.. nothing else!!

Pluggable? ActiveForm contains most common validations (regular expressions, presence, conditional presence..), and it performs them in a certain order. If you don’t like this, you can simply redeclare a method, or exclude it from validation process.

Features:

[]

Javascript HashMap

Reading around i’ve discovered that Javascript associative arrays returns unpredictable results, i.e. length param is not correctly handled.

Array object should be used only with a numeric index and best way to avoid this problem is to use Object.

But, if I wanna put more objects in my object?

Of course i can do this with previous method, but i don’t like it :-P. So, i’ve written a 100% OOP class, that use Prototype and it simulate a java HashMap.

[]