-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path1-object.js
More file actions
70 lines (61 loc) · 1.42 KB
/
1-object.js
File metadata and controls
70 lines (61 loc) · 1.42 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
'use strict';
class Dictionary {
constructor() {
this.map = Object.create(null);
}
set(key, value) {
this.map[key] = value;
return this;
}
get(key) {
return this.map[key];
}
has(key) {
// TODO: handel false, null, undefined, '', 0
return !!this.map[key];
}
delete(key) {
delete this.map[key];
}
get size() {
return Object.keys(this.map).length;
}
keys() {
return Object.keys(this.map);
}
clear() {
this.map = Object.create(null);
}
static from(hash) {
const instance = new Dictionary();
for (const key in hash) {
instance.set(key, hash[key]);
}
return instance;
}
}
// Usage
const cities = {
Shanghai: 24256800,
Beijing: 21516000,
Delhi: 16787941,
Lagos: 16060303,
};
const cityPopulation1 = Dictionary.from(cities);
console.dir({ cityPopulation1 });
const cityPopulation2 = new Dictionary();
cityPopulation2.set('Shanghai', 24256800);
cityPopulation2.set('Beijing', 21516000);
cityPopulation2.set('Delhi', 16787941);
cityPopulation2.set('Lagos', 16060303);
console.dir({ cityPopulation2 });
cityPopulation2.delete('Shanghai');
console.dir({ cityPopulation2 });
if (cityPopulation2.has('Beijing')) {
console.log('Beijing:', cityPopulation2.get('Beijing'));
}
if (!cityPopulation2.has('Shanghai')) {
console.log('no data for Shanghai');
}
console.log('size:', cityPopulation2.size);
console.log('keys:', cityPopulation2.keys());