Count the Number of Object keys/properties in Node.js

When using the excellent formidable library to handle file uploads, I needed to get a count of the number of files unloaded in a multi-part form. Javascript arrays have a .length property that you can use, but objects do not. I instinctively typed:files.length

Which returned undefined. So if there is no length property present, an easy way to count the number of keys or properties of an object in ES5-compliant javascript environments like Node.js is to use the Object prototype directly:

Object.keys(files).length

A little more typing, but it is fast, efficient, and most importantly: already built-in.

Titanium Mobile: How to Display an Image with Pinch-to-Zoom and Panning

There are many things that are not immediately obvious when working with Appcelerator’s Titanium platform. Displaying a large image with support for common gestures like pinch-to-zoom or panning around, for instance. If you are used to the Titanium platform and developing apps with it, you think it would be available in the events you can listen for – something like "twofingertouch" or similar that would have a dictionary of coordinates for each finger that is present on the screen.

is one of the most often posted questions in the Q&A section.

Self Employed

This post comes with a bit of a delay, as it is already three months into the year, but as of January 2011, I have been full time self-employed. I made the difficult decision to voluntarily leave an excellent job with Company52 at the beginning of the year to venture out on my own. Running  my own company with full-time focus has always been one of my dreams, and several side projects I have started and been involved with up until now have built up towards that goal.

There is a lot to consider in a decision like this, and I did not weigh it lightly. Somehow after running through all the options, now still seemed like the best time to take the leap. Here’s to a bright future in 2011 and beyond.

Listing Aliases Inside an Android Keystore File With Keytool

If you lose or forget your Android keystore file alias that is used to build APK files for distribution (like I did when trying to package Autoridge Lite for the Android Market), here is a quick and easy way to see them:

  1. Open a Terminal Window, Run This Command:

    keytool -list -keystore /location/of/your/com.example.keystore

    Make sure "keytool" is either in your PATH, or "cd" into the "tools" directory where your Android SDK files are.

  2. Enter your keystore password when prompted (you didn’t forget that too, did you? Did you?)
  3. See results! You should see something like the picture below if you did everything right. The alias is circled in yellow. If you have multiple aliases in your keystore, they will all be listed, one per line.

Continue reading

Why MVC is so Popular on the Web

The MVC design pattern has been getting a lot of attention in the past few years.  It seems like a new MVC framework pops up every week, and discussions about MVC have become commonplace throughout web programming communities.###MVC is Here to Stay

While MVC is certainly the "new thing" again (funny, because it actually dates back to 1979), it won’t be a passing fad, and it won’t fade quickly – at least not on the web.

Solving the Fundamental Problem of Web Development

Unlike most dektop applications which can be completely coded with one or two different programming languages, websites are a mix of several different programming languages that are constantly changing.  The typical website or web application in composed of no less than 6 different programming languages:

  • Server-side language like PHP/Ruby/Python/.NET, plus:
  • SQL – (Variants: MySQL, MSSQL, PgSQL, SQLite)
  • HTML/XHTML
  • CSS
  • JavaScript
  • XML (Plus specific formats like RSS and Atom and possibly XSLT)
  • JSON if the web application has an API
  • And who knows what else a few years from now…

As a result, the code naturally has to be separated somehow, and the same content has to be able to be displayed in many different formats (most commonly HTML plus XML and JSON for APIs).

Mirror Remote Server With Linux FTP Commands

A client of mine called me last night around 8:00pm a little worried. I had recently setup a hosting account for her on my server, so that she would be able to switch from her current FTP-only solution to a full hosting account with a domain and everything for when she makes a webstie in the future (she only needs the FTP to share files for now). On the phone, she said:>The guy who hosts my files just called me. He got in a disagreement with the guy who manages his servers, and told me to back everything up because it might not be there tomorrow

Wow. In a split second, all your data can be gone. The forever kind of gone. The problem was – and the reason she called me – was that she had amassed so many files over the years, that it would take days to backup using her internet connection, and she only had hours to get it done. Okay, relax, I told her – I’ve got it taken care of. I can use linux shell commands to download all the files to my server from yours. It will be much faster, and the files will go directly to the new server instead of having to be re-uploaded there, saving some very time-consuming steps.

Okay, I thought. I’ll just login, make a big tarball of all the files and grab that with my server. But her file hosting account did not allow shell access, and probably didn’t have the extra space for an additional tarball of all the files anyway. So I’m stuck with the linux ftp commands – or so I thought. Turns out, the mget ftp command does not recursively download folders on most servers. So the best function to use on a remote linux server that you can’t run shell commands on is wget, because wget also supports the FTP protocol. The usage goes like this:

wget -r ftp://user:pass@domain.com

That was going fine, and then the connection was cut-off by the remote server a short way through getting all the files, probably due to some data transfer cap or something. I re-started it, and it cut off again near the same place. So this isn’t enough either, and I still don’t want to do it manually. Luckily, there is a wget flag to ignore already existing files – ‘-nc’. So the whole command to download everything recursively and not re-download files you already have is:

wget -nc -r ftp://user:pass@domain.com

Remember to back up often. You never know when you might find yourself in a sudden and unexpected data loss situation, like Ma.Gnolia did Friday, January 30th. There’s a good discussion happening on the SitePoint open thread on data loss that same some good backup ideas and methods, too.

Interested in Coworking in Oklahoma?

okccoco_logoIf you’re in or around the Oklahoma City area doing freelance work, there’s some very good news for you.  Derrick Parkhurst along with a few members of the local community are opening OKCCoCo – Oklahoma City Coworking Collaborative.  What is Coworking? Coworking is essentially a movement to create a working office environment for freelancers and independent workers that they miss out on working from home.  Individuals will be able to rent a desk or private office to work at every month with a bunch or other really cool and smart people instead of working from home or signing a long-term lease for a real office and still being by themselves.  If you’re a freelancer or an independent worker that is interested in coworking, you should plan on attending the OPEN HOUSE to view the space on Saturday, January 17th at 2pm.

Details and registration are on the OKCCoCo website.

There will be a few additional announcements and activities at the open house, including a speech from Noah Everett, the founder of TwitPic. My own startup InvoiceMore will have a special announcement as well and a bonus for those who sign-up for OKCCoCo with a full or part-time commitment. Come out if you can, and don’t forget to register first! These are exciting times for the Oklahoma technology community!

PHP array_merge – Preserving Numeric Keys

This is just a quick post on PHP’s default behavior of re-indexing numeric keys when using PHP’s internal array_merge and array_merge_recursive functions, because it’s a problem I recently ran into, and was unable to find a quick solution to online.

Basically, the problem is that if you’re using numerically-indexed arrays with a set number that you don’t want to change (like an ID or some other unique identifier), you can’t use array_merge, because it automatically re-indexes all the numeric keys in the array to start with 0 on down in order.  There is no flag or option for the function to NOT do this, but there is another way to achieve the same result using PHP’s little-documented overridden plus operator ‘+’ for appending an array to another array.

So just replace this:

$destinationArray = array_merge($array1, $array2);

With this instead:

$destinationArray = $array1 + $array2;

Both $array1 and $array2 MUST be arrays or a fatal error will be thrown, so you may want to do some type checking or casting before that line of code. The difference is that instead of merging the arrays together, the second array will simply be appended to the first one with no changes.

Note that the plus operator for arrays ‘+’ is only one-dimensional, and is only suitable for simple arrays. If you need a multi-dimensional or complex solution, Keith Devens has a custom merge function that might work for you.

Get the Most From Your Google Analytics Dashboard

main_logoI’m a big fan of Google Analytics.  The service is free, can go on multiple websites using just one account, and displays and processes stats beautifully. But the one thing that’s always annoyed me about Google Analytics is the default dashboard setup when you create a new website profile.

The dashboard is the place for the most important things to be. it should be the single place you can view to and get an overview of all the most important things about your website regarding your visitors without having to drill deeper or go through multiple pages or sub-sections.  But the default dashboard Analytics starts you off with is all wrong, and is almost never the information I really want to see.  Let’s see how we can fix this. Continue reading