-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrafity.Dictionary.js
executable file
·211 lines (185 loc) · 5.09 KB
/
crafity.Dictionary.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
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
/*jslint bitwise: true, unparam: true, maxerr: 50, white: true */
/*globals crafity, window*/
/*!
* crafity.Dictionary
* Copyright(c) 2011 Crafity
* Copyright(c) 2011 Bart Riemens
* Copyright(c) 2011 Galina Slavova
* MIT Licensed
*/
(function (crafity) {
"use strict";
if (!crafity.common) {
throw new Error("Missing dependency 'crafity.common'");
}
if (!crafity.Event) {
throw new Error("Missing dependency 'crafity.Event'");
}
if (!crafity.arrays) {
throw new Error("Missing dependency 'crafity.arrays'");
}
if (!crafity.objects) {
throw new Error("Missing dependency 'crafity.objects'");
}
var common = crafity.common
, Event = crafity.Event
, arrays = crafity.arrays
, objects = crafity.objects;
/**
* A Dictionary Type
* @param {Object} obj (Optional) An initial object literal with values
*/
crafity.Dictionary = function Dictionary(obj) {
/**
* Validate Arguments
*/
common.arg({ name: 'obj', value: obj, type: Object, required: false });
/**
* Variable Declarations
*/
var self = this
, innerDictionary = {};
/**
* On Item Added Event
*/
this.onItemAdded = new Event();
/**
* On Item Removed Event
*/
this.onItemRemoved = new Event();
/**
* On Item Changed Event
*/
this.onItemChanged = new Event();
/**
* Number of obj in the dictionary
* @returns {Number} Number of obj in the dictionary
*/
this.count = 0;
/**
* Are there any obj in the dictionary
* @returns {Boolean} True if there are obj else False
*/
this.hasAny = false;
/**
* Get a value by key
* @param {String} key The key of the value to get
* @returns {Object} The value or undefined
*/
this.get = function (key) {
common.arg({ name: 'key', value: key, type: String, required: true });
return innerDictionary[key];
};
/**
* Convert dictionary into an object literal
* @returns {Object} Object literal of the dictionary
*/
this.toObject = function () {
return objects.clone(innerDictionary);
};
/**
* Loop over the dictionary
* @param {Function} fn Pass in a function with the arguments value, key and
* @returns {Dictionary} The dictionary itself
*/
this.forEach = function (fn) {
objects.forEach(innerDictionary, function (value, key, innerDict) {
fn(value, key);
});
return self;
};
/**
* Remove all the obj in the dictionary
* @returns {Dictionary} The dictionary itself
*/
this.clear = function () {
self.forEach(function (item, key) {
self.remove(key, item);
});
return self;
};
/**
* Get all the keys
* @returns {Array} An array with all the keys
*/
this.getKeys = function () {
return Object.keys(innerDictionary);
};
/**
* Check if the dictionary contains a specific key
* @param {String} key The key to check for
* @returns {Boolean} True if the keys exists else False
*/
this.containsKey = function (key) {
common.arg({ name: 'key', value: key, type: String, required: true });
return arrays.contains(self.getKeys(), key);
};
/**
* Remove an item from the dictionary by using the key
* @param {String} key The key to remove
* @returns {Dictionary} The dictionary itself
*/
this.remove = function (key) {
common.arg({ name: 'key', value: key, type: String, required: true });
var item;
if (self.containsKey(key)) {
item = self.get(key);
delete innerDictionary[key];
self.count -= 1;
self.hasAny = self.count > 0;
self.onItemRemoved.raise(item, key);
}
return self;
};
/**
* Set a value of an existing key
* @param {String} key The key of the value to set
* @param {Object} value The value to set
* @returns {Dictionary} The dictionary itself
*/
this.set = function (key, value) {
common.arg({ name: 'key', value: key, type: String, required: true });
common.arg({ name: 'value', value: value, required: true });
if (!self.containsKey(key)) {
throw new common.Exception("Key '" + key + "' does not exist");
}
innerDictionary[key] = value;
self.onItemChanged.raise(value, key);
return self;
};
/**
* Add a new key and value to the dictionary
* @param {String} key The key to add
* @param {Object} value The value to add
* @returns {Dictionary} The dictionary itself
*/
this.add = function (key, value) {
common.arg({ name: 'key', value: key, type: String, required: true });
common.arg({ name: 'value', value: value });
if (self.containsKey(key)) {
throw new common.Exception("Key '" + key + "' already exists");
}
innerDictionary[key] = value;
self.count += 1;
self.hasAny = true;
self.onItemAdded.raise(value, key);
return self;
};
/**
* Add an object literal's keys and values
* @param {Object} obj The value to add
* @returns {Dictionary} The dictionary itself
*/
this.addMany = function (obj) {
common.arg({ name: 'obj', value: obj, type: Object, required: true });
self.clear();
objects.forEach(obj, function (item, key) {
self.add(key, item);
});
return self;
};
if (obj) {
self.addMany(obj);
}
};
}(window.crafity = window.crafity || {}));