-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValue.ts
83 lines (60 loc) · 1.78 KB
/
Value.ts
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
import _ = require('underscore');
///ts:import=Util
import Util = require('./Util'); ///ts:import:generated
module Value {
export class CachedValue<T> {
private value: T;
constructor(private calculateValue: () => T) { }
get(): T {
if (!this.value) {
this.value = this.calculateValue();
}
return this.value;
}
}
export class Unique {
private i = 0;
constructor(private prefix: string) { }
get() {
return this.prefix + (++this.i);
}
}
export class NameCounter {
private counts: { [v: string]: number } = {};
getPositiveNames(): string[] {
return _.filter(_.keys(this.counts), key => this.counts[key] > 0);
}
increment(name) {
this.counts[name] = Util.nvl(this.counts[name], 0) + 1;
}
decrement(name) {
this.counts[name]--;
}
}
interface ObjectCount<T> {
object: T;
count: number;
}
export class ObjectCounter<T> {
private counts: ObjectCount<T>[] = [];
increment(object: T): number {
var count = _.find(this.counts, count => count.object == object);
if (count == null) {
count = {
object: object,
count: 0
}
this.counts.push(count);
}
return ++count.count;
}
decrement(object: T): number {
var count = _.find(this.counts, count => count.object == object);
return --count.count;
}
getPositiveValues(): T[] {
return _.pluck(_.filter(this.counts, (c) => c.count > 0), 'object');
}
}
}
export = Value;