forked from yangshun/lago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCounter.js
146 lines (132 loc) · 3.45 KB
/
Counter.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
class Counter extends Map {
/**
* Initialize the Counter
* @param {*} items Iterable or mapping.
* @return {undefined}
*/
constructor(items) {
// String type.
if (typeof items === 'string') {
super();
const seq = items.split('');
seq.forEach(item => this.increment(item));
return;
}
// Array type.
if (Array.isArray(items)) {
if (items.length === 0) {
super();
return;
}
// Assume iterable object and fallback to Map's initialization.
if (typeof items[0] === 'object') {
super(items);
return;
}
super();
items.forEach(item => this.increment(item));
return;
}
// Object type.
// Note that object keys are always strings.
if (Object.prototype.toString.call(items) === '[object Object]') {
super(Object.entries(items));
return;
}
// Default behavior.
super();
}
/**
* Returns the count associated with the key or 0 if it
* does not exist in the counter.
* @param {*} key The key which count we are interested in.
* @return {number} The count associated with the key.
*/
get(key) {
let value = super.get(key);
if (value == null) {
value = 0;
}
this.set(key, value);
return value;
}
/**
* Increases the count associated with the key by 1.
* If the key does not exist, the count is set to 1.
* @param {*} key The key which count we want to increment.
* @return {Counter}
*/
increment(key) {
const value = this.get(key);
this.set(key, value + 1);
return this;
}
/**
* Decreases the count associated with the key by 1.
* If the key does not exist, the count is set to -1.
* @param {*} key The key which count we want to decrement.
* @return {Counter}
*/
decrement(key) {
const value = this.get(key);
this.set(key, value - 1);
return this;
}
/**
* Returns the key repeated by their counts.
* @return {*[]} An array of keys.
*/
elements() {
const elements = [];
this.forEach((count, key) => {
if (count <= 0) {
return;
}
elements.push(...Array(count).fill(key));
});
return elements;
}
/**
* Returns an array of n most common keys and their counts from the most
* common to the least.
* If n is omitted, all keys in the counter are returned.
* Keys with equal counts are ordered arbitrarily.
* @return {*[]} An array of [key, count] pairs.
*/
mostCommon(n) {
const number = n !== undefined ? n : this.size;
return Array.from(this)
.sort((a, b) => b[1] - a[1])
.slice(0, number);
}
_merge(items, op) {
const other = new Counter(items);
other.forEach((count, key) => {
let value = 0;
if (this.has(key)) {
value = this.get(key);
}
this.set(key, op(value, count));
});
return this;
}
/**
* Keys are subtracted from an iterable or from another mapping.
* Both inputs and outputs may be zero or negative.
* @param {*} items Iterable or mapping.
* @return {Counter}
*/
subtract(items) {
return this._merge(items, (value, count) => value - count);
}
/**
* Keys are added-in from an iterable or from another mapping.
* Both inputs and outputs may be zero or negative.
* @param {*} items Iterable or mapping.
* @return {Counter}
*/
update(items) {
return this._merge(items, (value, count) => value + count);
}
}
export default Counter;