-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path030-Namespace.js
More file actions
41 lines (37 loc) · 1.21 KB
/
030-Namespace.js
File metadata and controls
41 lines (37 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
Script Name: Javascript Namespace Script
Author: Michael J. Ryan (http://tracker1.info)
Public Domain
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
//console.debug("begin namespace.js");
(function(root){
//regular expression to limit formatting of namespaces
var nsre = /^([\$\_a-z][\$\_a-z\d]*\.?)+$/i
//define returned function
this.namespace = function(ns) {
var args = Array.prototype.slice.call(arguments);
var ret = [];
while (args.length) {
ns = genNS(args.shift());
if (ns) ret.push(ns);
}
if (ret.length == 0) return; //undefined, no valid input
if (arguments.length == 1) return ret[0]; //only a single input, return that namespace
return ret; //used overload for multiple namespaces, return the array/list
}
//private static method to generate a single namespace
function genNS(ns) {
if (!ns.match(nsre)) return;
ns = ns.split('.');
var base = root;
for (var i=0; i<ns.length; i++) {
base[ns[i]] = base[ns[i]] || {};
base = base[ns[i]];
}
return base; //return resulting namespace object
}
}(this));
//console.debug("end namespace.js");