-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprivate.js
More file actions
242 lines (204 loc) · 6.18 KB
/
Copy pathprivate.js
File metadata and controls
242 lines (204 loc) · 6.18 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* @description
* This function finds array index
* used as store key through reference.
*
* @param {object} reference
* @param {object} store
*/
const getUniqueId = (reference, store)=>{
let index = -1
for(let storeItemIndex in store){
let storeItem = store[storeItemIndex]
if(storeItem['reference'] === reference){
index = storeItemIndex
break
}
}
return index
}
/**
* @description
* This function verifies that an
* instance based on the class
* prototype is used as a reference.
*
* @param {object} reference
* @param {function} originInstance
*/
const validate = (reference, originInstance)=>{
if(originInstance === null || originInstance === undefined)
return true
if(reference instanceof originInstance)
return true
let yello = String.fromCharCode(0x1b) + '[33;1m'
let reset = String.fromCharCode(0x1b) + '[39;1m'
throw new Error(`${yello}(capsulable.js:23)\n`
+ `A value different from the class prototype\n`
+ `registered in the repository was used as a reference.${reset}\n`)
}
/**
* @param {function} _instance
* @returns {Private} instance
*/
module.exports = (_instance)=>{
var store = []
var originInstance = undefined
class Private {
/**
* @param {object} reference
* @param {string} key
*/
get(reference, key){
if(key === undefined)
return undefined
validate(reference, originInstance)
let uniqueId = getUniqueId(reference, store)
if(uniqueId === -1)
return undefined
if(typeof store[uniqueId]['data'][key] === 'undefined')
return undefined
return store[uniqueId]['data'][key]
}
/**
* @param {object} reference
*/
getAll(reference){
validate(reference, originInstance)
let uniqueId = getUniqueId(reference, store)
if(uniqueId === -1)
return undefined
return store[uniqueId]
}
/**
* @param {object} reference
* @param {string} key
* @param {*} data
* @returns {boolean} isSuccess
*/
set(reference, key, data){
validate(reference, originInstance)
// Prevents storing self instance within variables.
if(data instanceof Private)
return false
let uniqueId = getUniqueId(reference, store)
if(key === undefined)
return false
if(data === undefined)
return this.remove(reference, key)
if(uniqueId !== -1){
store[uniqueId]['data'][key] = data
}else{
let storedData = {}
storedData[key] = data
store.push({
reference,
data: storedData
})
}
return true
}
/**
* @param {object} reference
* @param {string} key
* @returns {boolean} isSuccess
*/
remove(reference, key){
validate(reference, originInstance)
let uniqueId = getUniqueId(reference, store)
if(uniqueId === -1)
return false
if(key === undefined)
return false
if(typeof store[uniqueId]['data'][key] == 'undefined')
return false
delete(store[uniqueId]['data'][key])
if(Object.keys(store[uniqueId]['data']).length == 0)
this.removeAll(reference)
return true
}
/**
* @param {object} reference
* @returns {boolean} isSuccess
*/
removeAll(reference){
validate(reference, originInstance)
let uniqueId = getUniqueId(reference, store)
if(uniqueId === -1)
return false
store.splice(uniqueId, 1)
return true
}
/**
* @param {object} reference
* @param {string} key
* @returns {boolean} isExist
*/
exist(reference, key){
validate(reference, originInstance)
let uniqueId = getUniqueId(reference, store)
if(uniqueId === -1)
return false
if(key === undefined) return false
return this.get(reference, key) !== undefined
}
/**
* @description
* Registering a class prototype will cause
* an error if the reference is different from
* the class prototype when using the store.
*
* @param {function} instance
* @returns {boolean} isSuccess
*/
register(instance){
if(originInstance !== undefined) return false
originInstance = instance
return true
}
}
const _private = new Private()
if(_instance !== undefined){
class GuidedPrivate{
/**
* @param {string} key
*/
get(key){
return _private.get(_instance, key)
}
getAll(){
return _private.getAll(_instance)
}
/**
* @param {string} key
* @param {*} data
* @returns {boolean} isSuccess
*/
set(key, data){
return _private.set(_instance, key, data)
}
/**
* @param {string} key
* @returns {boolean} isSuccess
*/
remove(key){
return _private.remove(_instance, key)
}
/**
* @returns {boolean} isSuccess
*/
removeAll(){
return _private.removeAll(_instance)
}
/**
* @param {string} key
* @returns {boolean} isExist
*/
exist(key){
return _private.exist(_instance, key)
}
}
return new GuidedPrivate()
}
return _private
}