Last active
March 1, 2018 15:04
-
-
Save tzengerink/3798895 to your computer and use it in GitHub Desktop.
Desire.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* Desire.js | |
* --------- | |
* | |
* Create the namespaces you so desire. | |
* | |
* // Create a new module | |
* desire("APP.MyModule.MySubModule", function(){ | |
* return { | |
* myProp : "Your property value", | |
* myMethod : function( arg1, arg2 ){ | |
* // Your method contents | |
* } | |
* }; | |
* }); | |
* | |
* // Create a single method | |
* desire("APP.mySingleMethod", function(){ | |
* return function( arg1, arg2 ){ | |
* // Your method contents | |
* }; | |
* }); | |
* | |
* Copyright (c) 2013, T. Zengerink | |
* Licensed under MIT License | |
* See: https://gist.github.com/raw/3151357/6806e68cb9cc0042b265f25be9bc25dd39f75267/LICENSE.md | |
*/ | |
var desire = (function(){ | |
var nsDelimiter = "."; | |
var extend = function( src, des ){ | |
for (var key in des) { | |
src[key] = des[key]; | |
} | |
return src; | |
}; | |
var getModuleName = function( ns ){ | |
return ns.split(nsDelimiter).pop(); | |
}; | |
var getNameSpace = function( ns ){ | |
var arr = ns.split(nsDelimiter); | |
arr.pop(); | |
return factory(arr.join(nsDelimiter)); | |
}; | |
var factory = function( ns ){ | |
var i = 0, | |
s = ns.split(nsDelimiter), | |
o = this; | |
for (; i < s.length; i++) { | |
o[s[i]] = o[s[i]] || {}; | |
o = o[s[i]]; | |
} | |
return o; | |
}; | |
return function( ns, fn ){ | |
var moduleName = getModuleName(ns), | |
nameSpace = getNameSpace(ns), | |
module = fn.call(this, nameSpace, moduleName); | |
switch (typeof module) { | |
case "object": | |
nameSpace[moduleName] = extend((nameSpace[moduleName] || {}), module) | |
break; | |
default: | |
nameSpace[moduleName] = module; | |
break; | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment