﻿function namespace(spec, context) {
    var validIdentifier = /^(?:[a-zA-Z_]\w*[.])*[a-zA-Z_]\w*$/,
        i, N;
    context = context || window;
    if (typeof spec === 'object') {
        if (typeof spec.length === 'number') {//assume an array-like object
            for (i = 0, N = spec.length; i < N; i++) {
                namespace(spec[i], context);
            }
        }
        else {//spec is a specification object e.g, {com: {trifork: ['model,view']}}
            for (i in spec) if (spec.hasOwnProperty(i)) {
                context[i] = context[i] || {};
                namespace(spec[i], context[i]); //recursively descend tree
            }
        }
    } else if (typeof spec === 'string') {
        (function handleStringCase() {
            var parts;
            if (!validIdentifier.test(spec)) {
                throw new Error('"' + spec + '" is not a valid name for a package.');
            }
            parts = spec.split('.');
            for (i = 0, N = parts.length; i < N; i++) {
                spec = parts[i];
                context[spec] = context[spec] || {};
                context = context[spec];
            }
        })();
    }
    else {
        throw new TypeError();
    }
}

function using() {
    var args = arguments;
    return { run: function(inner) { return inner.apply(args[0], args); } };
}


