Addressed code review feedback.

This commit is contained in:
Daniel Rosenwasser
2014-08-04 17:52:58 -07:00
parent a64db42337
commit 348d0fca21
2 changed files with 22 additions and 25 deletions

View File

@@ -39,7 +39,7 @@ function main(): void {
function buildUniqueNameMap(names: string[]): IIndexable<string> {
var nameMap: IIndexable<string> = {};
var uniqueNames = NameGenerator.ensureUniqueness(names, /*isFixed */ undefined, /* isCaseSensitive */ false);
var uniqueNames = NameGenerator.ensureUniqueness(names, /* isCaseSensitive */ false, /* isFixed */ undefined);
for (var i = 0; i < names.length; i++) {
nameMap[names[i]] = uniqueNames[i];
@@ -94,14 +94,17 @@ function convertPropertyName(origName: string): string {
}
module NameGenerator {
export function ensureUniqueness(names: string[], isFixed: boolean[] = names.map(() => false), isCaseSensitive: boolean = false): string[] {
export function ensureUniqueness(names: string[], isCaseSensitive: boolean, isFixed?: boolean[]): string[]{
if (!isFixed) {
isFixed = names.map(() => false)
}
var names = names.map(x => x);
ensureUniquenessInPlace(names, isFixed, isCaseSensitive);
var names = names.slice();
ensureUniquenessInPlace(names, isCaseSensitive, isFixed);
return names;
}
function ensureUniquenessInPlace(names: string[], isFixed: boolean[], isCaseSensitive: boolean): void {
function ensureUniquenessInPlace(names: string[], isCaseSensitive: boolean, isFixed: boolean[]): void {
for (var i = 0; i < names.length; i++) {
var name = names[i];
var collisionIndices = Utilities.collectMatchingIndices(name, names, isCaseSensitive);
@@ -128,9 +131,12 @@ module NameGenerator {
}
while (true) {
var newName = name + suffix++;
var newName = name + suffix;
suffix++;
if (!proposedNames.some((name) => Utilities.stringEquals(name, newName, isCaseSensitive))) {
// Check if we've synthesized a unique name, and if so
// replace the conflicting name with the new one.
if (!proposedNames.some(name => Utilities.stringEquals(name, newName, isCaseSensitive))) {
proposedNames[collisionIndex] = newName;
break;
}
@@ -141,7 +147,7 @@ module NameGenerator {
module Utilities {
/// Return a list of all indices where a string occurs.
export function collectMatchingIndices(name: string, proposedNames: string[], isCaseSensitive: boolean = false): number[] {
export function collectMatchingIndices(name: string, proposedNames: string[], isCaseSensitive: boolean): number[] {
var matchingIndices: number[] = [];
for (var i = 0; i < proposedNames.length; i++) {
@@ -153,7 +159,7 @@ module Utilities {
return matchingIndices;
}
export function stringEquals(s1: string, s2: string, caseSensitive: boolean = false): boolean {
export function stringEquals(s1: string, s2: string, caseSensitive: boolean): boolean {
if (caseSensitive) {
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();