-
Notifications
You must be signed in to change notification settings - Fork 0
/
astares-atom.js
87 lines (75 loc) · 2.23 KB
/
astares-atom.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
module.exports = {
/*
* A namespace
*/
Namespace : class Namespace {
/////////////////////////////////////////////////////////////////////////////////////
// STATIC ACCESSING
/////////////////////////////////////////////////////////////////////////////////////
/**
* Return the global object which is either self, or window or the defined global object
* {return} the global object
*/
static global() {
if (typeof self !== 'undefined') { return self; } //eslint-disable-line no-undef
if (typeof window !== 'undefined') { return window; } //eslint-disable-line no-undef
if (typeof global !== 'undefined') { return global; } //eslint-disable-line no-undef
throw new Error('No global object');
}
/////////////////////////////////////////////////////////////////////////////////////
// STATIC UTILITIES
/////////////////////////////////////////////////////////////////////////////////////
/**
* Return a new unique ID
*/
static createUniqueID() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + s4() + s4() + s4() + s4() + s4() + s4();
};
/**
* Map the given object to the given Namespace.
*
* @param {*} object
* @param {*} namespaceName
*/
static mapObjectToNamespace(object, namespaceName) {
if (typeof object !== 'undefined') {
(Namespace.create(namespaceName))[object.name] = object;
}
return object;
}
/**
* Create a new namespace.
* {@param} {String} namespace the fully qualified
* {return} the created Namespace
*/
static create(namespace) {
var path = '';
return namespace.split('.').reduce(function(holder, name){
if(holder[name] == undefined) {
path = path + name;
var ns = new Namespace(path);
holder[name] = holder[name] || ns;
path = path + '.';
}
return holder[name];
}, Namespace.global());
}
/**
* Return a new instance of the receiver
* {@param} {String} -
* {return} a new instance with the given name
*/
constructor(newName) {
this.name = newName;
this._ooid = Namespace.createUniqueID();
}
toString() {
return this._name;
}
}
}