From b15ffda7f699097c7abf49252558dcafb3da3a50 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Thu, 8 Dec 2016 06:55:49 -0800 Subject: [PATCH] Simplify forEachKeyInMap and someKeyInMap --- src/compiler/checker.ts | 2 +- src/compiler/core.ts | 46 +++++++++-------------------------------- 2 files changed, 11 insertions(+), 37 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 00a6ad2c366..67c4db715e2 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -18991,7 +18991,7 @@ namespace ts { } function hasExportedMembers(moduleSymbol: Symbol) { - return someKeyInMap(moduleSymbol.exports, id => id !== "export="); + return someInMap(moduleSymbol.exports, (_, id) => id !== "export="); } function checkExternalModuleExports(node: SourceFile | ModuleDeclaration) { diff --git a/src/compiler/core.ts b/src/compiler/core.ts index affd225b3a7..3fe3b386701 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -68,7 +68,6 @@ namespace ts { interface ES6Map extends Map { readonly size: number; entries(): Iterator<[string, T]>; - keys(): Iterator; } /** ES6 Iterator type. */ @@ -80,9 +79,7 @@ namespace ts { interface ShimMap extends Map { isEmpty(): boolean; forEachInMap(callback: (value: T, key: string) => U | undefined): U | undefined; - forEachKey(callback: (key: string) => U | undefined): U | undefined; some(predicate: (value: T, key: string) => boolean): boolean; - someKey(predicate: (key: string) => boolean): boolean; } // The global Map object. This may not be available, so we must test for it. @@ -130,16 +127,12 @@ namespace ts { } isEmpty(): boolean { - return !this.someKey(() => true); + return !this.some(() => true); } forEachInMap(callback: (value: T, key: string) => U | undefined): U | undefined { - return this.forEachKey(key => callback(this.data[key], key)); - } - - forEachKey(callback: (key: string) => U | undefined): U | undefined { for (const key in this.data) { - const result = callback(key); + const result = callback(this.data[key], key); if (result !== undefined) { return result; } @@ -147,12 +140,8 @@ namespace ts { } some(predicate: (value: T, key: string) => boolean): boolean { - return this.someKey(key => predicate(this.data[key], key)); - } - - someKey(predicate: (key: string) => boolean): boolean { for (const key in this.data) { - if (predicate(key)) { + if (predicate(this.data[key], key)) { return true; } } @@ -939,17 +928,9 @@ namespace ts { : (map: ShimMap, callback: (value: T, key: string) => U | undefined) => map.forEachInMap(callback); /** `forEachInMap` for just keys. */ - export const forEachKeyInMap: (map: Map<{}>, callback: (key: string) => T | undefined) => T | undefined = usingNativeMaps - ? (map: ES6Map, callback: (key: string) => T | undefined) => { - const iterator = map.keys(); - while (true) { - const { value: key, done } = iterator.next(); - if (done) return undefined; - const result = callback(key); - if (result !== undefined) return result; - } - } - : (map: ShimMap, callback: (key: string) => T | undefined) => map.forEachKey(callback); + export function forEachKeyInMap(map: Map<{}>, callback: (key: string) => T | undefined): T | undefined { + return forEachInMap(map, (_, key) => callback(key)); + } /** Whether `predicate` is true for some entry in the map. */ export const someInMap: (map: Map, predicate: (value: T, key: string) => boolean) => boolean = usingNativeMaps @@ -964,17 +945,10 @@ namespace ts { } : (map: ShimMap, predicate: (value: T, key: string) => boolean) => map.some(predicate); - /** Whether `predicate` is true for some key in the map. */ - export const someKeyInMap: (map: Map<{}>, predicate: (key: string) => boolean) => boolean = usingNativeMaps - ? (map: ES6Map<{}>, predicate: (key: string) => boolean) => { - const iterator = map.keys(); - while (true) { - const { value: key, done } = iterator.next(); - if (done) return false; - if (predicate(key)) return true; - } - } - : (map: ShimMap<{}>, predicate: (key: string) => boolean) => map.someKey(predicate); + /** `someInMap` for just keys. */ + export function someKeyInMap(map: Map<{}>, predicate: (key: string) => boolean): boolean { + return someInMap(map, (_, key) => predicate(key)); + } /** Copy entries from `source` to `target`. */ export function copyMapEntries(source: Map, target: Map): void {