This repository has been archived by the owner on Jun 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
namespace.js
119 lines (105 loc) · 3.21 KB
/
namespace.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//let namespaceWarningEmitted = false;
/**
* Namespace creation, manipulation and traversal utility. This utility is used
* to create semi-global shared namespaces for registering references to
* interfaces, classes and constants of the application to provide access to
* each other more easily than by using the ES6 import/export mechanism.
*
* @deprecated
*/
export class Namespace {
/**
* Initializes the namespace provider.
*
* This is a private constructor, you should use the exported {@code ns}
* instance to create and use namespaces (see the examples).
*
* @private
* @example
* import ns from 'ima/namespace/ns.js';
* ns.namespace('ima');
* ns.has('ima');
*/
constructor() {}
/**
* Verifies that the specified path in namespace exists, creates it if it
* does not, and returns the value at the specified path in the namespace.
*
* The method recursively creates all path parts in the namespaces as empty
* plain objects for all path parts that do not exist yet, including the
* last one. This means, that if called with a non-existing namespace path
* as an argument, the return value will be the last created namespace
* object.
*
* @deprecated
* @param {string} path The namespace path.
* @return {*} The value at the specified path in the namespace.
*/
namespace(path) {
/*if (
(typeof $Debug !== 'undefined') &&
$Debug &&
/^app./i.test(path) &&
!namespaceWarningEmitted
) {
console.warn(
'DEPRECATION WARNING: Your application seems to be using ' +
`namespaces (attempted to create the ${path} namespace), ` +
'but namespaces were deprecated since IMA 0.12.0. Please ' +
'switch to ES6 imports as the support for namespaces will ' +
'be removed in an upcoming version of IMA.js.'
);
namespaceWarningEmitted = true;
}*/
let self = this;
let levels = path.split('.');
for (let levelName of levels) {
if (!Object.prototype.hasOwnProperty.call(self, levelName)) {
self[levelName] = {};
}
self = self[levelName];
}
return self;
}
/**
* Verifies that the specified namespace path point to an existing
* namespace or terminal value.
*
* @param {string} path The namespace path to test.
* @return {boolean} {@code true} if the namespace or terminal value exists
* at the specified path.
*/
has(path) {
return typeof this.get(path) !== 'undefined';
}
/**
* Return value for the specified namespace path point.
*
* @param {string} path The namespace path to test.
* @return {*} The value at the specified path in the namespace.
*/
get(path) {
let self = this;
let levels = path.split('.');
for (let level of levels) {
if (!self[level]) {
return undefined;
}
self = self[level];
}
return self;
}
/**
* Set value for the specified namespace path point.
*
* @param {string} path The namespace path to set.
* @param {*} value
*/
set(path, value) {
let levels = path.split('.');
const lastKey = levels.pop();
let namespace = this.namespace(levels.join('.'));
namespace[lastKey] = value;
}
}
export default new Namespace();