refactor: remove some facades (#12731)
This commit is contained in:
parent
acbf1d859c
commit
7694f974af
|
@ -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++) {
|
||||
|
|
|
@ -13,7 +13,7 @@ import {getSymbolIterator, isJsObject, isPresent} from './lang';
|
|||
*/
|
||||
export class StringMapWrapper {
|
||||
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)) {
|
||||
m[k] = m1[k];
|
||||
|
@ -55,7 +55,9 @@ export class ListWrapper {
|
|||
static removeAll<T>(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<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[] {
|
||||
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 (<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 {
|
||||
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 {
|
||||
|
|
|
@ -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] = {};
|
||||
|
|
|
@ -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',
|
||||
|
|
Loading…
Reference in New Issue