hashMap.js
9.68 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/**
* @fileoverview This module provides the HashMap constructor.
* @author NHN.
* FE Development Lab <dl_javascript@nhn.com>
*/
'use strict';
var collection = require('./collection');
var type = require('./type');
/**
* All the data in hashMap begin with _MAPDATAPREFIX;
* @type {string}
* @private
*/
var _MAPDATAPREFIX = 'å';
/**
* HashMap can handle the key-value pairs.<br>
* Caution:<br>
* HashMap instance has a length property but is not an instance of Array.
* @param {Object} [obj] A initial data for creation.
* @constructor
* @memberof tui.util
* @deprecated since version 1.3.0
* @example
* // node, commonjs
* var HashMap = require('tui-code-snippet').HashMap;
* var hm = new tui.util.HashMap({
'mydata': {
'hello': 'imfine'
},
'what': 'time'
});
* @example
* // distribution file, script
* <script src='path-to/tui-code-snippt.js'></script>
* <script>
* var HashMap = tui.util.HashMap;
* <script>
* var hm = new tui.util.HashMap({
'mydata': {
'hello': 'imfine'
},
'what': 'time'
});
*/
function HashMap(obj) {
/**
* size
* @type {number}
*/
this.length = 0;
if (obj) {
this.setObject(obj);
}
}
/**
* Set a data from the given key with value or the given object.
* @param {string|Object} key A string or object for key
* @param {*} [value] A data
* @example
* //-- #1. Get Module --//
* var HashMap = require('tui-code-snippet').HashMap; // node, commonjs
* var HashMap = tui.util.HashMap; // distribution file
*
* //-- #2. Use property --//
* var hm = new HashMap();
* hm.set('key', 'value');
* hm.set({
* 'key1': 'data1',
* 'key2': 'data2'
* });
*/
HashMap.prototype.set = function(key, value) {
if (arguments.length === 2) {
this.setKeyValue(key, value);
} else {
this.setObject(key);
}
};
/**
* Set a data from the given key with value.
* @param {string} key A string for key
* @param {*} value A data
* @example
* //-- #1. Get Module --//
* var HashMap = require('tui-code-snippet').HashMap; // node, commonjs
* var HashMap = tui.util.HashMap; // distribution file
*
* //-- #2. Use property --//
* var hm = new HashMap();
* hm.setKeyValue('key', 'value');
*/
HashMap.prototype.setKeyValue = function(key, value) {
if (!this.has(key)) {
this.length += 1;
}
this[this.encodeKey(key)] = value;
};
/**
* Set a data from the given object.
* @param {Object} obj A object for data
* @example
* //-- #1. Get Module --//
* var HashMap = require('tui-code-snippet').HashMap; // node, commonjs
* var HashMap = tui.util.HashMap; // distribution file
*
* //-- #2. Use property --//
* var hm = new HashMap();
* hm.setObject({
* 'key1': 'data1',
* 'key2': 'data2'
* });
*/
HashMap.prototype.setObject = function(obj) {
var self = this;
collection.forEachOwnProperties(obj, function(value, key) {
self.setKeyValue(key, value);
});
};
/**
* Merge with the given another hashMap.
* @param {HashMap} hashMap Another hashMap instance
*/
HashMap.prototype.merge = function(hashMap) {
var self = this;
hashMap.each(function(value, key) {
self.setKeyValue(key, value);
});
};
/**
* Encode the given key for hashMap.
* @param {string} key A string for key
* @returns {string} A encoded key
* @private
*/
HashMap.prototype.encodeKey = function(key) {
return _MAPDATAPREFIX + key;
};
/**
* Decode the given key in hashMap.
* @param {string} key A string for key
* @returns {string} A decoded key
* @private
*/
HashMap.prototype.decodeKey = function(key) {
var decodedKey = key.split(_MAPDATAPREFIX);
return decodedKey[decodedKey.length - 1];
};
/**
* Return the value from the given key.
* @param {string} key A string for key
* @returns {*} The value from a key
* @example
* //-- #1. Get Module --//
* var HashMap = require('tui-code-snippet').HashMap; // node, commonjs
* var HashMap = tui.util.HashMap; // distribution file
*
* //-- #2. Use property --//
* var hm = new HashMap();
* hm.set('key', 'value');
* hm.get('key') // value
*/
HashMap.prototype.get = function(key) {
return this[this.encodeKey(key)];
};
/**
* Check the existence of a value from the key.
* @param {string} key A string for key
* @returns {boolean} Indicating whether a value exists or not.
* @example
* //-- #1. Get Module --//
* var HashMap = require('tui-code-snippet').HashMap; // node, commonjs
* var HashMap = tui.util.HashMap; // distribution file
*
* //-- #2. Use property --//
* var hm = new HashMap();
* hm.set('key', 'value');
* hm.has('key') // true
*/
HashMap.prototype.has = function(key) {
return this.hasOwnProperty(this.encodeKey(key));
};
/**
* Remove a data(key-value pairs) from the given key or the given key-list.
* @param {...string|string[]} key A string for key
* @returns {string|string[]} A removed data
* @example
* //-- #1. Get Module --//
* var HashMap = require('tui-code-snippet').HashMap; // node, commonjs
* var HashMap = tui.util.HashMap; // distribution file
*
* //-- #2. Use property --//
* var hm = new HashMap();
* hm.set('key', 'value');
* hm.set('key2', 'value');
*
* hm.remove('key');
* hm.remove('key', 'key2');
* hm.remove(['key', 'key2']);
*/
HashMap.prototype.remove = function(key) {
if (arguments.length > 1) {
key = collection.toArray(arguments);
}
return type.isArray(key) ? this.removeByKeyArray(key) : this.removeByKey(key);
};
/**
* Remove data(key-value pair) from the given key.
* @param {string} key A string for key
* @returns {*|null} A removed data
* @example
* //-- #1. Get Module --//
* var HashMap = require('tui-code-snippet').HashMap; // node, commonjs
* var HashMap = tui.util.HashMap; // distribution file
*
* //-- #2. Use property --//
* var hm = new HashMap();
* hm.set('key', 'value');
* hm.removeByKey('key')
*/
HashMap.prototype.removeByKey = function(key) {
var data = this.has(key) ? this.get(key) : null;
if (data !== null) {
delete this[this.encodeKey(key)];
this.length -= 1;
}
return data;
};
/**
* Remove a data(key-value pairs) from the given key-list.
* @param {string[]} keyArray An array of keys
* @returns {string[]} A removed data
* @example
* //-- #1. Get Module --//
* var HashMap = require('tui-code-snippet').HashMap; // node, commonjs
* var HashMap = tui.util.HashMap; // distribution file
*
* //-- #2. Use property --//
* var hm = new HashMap();
* hm.set('key', 'value');
* hm.set('key2', 'value');
* hm.removeByKeyArray(['key', 'key2']);
*/
HashMap.prototype.removeByKeyArray = function(keyArray) {
var data = [];
var self = this;
collection.forEach(keyArray, function(key) {
data.push(self.removeByKey(key));
});
return data;
};
/**
* Remove all the data
*/
HashMap.prototype.removeAll = function() {
var self = this;
this.each(function(value, key) {
self.remove(key);
});
};
/**
* Execute the provided callback once for each all the data.
* @param {Function} iteratee Callback function
* @example
* //-- #1. Get Module --//
* var HashMap = require('tui-code-snippet').HashMap; // node, commonjs
* var HashMap = tui.util.HashMap; // distribution file
*
* //-- #2. Use property --//
* var hm = new HashMap();
* hm.set('key', 'value');
* hm.set('key2', 'value');
*
* hm.each(function(value, key) {
* //do something...
* });
*/
HashMap.prototype.each = function(iteratee) {
var self = this;
var flag;
collection.forEachOwnProperties(this, function(value, key) { // eslint-disable-line consistent-return
if (key.charAt(0) === _MAPDATAPREFIX) {
flag = iteratee(value, self.decodeKey(key));
}
if (flag === false) {
return flag;
}
});
};
/**
* Return the key-list stored.
* @returns {Array} A key-list
* @example
* //-- #1. Get Module --//
* var HashMap = require('tui-code-snippet').HashMap; // node, commonjs
* var HashMap = tui.util.HashMap; // distribution file
*
* //-- #2. Use property --//
* var hm = new HashMap();
* hm.set('key', 'value');
* hm.set('key2', 'value');
* hm.keys(); //['key', 'key2');
*/
HashMap.prototype.keys = function() {
var keys = [];
var self = this;
this.each(function(value, key) {
keys.push(self.decodeKey(key));
});
return keys;
};
/**
* Work similarly to Array.prototype.map().<br>
* It executes the provided callback that checks conditions once for each element of hashMap,<br>
* and returns a new array having elements satisfying the conditions
* @param {Function} condition A function that checks conditions
* @returns {Array} A new array having elements satisfying the conditions
* @example
* //-- #1. Get Module --//
* var HashMap = require('tui-code-snippet').HashMap; // node, commonjs
* var HashMap = tui.util.HashMap; // distribution file
*
* //-- #2. Use property --//
* var hm1 = new HashMap();
* hm1.set('key', 'value');
* hm1.set('key2', 'value');
*
* hm1.find(function(value, key) {
* return key === 'key2';
* }); // ['value']
*
* var hm2 = new HashMap({
* 'myobj1': {
* visible: true
* },
* 'mybobj2': {
* visible: false
* }
* });
*
* hm2.find(function(obj, key) {
* return obj.visible === true;
* }); // [{visible: true}];
*/
HashMap.prototype.find = function(condition) {
var founds = [];
this.each(function(value, key) {
if (condition(value, key)) {
founds.push(value);
}
});
return founds;
};
/**
* Return a new Array having all values.
* @returns {Array} A new array having all values
*/
HashMap.prototype.toArray = function() {
var result = [];
this.each(function(v) {
result.push(v);
});
return result;
};
module.exports = HashMap;