-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndexedObject.js
64 lines (59 loc) · 1.39 KB
/
IndexedObject.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
/**
* IndexedObject
*
* @version 1.0.0
*
* @author m13p4
* @copyright Meliantchenkov Pavel
*/
var IndexedObject = function(init)
{
var obj = {}, index = [], map = {}, indexObj;
function _get(key)
{
if("undefined" === typeof key) return obj;
if("undefined" !== typeof map[key]) return obj[key];
}
function _set(key, val)
{
if("undefined" === typeof map[key])
{
obj[key] = val;
map[key] = index.length;
indexObj.length = index.push(key);
}
else obj[key] = val;
}
function _delete(key)
{
if(map[key])
{
delete index[map[key]];
delete obj[key];
delete map[key];
}
}
function _for(callback)
{
if("function" === typeof callback)
for(var i = 0; i < index.length; i++)
if("undefined" !== typeof index[i])
callback(index[i], obj[index[i]]);
}
function _byIndex(indexKey)
{
if("undefined" !== typeof index[indexKey])
return obj[index[indexKey]];
}
indexObj = {
get: _get,
set: _set,
delete: _delete,
for: _for,
byIndex: _byIndex,
length: 0
};
if(init) for(var i in init) _set(i, init[i]);
return indexObj;
};
module.exports = IndexedObject;