Skip to content

Commit b15ffda

Browse files
author
Andy Hanson
committed
Simplify forEachKeyInMap and someKeyInMap
1 parent 6b1cc89 commit b15ffda

2 files changed

Lines changed: 11 additions & 37 deletions

File tree

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18991,7 +18991,7 @@ namespace ts {
1899118991
}
1899218992

1899318993
function hasExportedMembers(moduleSymbol: Symbol) {
18994-
return someKeyInMap(moduleSymbol.exports, id => id !== "export=");
18994+
return someInMap(moduleSymbol.exports, (_, id) => id !== "export=");
1899518995
}
1899618996

1899718997
function checkExternalModuleExports(node: SourceFile | ModuleDeclaration) {

src/compiler/core.ts

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ namespace ts {
6868
interface ES6Map<T> extends Map<T> {
6969
readonly size: number;
7070
entries(): Iterator<[string, T]>;
71-
keys(): Iterator<string>;
7271
}
7372

7473
/** ES6 Iterator type. */
@@ -80,9 +79,7 @@ namespace ts {
8079
interface ShimMap<T> extends Map<T> {
8180
isEmpty(): boolean;
8281
forEachInMap<U>(callback: (value: T, key: string) => U | undefined): U | undefined;
83-
forEachKey<U>(callback: (key: string) => U | undefined): U | undefined;
8482
some(predicate: (value: T, key: string) => boolean): boolean;
85-
someKey(predicate: (key: string) => boolean): boolean;
8683
}
8784

8885
// The global Map object. This may not be available, so we must test for it.
@@ -130,29 +127,21 @@ namespace ts {
130127
}
131128

132129
isEmpty(): boolean {
133-
return !this.someKey(() => true);
130+
return !this.some(() => true);
134131
}
135132

136133
forEachInMap<U>(callback: (value: T, key: string) => U | undefined): U | undefined {
137-
return this.forEachKey(key => callback(this.data[key], key));
138-
}
139-
140-
forEachKey<U>(callback: (key: string) => U | undefined): U | undefined {
141134
for (const key in this.data) {
142-
const result = callback(key);
135+
const result = callback(this.data[key], key);
143136
if (result !== undefined) {
144137
return result;
145138
}
146139
}
147140
}
148141

149142
some(predicate: (value: T, key: string) => boolean): boolean {
150-
return this.someKey(key => predicate(this.data[key], key));
151-
}
152-
153-
someKey(predicate: (key: string) => boolean): boolean {
154143
for (const key in this.data) {
155-
if (predicate(key)) {
144+
if (predicate(this.data[key], key)) {
156145
return true;
157146
}
158147
}
@@ -939,17 +928,9 @@ namespace ts {
939928
: <T, U>(map: ShimMap<T>, callback: (value: T, key: string) => U | undefined) => map.forEachInMap(callback);
940929

941930
/** `forEachInMap` for just keys. */
942-
export const forEachKeyInMap: <T>(map: Map<{}>, callback: (key: string) => T | undefined) => T | undefined = usingNativeMaps
943-
? <T>(map: ES6Map<T>, callback: (key: string) => T | undefined) => {
944-
const iterator = map.keys();
945-
while (true) {
946-
const { value: key, done } = iterator.next();
947-
if (done) return undefined;
948-
const result = callback(key);
949-
if (result !== undefined) return result;
950-
}
951-
}
952-
: <T>(map: ShimMap<T>, callback: (key: string) => T | undefined) => map.forEachKey(callback);
931+
export function forEachKeyInMap<T>(map: Map<{}>, callback: (key: string) => T | undefined): T | undefined {
932+
return forEachInMap(map, (_, key) => callback(key));
933+
}
953934

954935
/** Whether `predicate` is true for some entry in the map. */
955936
export const someInMap: <T>(map: Map<T>, predicate: (value: T, key: string) => boolean) => boolean = usingNativeMaps
@@ -964,17 +945,10 @@ namespace ts {
964945
}
965946
: <T>(map: ShimMap<T>, predicate: (value: T, key: string) => boolean) => map.some(predicate);
966947

967-
/** Whether `predicate` is true for some key in the map. */
968-
export const someKeyInMap: (map: Map<{}>, predicate: (key: string) => boolean) => boolean = usingNativeMaps
969-
? (map: ES6Map<{}>, predicate: (key: string) => boolean) => {
970-
const iterator = map.keys();
971-
while (true) {
972-
const { value: key, done } = iterator.next();
973-
if (done) return false;
974-
if (predicate(key)) return true;
975-
}
976-
}
977-
: (map: ShimMap<{}>, predicate: (key: string) => boolean) => map.someKey(predicate);
948+
/** `someInMap` for just keys. */
949+
export function someKeyInMap(map: Map<{}>, predicate: (key: string) => boolean): boolean {
950+
return someInMap(map, (_, key) => predicate(key));
951+
}
978952

979953
/** Copy entries from `source` to `target`. */
980954
export function copyMapEntries<T>(source: Map<T>, target: Map<T>): void {

0 commit comments

Comments
 (0)