defineNamespace.js
1.46 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* @fileoverview Define namespace
* @author NHN.
* FE Development Lab <dl_javascript@nhn.com>
* @dependency object.js, collection.js
*/
'use strict';
var collection = require('./collection');
var object = require('./object');
/**
* Define namespace
* @param {string} namespace - Namespace (ex- 'foo.bar.baz')
* @param {(object|function)} props - A set of modules or one module
* @param {boolean} [isOverride] - Override the props to the namespace.<br>
* (It removes previous properties of this namespace)
* @returns {(object|function)} Defined namespace
* @memberof tui.util
* @example
* //-- #1. Get Module --//
* var util = require('tui-code-snippet'); // node, commonjs
* var util = tui.util; // distribution file
*
* //-- #2. Use property --//
* var neComp = util.defineNamespace;
* neComp.listMenu = defineClass({
* init: function() {
* // ...
* }
* });
*/
function defineNamespace(namespace, props, isOverride) {
var names, result, prevLast, last;
names = namespace.split('.');
names.unshift(window);
result = collection.reduce(names, function(obj, name) {
obj[name] = obj[name] || {};
return obj[name];
});
if (isOverride) {
last = names.pop();
prevLast = object.pick.apply(null, names);
result = prevLast[last] = props;
} else {
object.extend(result, props);
}
return result;
}
module.exports = defineNamespace;