“I'm an experienced web developer and software engineer. Welcome to my blog. If you need to reach out you can continue the conversation with a tweet to @geedew. I hope you find what you are looking for here. You can also find me on Github and StackOverflow.”

Trimming Strings; Stripping Text in JavaScript

Strings are one of the most basic structures in any development language. Developing with strings happens in common ways across languages. Sometimes languages don’t offer the common gamut of tools and leave the developer to implement their own methods. One of these cases occurs with a proper strip method in JavaScript. JavaScript added the String.Trim[1] with EcmaScript5, which is a specialized version of a generic strip, so that leaves developers to implement it themselves.

Read More

Remove a directory that is not empty in NodeJS

A quick pro-tip for handling this situation.

NodeJS provides an easy to use fs.rmdir command that follows the POSIX standard. This unfortunately means that it will error ENOTEMPTY if there is any file in the directory you are attempting to remove. NodeJS doesn’t have an easy way to force the removal, so you have to get fancy.

By far, the easiest, safest and most cross environment approach is to use rimraf whose source code shows nearly 250 lines of premium quality work.

But doing it yourself is easy and a valid option. The following is a synchronous way to handle the deletion of a directory that may not be empty.

var fs = require('fs');
var deleteFolderRecursive = function(path) {
  if( fs.existsSync(path) ) {
    fs.readdirSync(path).forEach(function(file,index){
      var curPath = path + "/" + file;
      if(fs.lstatSync(curPath).isDirectory()) { // recurse
        deleteFolderRecursive(curPath);
      } else { // delete file
        fs.unlinkSync(curPath);
      }
    });
    fs.rmdirSync(path);
  }
};

I posted this solution on Stack Overflow as well.

I’ve further added an Asyncronous method for removing the directories. While it’s more code (and that means possibly brittle), and I’m afraid I haven’t battle tested it as much, it’s seemingly twice as fast in my rudimentary testing (However, all sorts of factors can affect a test like that).

var fs = require('fs');
var rmdirAsync = function(path, callback) {
    fs.readdir(path, function(err, files) {
        if(err) {
            // Pass the error on to callback
            callback(err, []);
            return;
        }
        var wait = files.length,
            count = 0,
            folderDone = function(err) {
            count++;
            // If we cleaned out all the files, continue
            if( count >= wait || err) {
                fs.rmdir(path,callback);
            }
        };
        // Empty directory to bail early
        if(!wait) {
            folderDone();
            return;
        }

        // Remove one or more trailing slash to keep from doubling up
        path = path.replace(/\/+$/,"");
        files.forEach(function(file) {
            var curPath = path + "/" + file;
            fs.lstat(curPath, function(err, stats) {
                if( err ) {
                    callback(err, []);
                    return;
                }
                if( stats.isDirectory() ) {
                    rmdirAsync(curPath, folderDone);
                } else {
                    fs.unlink(curPath, folderDone);
                }
            });
        });
    });
};

Advanced button theming on jQuery UI Dialog - Update

Since my previous post, jQueryUI has updated as well as jQuery itself. The code is largely the same, but they did implement some of the usefulness that I proposed. Essentially, they added the ability to pass the name of the button as text, rather then as the key to a key->value pair object.

Read More

Unicode in Javascript: Closure Compiler blunders

1
> Uncaught SyntaxError: Unexpected token ,

When coding in JavaScript, as an english speaking citizen, unicode is rarely something that I think of. Recently though, I attempted to upgrade an Underscore package to version 1.3.3. This didn’t turn out as well as I hoped, I was immediately met with errors in the Javascript caused by using Closure Compiler in UTF charset mode. This could be avoided in the Underscore code, but it’s not entirely something they are to blame for. It has more to do with encoding a javascript file that has unicode literals in the code. When this is done, the u sequence becomes encoded and could possibly break your code, as I will demonstrate.

Read More