PHP vs JS conditionals and ternary

Just a quick note about how PHP conditional operators differs from ECMA (JS and AS)… The double-pipe ‘OR’ operator, in JS, evaluated to the value of the expression that is not false. In PHP, it evaluates to true if any any expression is true, or false if not (the output will actually be “1″ if […]

JS/AS – Cache your RegExp!

In both JavaScript and ActionScript, creating a regular expression – even using literal notation – results in the instantiation of a new RegExp object. If you’re going to use a pattern repeatedly (which is usually the case) – cache your patterns! Just like you would any other object – “literal” doesn’t mean “free”. Simple test […]

JS/AS Date Object from MySQL DATE or DATETIME

The ECMAScript Date object accepts a string that it pretty generously tries to convert to a valid date. However, DATE and DATETIME returns from MySQL are formatted Y-M-D, which will cause new Date(str) or Date.parse(str) to barf. The quick fix is just to use a simple RegExp to move the year to the 3rd position. […]

RegExp – match JS/AS/PHP/CSS comments

The following pattern matches comments used in JavaScript, ActionScript, PHP, CSS, etc (/* comments */, and // comments), greedily.

AS/JS Translating Colors

just some snippets re: color values. number to hex string:

hex string to number:

RGBA to RGB:

individual colors from number:

number from individual colors:

RegExp – validate US Zip

Simple regular expression to validate US zip codes.

RegExp – validate US phone

A regular expression to validate US phone numbers. Works in all languages I’ve tested.

Flavored curry…

Here’s a few different takes on currying functions by extending the Function object, for JavaScript and ActionScript 2 (if using for ActionScript, use _global["Function"].prototype instead of Function.prototype).

Currying Function Arguments

Currying function arguments is the process of pre-defining arguments within a functor (a function that returns a function). A simple way to achieve this in JavaScript or ActionScript 2 is to extends the Function object:

(in ActionScript 2, the Function class isn’t directly available – you’ll need to change

to

Often, currying […]

Array + Random

(For JavaScript and ActionScript 2) To randomize an entire array:

To get a single random element:

function versions for the non-polluters…