UNPKG

3.36 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6//let namespaceWarningEmitted = false;
7
8/**
9 * Namespace creation, manipulation and traversal utility. This utility is used
10 * to create semi-global shared namespaces for registering references to
11 * interfaces, classes and constants of the application to provide access to
12 * each other more easily than by using the ES6 import/export mechanism.
13 *
14 * @deprecated
15 */
16class Namespace {
17 /**
18 * Initializes the namespace provider.
19 *
20 * This is a private constructor, you should use the exported {@code ns}
21 * instance to create and use namespaces (see the examples).
22 *
23 * @private
24 * @example
25 * import ns from 'ima/namespace/ns.js';
26 * ns.namespace('ima');
27 * ns.has('ima');
28 */
29 constructor() {}
30
31 /**
32 * Verifies that the specified path in namespace exists, creates it if it
33 * does not, and returns the value at the specified path in the namespace.
34 *
35 * The method recursively creates all path parts in the namespaces as empty
36 * plain objects for all path parts that do not exist yet, including the
37 * last one. This means, that if called with a non-existing namespace path
38 * as an argument, the return value will be the last created namespace
39 * object.
40 *
41 * @deprecated
42 * @param {string} path The namespace path.
43 * @return {*} The value at the specified path in the namespace.
44 */
45 namespace(path) {
46 /*if (
47 (typeof $Debug !== 'undefined') &&
48 $Debug &&
49 /^app./i.test(path) &&
50 !namespaceWarningEmitted
51 ) {
52 console.warn(
53 'DEPRECATION WARNING: Your application seems to be using ' +
54 `namespaces (attempted to create the ${path} namespace), ` +
55 'but namespaces were deprecated since IMA 0.12.0. Please ' +
56 'switch to ES6 imports as the support for namespaces will ' +
57 'be removed in an upcoming version of IMA.js.'
58 );
59 namespaceWarningEmitted = true;
60 }*/
61
62 let self = this;
63 let levels = path.split('.');
64
65 for (let levelName of levels) {
66 if (!self.hasOwnProperty(levelName)) {
67 self[levelName] = {};
68 }
69
70 self = self[levelName];
71 }
72
73 return self;
74 }
75
76 /**
77 * Verifies that the specified namespace path point to an existing
78 * namespace or terminal value.
79 *
80 * @param {string} path The namespace path to test.
81 * @return {boolean} {@code true} if the namespace or terminal value exists
82 * at the specified path.
83 */
84 has(path) {
85 return typeof this.get(path) !== 'undefined';
86 }
87
88 /**
89 * Return value for the specified namespace path point.
90 *
91 * @param {string} path The namespace path to test.
92 * @return {*} The value at the specified path in the namespace.
93 */
94 get(path) {
95 let self = this;
96 let levels = path.split('.');
97
98 for (let level of levels) {
99 if (!self[level]) {
100 return undefined;
101 }
102
103 self = self[level];
104 }
105
106 return self;
107 }
108}
109
110exports.Namespace = Namespace;
111exports.default = new Namespace();
112
113typeof $IMA !== 'undefined' && $IMA !== null && $IMA.Loader && $IMA.Loader.register('ima/namespace', [], function (_export, _context) {
114 'use strict';
115 return {
116 setters: [],
117 execute: function () {
118 _export('Namespace', exports.Namespace);
119 _export('default', exports.default);
120 }
121 };
122});