Wrapping / Looping / Infinite Array Traversal

I’ll use JavaScript syntax and provide a JS fiddle POC, but this really applies to almost any language – at least any that use indexed arrays (all that I can think of). To get an index greater than length to “wrap” back from the beginning, use a simple modulus:

To get a negative index […]

jQuery (or anything else) basics of dragging

Here’s the most basic implementation of dragging I’m aware of… I’ll present an example usin jQuery, but the same logic can be used in any JS framework, raw JS, or even ActionScript, Java, or any other language or technology that processes similar user events and provided a UI. Here’s the idea: On mousedown: 1. record […]

JavaScript – remove multiple elements from an array

While you can always .filter() an array to remove unwanted elements, here’s a little sugar to remove all elements that evaluate truthily when compared to any argument passed:


POC: http://jsfiddle.net/moagrius/yssfv/ Non-polluting:

JavaScript – splice array on multiple indices (multisplice)

Managing multiple splices in a loop can get confusing, as the position of each element changes after each splice operation. Here’s a little extension to help manage that:

The trick is in the call to sort() and the reduction of the index by the iteration count, which updates the originally passed index to match […]

JavaScript – separate (split) array

I often have a list from the server that I need to split into 2 separate arrays. For example, all options that are enabled and all that are not. Here’s a little extension to handle that. Takes a single callback as an argument, with map-style params (value, index, array) – if that iterated callback returns […]

Cache “nudger”

With web technologies generally, and flash specifically, managing cached assets can sometimes feel like walking a tightrope. If you’ve got a simple slideshow and have occasional updates to the image you want immediately reflected to the client, you’ll often see traditional “cache-busting” methods like appending a random number of the current UTC time to the […]

Understanding Easing (Explaining Penner’s equations) – JavaScript and ActionScript

Despite the commonality of the classic easing equations, largely attributed to Penner, there doesn’t seem to be the in-depth examination of “how it works” that a lot of code is subject to nowadays. First, a quick clarification on the most popular easing Classes. The “standard” easing classes (that provide predictable inertia), are Linear, Quad, Cubic, […]

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 Functions

A few date manipulation functions for JS/AS… Convert a MySQL DATE or DATETIME field into a valid JS/AS Date object:

UPDATE: Apparently FireFox can’t handle dashes when parsing the date. In the above replacement, use slashes instead:

Convert a JS Date Object to a MySQL formatted string

Get amount of time between […]