Skip to content

Commit b0b3f10

Browse files
committed
Rename some variables in Map and Set to match decloration files
1 parent dcbd1b9 commit b0b3f10

File tree

4 files changed

+138
-146
lines changed

4 files changed

+138
-146
lines changed

src/lualib/Map.ts

Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
Map = class Map<TKey, TValue> {
1+
Map = class Map<K, V> {
22
public static [Symbol.species] = Map;
33
public [Symbol.toStringTag] = "Map";
44

5-
public size: number;
5+
// Type of key is actually K
6+
private items: { [key: string]: V } = {};
7+
public size = 0;
68

7-
private items: { [key: string]: TValue }; // Type of key is actually TKey
9+
constructor(entries?: Iterable<readonly [K, V]> | Array<readonly [K, V]>) {
10+
if (entries === undefined) return;
811

9-
constructor(other?: Iterable<readonly [TKey, TValue]> | Array<readonly [TKey, TValue]>) {
10-
this.items = {};
11-
this.size = 0;
12-
13-
if (other === undefined) return;
14-
15-
const iterable = other as Iterable<[TKey, TValue]>;
12+
const iterable = entries as Iterable<[K, V]>;
1613
if (iterable[Symbol.iterator]) {
1714
// Iterate manually because Map is compiled with ES5 which doesn't support Iterables in for...of
1815
const iterator = iterable[Symbol.iterator]();
@@ -21,11 +18,11 @@ Map = class Map<TKey, TValue> {
2118
if (result.done) {
2219
break;
2320
}
24-
const value: [TKey, TValue] = result.value; // Ensures index is offset when tuple is accessed
21+
const value: [K, V] = result.value; // Ensures index is offset when tuple is accessed
2522
this.set(value[0], value[1]);
2623
}
2724
} else {
28-
const array = other as Array<[TKey, TValue]>;
25+
const array = entries as Array<[K, V]>;
2926
this.size = array.length;
3027
for (const kvp of array) {
3128
this.items[kvp[0] as any] = kvp[1];
@@ -39,7 +36,7 @@ Map = class Map<TKey, TValue> {
3936
return;
4037
}
4138

42-
public delete(key: TKey): boolean {
39+
public delete(key: K): boolean {
4340
const contains = this.has(key);
4441
if (contains) {
4542
this.size--;
@@ -48,71 +45,71 @@ Map = class Map<TKey, TValue> {
4845
return contains;
4946
}
5047

51-
public [Symbol.iterator](): IterableIterator<[TKey, TValue]> {
48+
public forEach(callback: (value: V, key: K, map: Map<K, V>) => any): void {
49+
for (const key in this.items) {
50+
callback(this.items[key], key as any, this);
51+
}
52+
return;
53+
}
54+
55+
public get(key: K): V | undefined {
56+
return this.items[key as any];
57+
}
58+
59+
public has(key: K): boolean {
60+
return this.items[key as any] !== undefined;
61+
}
62+
63+
public set(key: K, value: V): this {
64+
if (!this.has(key)) {
65+
this.size++;
66+
}
67+
this.items[key as any] = value;
68+
return this;
69+
}
70+
71+
public [Symbol.iterator](): IterableIterator<[K, V]> {
5272
return this.entries();
5373
}
5474

55-
public entries(): IterableIterator<[TKey, TValue]> {
75+
public entries(): IterableIterator<[K, V]> {
5676
const items = this.items;
57-
let key: TKey;
58-
let value: TValue;
77+
let key: K;
78+
let value: V;
5979
return {
60-
[Symbol.iterator](): IterableIterator<[TKey, TValue]> {
80+
[Symbol.iterator](): IterableIterator<[K, V]> {
6181
return this;
6282
},
63-
next(): IteratorResult<[TKey, TValue]> {
83+
next(): IteratorResult<[K, V]> {
6484
[key, value] = next(items, key);
6585
return { done: !key, value: [key, value] };
6686
},
6787
};
6888
}
6989

70-
public forEach(callback: (value: TValue, key: TKey, map: Map<TKey, TValue>) => any): void {
71-
for (const key in this.items) {
72-
callback(this.items[key], key as any, this);
73-
}
74-
return;
75-
}
76-
77-
public get(key: TKey): TValue | undefined {
78-
return this.items[key as any];
79-
}
80-
81-
public has(key: TKey): boolean {
82-
return this.items[key as any] !== undefined;
83-
}
84-
85-
public keys(): IterableIterator<TKey> {
90+
public keys(): IterableIterator<K> {
8691
const items = this.items;
87-
let key: TKey;
92+
let key: K;
8893
return {
89-
[Symbol.iterator](): IterableIterator<TKey> {
94+
[Symbol.iterator](): IterableIterator<K> {
9095
return this;
9196
},
92-
next(): IteratorResult<TKey> {
97+
next(): IteratorResult<K> {
9398
[key] = next(items, key);
9499
return { done: !key, value: key };
95100
},
96101
};
97102
}
98103

99-
public set(key: TKey, value: TValue): this {
100-
if (!this.has(key)) {
101-
this.size++;
102-
}
103-
this.items[key as any] = value;
104-
return this;
105-
}
106-
107-
public values(): IterableIterator<TValue> {
104+
public values(): IterableIterator<V> {
108105
const items = this.items;
109-
let key: TKey;
110-
let value: TValue;
106+
let key: K;
107+
let value: V;
111108
return {
112-
[Symbol.iterator](): IterableIterator<TValue> {
109+
[Symbol.iterator](): IterableIterator<V> {
113110
return this;
114111
},
115-
next(): IteratorResult<TValue> {
112+
next(): IteratorResult<V> {
116113
[key, value] = next(items, key);
117114
return { done: !key, value };
118115
},

src/lualib/Set.ts

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,35 @@
1-
Set = class Set<TValue> {
1+
Set = class Set<T> {
22
public static [Symbol.species] = Set;
33
public [Symbol.toStringTag] = "Set";
44

5-
public size: number;
5+
// Key type is actually T
6+
private items: { [key: string]: boolean } = {};
7+
public size = 0;
68

7-
private items: { [key: string]: boolean }; // Key type is actually TValue
9+
constructor(values?: Iterable<T> | T[]) {
10+
if (values === undefined) return;
811

9-
constructor(other: Iterable<TValue> | TValue[]) {
10-
this.items = {};
11-
this.size = 0;
12-
13-
if (other) {
14-
const iterable = other as Iterable<TValue>;
15-
if (iterable[Symbol.iterator]) {
16-
// Iterate manually because Set is compiled with ES5 which doesn't support Iterables in for...of
17-
const iterator = iterable[Symbol.iterator]();
18-
while (true) {
19-
const result = iterator.next();
20-
if (result.done) {
21-
break;
22-
}
23-
this.add(result.value);
24-
}
25-
} else {
26-
const arr = other as TValue[];
27-
this.size = arr.length;
28-
for (const value of arr) {
29-
this.items[value as any] = true;
12+
const iterable = values as Iterable<T>;
13+
if (iterable[Symbol.iterator]) {
14+
// Iterate manually because Set is compiled with ES5 which doesn't support Iterables in for...of
15+
const iterator = iterable[Symbol.iterator]();
16+
while (true) {
17+
const result = iterator.next();
18+
if (result.done) {
19+
break;
3020
}
21+
this.add(result.value);
22+
}
23+
} else {
24+
const array = values as T[];
25+
this.size = array.length;
26+
for (const value of array) {
27+
this.items[value as any] = true;
3128
}
3229
}
3330
}
3431

35-
public add(value: TValue): Set<TValue> {
32+
public add(value: T): Set<T> {
3633
if (!this.has(value)) {
3734
this.size++;
3835
}
@@ -46,7 +43,7 @@ Set = class Set<TValue> {
4643
return;
4744
}
4845

49-
public delete(value: TValue): boolean {
46+
public delete(value: T): boolean {
5047
const contains = this.has(value);
5148
if (contains) {
5249
this.size--;
@@ -55,56 +52,56 @@ Set = class Set<TValue> {
5552
return contains;
5653
}
5754

58-
public [Symbol.iterator](): IterableIterator<TValue> {
55+
public forEach(callback: (value: T, key: T, set: Set<T>) => any): void {
56+
for (const key in this.items) {
57+
callback(key as any, key as any, this);
58+
}
59+
}
60+
61+
public has(value: T): boolean {
62+
return this.items[value as any] === true;
63+
}
64+
65+
public [Symbol.iterator](): IterableIterator<T> {
5966
return this.values();
6067
}
6168

62-
public entries(): IterableIterator<[TValue, TValue]> {
69+
public entries(): IterableIterator<[T, T]> {
6370
const items = this.items;
64-
let key: TValue;
71+
let key: T;
6572
return {
66-
[Symbol.iterator](): IterableIterator<[TValue, TValue]> {
73+
[Symbol.iterator](): IterableIterator<[T, T]> {
6774
return this;
6875
},
69-
next(): IteratorResult<[TValue, TValue]> {
76+
next(): IteratorResult<[T, T]> {
7077
[key] = next(items, key);
7178
return { done: !key, value: [key, key] };
7279
},
7380
};
7481
}
7582

76-
public forEach(callback: (value: TValue, key: TValue, set: Set<TValue>) => any): void {
77-
for (const key in this.items) {
78-
callback(key as any, key as any, this);
79-
}
80-
}
81-
82-
public has(value: TValue): boolean {
83-
return this.items[value as any] === true;
84-
}
85-
86-
public keys(): IterableIterator<TValue> {
83+
public keys(): IterableIterator<T> {
8784
const items = this.items;
88-
let key: TValue;
85+
let key: T;
8986
return {
90-
[Symbol.iterator](): IterableIterator<TValue> {
87+
[Symbol.iterator](): IterableIterator<T> {
9188
return this;
9289
},
93-
next(): IteratorResult<TValue> {
90+
next(): IteratorResult<T> {
9491
[key] = next(items, key);
9592
return { done: !key, value: key };
9693
},
9794
};
9895
}
9996

100-
public values(): IterableIterator<TValue> {
97+
public values(): IterableIterator<T> {
10198
const items = this.items;
102-
let key: TValue;
99+
let key: T;
103100
return {
104-
[Symbol.iterator](): IterableIterator<TValue> {
101+
[Symbol.iterator](): IterableIterator<T> {
105102
return this;
106103
},
107-
next(): IteratorResult<TValue> {
104+
next(): IteratorResult<T> {
108105
[key] = next(items, key);
109106
return { done: !key, value: key };
110107
},

src/lualib/WeakMap.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,48 @@
1-
WeakMap = class WeakMap<TKey extends object, TValue> {
1+
WeakMap = class WeakMap<K extends object, V> {
22
public static [Symbol.species] = WeakMap;
33
public [Symbol.toStringTag] = "WeakMap";
44

5-
private items: { [key: string]: TValue }; // Type of key is actually TKey
5+
// Type of key is actually K
6+
private items: { [key: string]: V } = {};
67

7-
constructor(other: Iterable<[TKey, TValue]> | Array<[TKey, TValue]>) {
8-
this.items = {};
8+
constructor(entries?: Iterable<readonly [K, V]> | Array<readonly [K, V]>) {
99
setmetatable(this.items, { __mode: "k" });
10+
if (entries === undefined) return;
1011

11-
if (other) {
12-
const iterable = other as Iterable<[TKey, TValue]>;
13-
if (iterable[Symbol.iterator]) {
14-
// Iterate manually because WeakMap is compiled with ES5 which doesn't support Iterables in for...of
15-
const iterator = iterable[Symbol.iterator]();
16-
while (true) {
17-
const result = iterator.next();
18-
if (result.done) {
19-
break;
20-
}
21-
const value: [TKey, TValue] = result.value; // Ensures index is offset when tuple is accessed
22-
this.set(value[0], value[1]);
23-
}
24-
} else {
25-
for (const kvp of other as Array<[TKey, TValue]>) {
26-
this.items[kvp[0] as any] = kvp[1];
12+
const iterable = entries as Iterable<[K, V]>;
13+
if (iterable[Symbol.iterator]) {
14+
// Iterate manually because WeakMap is compiled with ES5 which doesn't support Iterables in for...of
15+
const iterator = iterable[Symbol.iterator]();
16+
while (true) {
17+
const result = iterator.next();
18+
if (result.done) {
19+
break;
2720
}
21+
const value: [K, V] = result.value; // Ensures index is offset when tuple is accessed
22+
this.set(value[0], value[1]);
23+
}
24+
} else {
25+
for (const kvp of entries as Array<[K, V]>) {
26+
this.items[kvp[0] as any] = kvp[1];
2827
}
2928
}
3029
}
3130

32-
public delete(key: TKey): boolean {
31+
public delete(key: K): boolean {
3332
const contains = this.has(key);
3433
this.items[key as any] = undefined;
3534
return contains;
3635
}
3736

38-
public get(key: TKey): TValue | undefined {
37+
public get(key: K): V | undefined {
3938
return this.items[key as any];
4039
}
4140

42-
public has(key: TKey): boolean {
41+
public has(key: K): boolean {
4342
return this.items[key as any] !== undefined;
4443
}
4544

46-
public set(key: TKey, value: TValue): this {
45+
public set(key: K, value: V): this {
4746
this.items[key as any] = value;
4847
return this;
4948
}

0 commit comments

Comments
 (0)