From 7694f974af7a6098d664ba36c0146adfa2d917f1 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Tue, 8 Nov 2016 15:43:24 -0800 Subject: [PATCH] refactor: remove some facades (#12731) --- .../compiler/src/expression_parser/lexer.ts | 6 +- modules/@angular/facade/src/collection.ts | 55 ++++--------------- modules/@angular/facade/src/lang.ts | 29 ++-------- .../@angular/facade/test/collection_spec.ts | 22 +------- 4 files changed, 22 insertions(+), 90 deletions(-) diff --git a/modules/@angular/compiler/src/expression_parser/lexer.ts b/modules/@angular/compiler/src/expression_parser/lexer.ts index 58e3e028b0..7694f4a239 100644 --- a/modules/@angular/compiler/src/expression_parser/lexer.ts +++ b/modules/@angular/compiler/src/expression_parser/lexer.ts @@ -296,9 +296,9 @@ class _Scanner { if (this.peek == chars.$u) { // 4 character hex code for unicode character. const hex: string = input.substring(this.index + 1, this.index + 5); - try { - unescapedCode = NumberWrapper.parseInt(hex, 16); - } catch (e) { + if (/^[0-9a-f]+$/i.test(hex)) { + unescapedCode = parseInt(hex, 16); + } else { return this.error(`Invalid unicode escape [\\u${hex}]`, 0); } for (let i: number = 0; i < 5; i++) { diff --git a/modules/@angular/facade/src/collection.ts b/modules/@angular/facade/src/collection.ts index a5c6fc07a7..f811a7c318 100644 --- a/modules/@angular/facade/src/collection.ts +++ b/modules/@angular/facade/src/collection.ts @@ -13,7 +13,7 @@ import {getSymbolIterator, isJsObject, isPresent} from './lang'; */ export class StringMapWrapper { static merge(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)) { m[k] = m1[k]; @@ -55,7 +55,9 @@ export class ListWrapper { static removeAll(list: T[], items: T[]) { for (let i = 0; i < items.length; ++i) { const index = list.indexOf(items[i]); - list.splice(index, 1); + if (index > -1) { + list.splice(index, 1); + } } } @@ -76,48 +78,14 @@ export class ListWrapper { return true; } - static maximum(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(list: Array): T[] { - var target: any[] = []; - _flattenArray(list, target); - return target; + return list.reduce((flat: any[], item: T | T[]): T[] => { + const flatItem = Array.isArray(item) ? ListWrapper.flatten(item) : item; + return (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 { if (!isJsObject(obj)) return false; return Array.isArray(obj) || @@ -125,7 +93,8 @@ export function isListLikeIterable(obj: any): boolean { 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 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)) { - for (var i = 0; i < obj.length; i++) { + for (let i = 0; i < obj.length; i++) { fn(obj[i]); } } else { diff --git a/modules/@angular/facade/src/lang.ts b/modules/@angular/facade/src/lang.ts index 193b39220b..6e2cda4177 100644 --- a/modules/@angular/facade/src/lang.ts +++ b/modules/@angular/facade/src/lang.ts @@ -90,13 +90,14 @@ export function stringify(token: any): string { return token; } - if (token === undefined || token === null) { + if (token == null) { return '' + token; } if (token.overriddenName) { return token.overriddenName; } + if (token.name) { return token.name; } @@ -115,24 +116,6 @@ export class NumberWrapper { 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)); } } @@ -154,11 +137,11 @@ export function warn(obj: Error | Object) { } export function setValueOnPath(global: any, path: string, value: any) { - var parts = path.split('.'); - var obj: any = global; + const parts = path.split('.'); + let obj: any = global; while (parts.length > 1) { - var name = parts.shift(); - if (obj.hasOwnProperty(name) && isPresent(obj[name])) { + const name = parts.shift(); + if (obj.hasOwnProperty(name) && obj[name] != null) { obj = obj[name]; } else { obj = obj[name] = {}; diff --git a/modules/@angular/facade/test/collection_spec.ts b/modules/@angular/facade/test/collection_spec.ts index d647e4d74e..760c965033 100644 --- a/modules/@angular/facade/test/collection_spec.ts +++ b/modules/@angular/facade/test/collection_spec.ts @@ -6,29 +6,9 @@ * found in the LICENSE file at https://angular.io/license */ -import {ListWrapper, StringMapWrapper} from '../src/collection'; +import {StringMapWrapper} from '../src/collection'; 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('equals', () => { it('should return true when comparing empty maps',