refactor: remove some facades (#12731)

This commit is contained in:
Victor Berchet 2016-11-08 15:43:24 -08:00 committed by vikerman
parent acbf1d859c
commit 7694f974af
4 changed files with 22 additions and 90 deletions

View File

@ -296,9 +296,9 @@ class _Scanner {
if (this.peek == chars.$u) { if (this.peek == chars.$u) {
// 4 character hex code for unicode character. // 4 character hex code for unicode character.
const hex: string = input.substring(this.index + 1, this.index + 5); const hex: string = input.substring(this.index + 1, this.index + 5);
try { if (/^[0-9a-f]+$/i.test(hex)) {
unescapedCode = NumberWrapper.parseInt(hex, 16); unescapedCode = parseInt(hex, 16);
} catch (e) { } else {
return this.error(`Invalid unicode escape [\\u${hex}]`, 0); return this.error(`Invalid unicode escape [\\u${hex}]`, 0);
} }
for (let i: number = 0; i < 5; i++) { for (let i: number = 0; i < 5; i++) {

View File

@ -13,7 +13,7 @@ import {getSymbolIterator, isJsObject, isPresent} from './lang';
*/ */
export class StringMapWrapper { export class StringMapWrapper {
static merge<V>(m1: {[key: string]: V}, m2: {[key: string]: V}): {[key: string]: V} { static merge<V>(m1: {[key: string]: V}, m2: {[key: string]: V}): {[key: string]: V} {
var m: {[key: string]: V} = {}; const m: {[key: string]: V} = {};
for (let k of Object.keys(m1)) { for (let k of Object.keys(m1)) {
m[k] = m1[k]; m[k] = m1[k];
@ -55,9 +55,11 @@ export class ListWrapper {
static removeAll<T>(list: T[], items: T[]) { static removeAll<T>(list: T[], items: T[]) {
for (let i = 0; i < items.length; ++i) { for (let i = 0; i < items.length; ++i) {
const index = list.indexOf(items[i]); const index = list.indexOf(items[i]);
if (index > -1) {
list.splice(index, 1); list.splice(index, 1);
} }
} }
}
static remove<T>(list: T[], el: T): boolean { static remove<T>(list: T[], el: T): boolean {
const index = list.indexOf(el); const index = list.indexOf(el);
@ -76,48 +78,14 @@ export class ListWrapper {
return true; return true;
} }
static maximum<T>(list: T[], predicate: (t: T) => number): T {
if (list.length == 0) {
return null;
}
var solution: any /** TODO #???? */ = null;
var maxValue = -Infinity;
for (var index = 0; index < list.length; index++) {
var candidate = list[index];
if (candidate == null) {
continue;
}
var candidateValue = predicate(candidate);
if (candidateValue > maxValue) {
solution = candidate;
maxValue = candidateValue;
}
}
return solution;
}
static flatten<T>(list: Array<T|T[]>): T[] { static flatten<T>(list: Array<T|T[]>): T[] {
var target: any[] = []; return list.reduce((flat: any[], item: T | T[]): T[] => {
_flattenArray(list, target); const flatItem = Array.isArray(item) ? ListWrapper.flatten(item) : item;
return target; return (<T[]>flat).concat(flatItem);
}, []);
} }
} }
function _flattenArray(source: any[], target: any[]): any[] {
if (isPresent(source)) {
for (let i = 0; i < source.length; i++) {
const item = source[i];
if (Array.isArray(item)) {
_flattenArray(item, target);
} else {
target.push(item);
}
}
}
return target;
}
export function isListLikeIterable(obj: any): boolean { export function isListLikeIterable(obj: any): boolean {
if (!isJsObject(obj)) return false; if (!isJsObject(obj)) return false;
return Array.isArray(obj) || return Array.isArray(obj) ||
@ -125,7 +93,8 @@ export function isListLikeIterable(obj: any): boolean {
getSymbolIterator() in obj); // JS Iterable have a Symbol.iterator prop getSymbolIterator() in obj); // JS Iterable have a Symbol.iterator prop
} }
export function areIterablesEqual(a: any, b: any, comparator: Function): boolean { export function areIterablesEqual(
a: any, b: any, comparator: (a: any, b: any) => boolean): boolean {
const iterator1 = a[getSymbolIterator()](); const iterator1 = a[getSymbolIterator()]();
const iterator2 = b[getSymbolIterator()](); const iterator2 = b[getSymbolIterator()]();
@ -138,9 +107,9 @@ export function areIterablesEqual(a: any, b: any, comparator: Function): boolean
} }
} }
export function iterateListLike(obj: any, fn: Function) { export function iterateListLike(obj: any, fn: (p: any) => any) {
if (Array.isArray(obj)) { if (Array.isArray(obj)) {
for (var i = 0; i < obj.length; i++) { for (let i = 0; i < obj.length; i++) {
fn(obj[i]); fn(obj[i]);
} }
} else { } else {

View File

@ -90,13 +90,14 @@ export function stringify(token: any): string {
return token; return token;
} }
if (token === undefined || token === null) { if (token == null) {
return '' + token; return '' + token;
} }
if (token.overriddenName) { if (token.overriddenName) {
return token.overriddenName; return token.overriddenName;
} }
if (token.name) { if (token.name) {
return token.name; return token.name;
} }
@ -115,24 +116,6 @@ export class NumberWrapper {
return result; return result;
} }
static parseInt(text: string, radix: number): number {
if (radix == 10) {
if (/^(\-|\+)?[0-9]+$/.test(text)) {
return parseInt(text, radix);
}
} else if (radix == 16) {
if (/^(\-|\+)?[0-9ABCDEFabcdef]+$/.test(text)) {
return parseInt(text, radix);
}
} else {
const result = parseInt(text, radix);
if (!isNaN(result)) {
return result;
}
}
throw new Error('Invalid integer literal when parsing ' + text + ' in base ' + radix);
}
static isNumeric(value: any): boolean { return !isNaN(value - parseFloat(value)); } static isNumeric(value: any): boolean { return !isNaN(value - parseFloat(value)); }
} }
@ -154,11 +137,11 @@ export function warn(obj: Error | Object) {
} }
export function setValueOnPath(global: any, path: string, value: any) { export function setValueOnPath(global: any, path: string, value: any) {
var parts = path.split('.'); const parts = path.split('.');
var obj: any = global; let obj: any = global;
while (parts.length > 1) { while (parts.length > 1) {
var name = parts.shift(); const name = parts.shift();
if (obj.hasOwnProperty(name) && isPresent(obj[name])) { if (obj.hasOwnProperty(name) && obj[name] != null) {
obj = obj[name]; obj = obj[name];
} else { } else {
obj = obj[name] = {}; obj = obj[name] = {};

View File

@ -6,29 +6,9 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {ListWrapper, StringMapWrapper} from '../src/collection'; import {StringMapWrapper} from '../src/collection';
export function main() { export function main() {
describe('ListWrapper', () => {
describe('maximum', () => {
it('should return the maximal element', () => {
expect(ListWrapper.maximum([1, 2, 3, 4], x => x)).toEqual(4);
});
it('should ignore null values', () => {
expect(ListWrapper.maximum([null, 2, 3, null], x => x)).toEqual(3);
});
it('should use the provided function to determine maximum', () => {
expect(ListWrapper.maximum([1, 2, 3, 4], x => -x)).toEqual(1);
});
it('should return null for an empty list',
() => { expect(ListWrapper.maximum([], x => x)).toEqual(null); });
});
});
describe('StringMapWrapper', () => { describe('StringMapWrapper', () => {
describe('equals', () => { describe('equals', () => {
it('should return true when comparing empty maps', it('should return true when comparing empty maps',