refactor(facade): inline StringWrapper (#12051)

This commit is contained in:
Alex Eagle
2016-10-06 15:10:27 -07:00
committed by Tobias Bosch
parent bb35fcb562
commit 8c975ed156
34 changed files with 105 additions and 288 deletions

View File

@ -137,73 +137,6 @@ export function stringify(token: any): string {
return newLineIndex === -1 ? res : res.substring(0, newLineIndex);
}
export class StringWrapper {
static fromCharCode(code: number): string { return String.fromCharCode(code); }
static charCodeAt(s: string, index: number): number { return s.charCodeAt(index); }
static split(s: string, regExp: RegExp): string[] { return s.split(regExp); }
static equals(s: string, s2: string): boolean { return s === s2; }
static stripLeft(s: string, charVal: string): string {
if (s && s.length) {
var pos = 0;
for (var i = 0; i < s.length; i++) {
if (s[i] != charVal) break;
pos++;
}
s = s.substring(pos);
}
return s;
}
static stripRight(s: string, charVal: string): string {
if (s && s.length) {
var pos = s.length;
for (var i = s.length - 1; i >= 0; i--) {
if (s[i] != charVal) break;
pos--;
}
s = s.substring(0, pos);
}
return s;
}
static replace(s: string, from: string, replace: string): string {
return s.replace(from, replace);
}
static replaceAll(s: string, from: RegExp, replace: string): string {
return s.replace(from, replace);
}
static slice<T>(s: string, from: number = 0, to: number = null): string {
return s.slice(from, to === null ? undefined : to);
}
static replaceAllMapped(s: string, from: RegExp, cb: (m: string[]) => string): string {
return s.replace(from, function(...matches: any[]) {
// Remove offset & string from the result array
matches.splice(-2, 2);
// The callback receives match, p1, ..., pn
return cb(matches);
});
}
static contains(s: string, substr: string): boolean { return s.indexOf(substr) != -1; }
static compare(a: string, b: string): number {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
}
}
export class StringJoiner {
constructor(public parts: string[] = []) {}

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {NumberWrapper, StringWrapper, escapeRegExp, hasConstructor} from '../src/lang';
import {NumberWrapper, escapeRegExp, hasConstructor} from '../src/lang';
class MySuperclass {}
class MySubclass extends MySuperclass {}
@ -50,92 +50,4 @@ export function main() {
() => { expect(NumberWrapper.isNumeric('2a')).toBe(false); });
});
});
describe('String', () => {
var s: string;
describe('slice', () => {
beforeEach(() => { s = 'abcdefghij'; });
it('should return the whole string if neither start nor end are specified',
() => { expect(StringWrapper.slice(s)).toEqual('abcdefghij'); });
it('should return up to the end if end is not specified',
() => { expect(StringWrapper.slice(s, 1)).toEqual('bcdefghij'); });
it('should support negative start',
() => { expect(StringWrapper.slice(s, -1)).toEqual('j'); });
it('should support negative end',
() => { expect(StringWrapper.slice(s, -3, -1)).toEqual('hi'); });
it('should return empty string if start is greater than end', () => {
expect(StringWrapper.slice(s, 4, 2)).toEqual('');
expect(StringWrapper.slice(s, -2, -4)).toEqual('');
});
});
describe('stripLeft', () => {
it('should strip the first character of the string if it matches the provided input', () => {
var input = '~angular2 is amazing';
var expectedOutput = 'angular2 is amazing';
expect(StringWrapper.stripLeft(input, '~')).toEqual(expectedOutput);
});
it('should keep stripping characters from the start until the first unmatched character',
() => {
var input = '#####hello';
var expectedOutput = 'hello';
expect(StringWrapper.stripLeft(input, '#')).toEqual(expectedOutput);
});
it('should not alter the provided input if the first character does not match the provided input',
() => {
var input = '+angular2 is amazing';
expect(StringWrapper.stripLeft(input, '*')).toEqual(input);
});
it('should not do any alterations when an empty string or null value is passed in', () => {
expect(StringWrapper.stripLeft('', 'S')).toEqual('');
expect(StringWrapper.stripLeft(null, 'S')).toEqual(null);
});
});
describe('stripRight', () => {
it('should strip the first character of the string if it matches the provided input', () => {
var input = 'angular2 is amazing!';
var expectedOutput = 'angular2 is amazing';
expect(StringWrapper.stripRight(input, '!')).toEqual(expectedOutput);
});
it('should not alter the provided input if the first character does not match the provided input',
() => {
var input = 'angular2 is amazing+';
expect(StringWrapper.stripRight(input, '*')).toEqual(input);
});
it('should keep stripping characters from the end until the first unmatched character',
() => {
var input = 'hi&!&&&&&';
var expectedOutput = 'hi&!';
expect(StringWrapper.stripRight(input, '&')).toEqual(expectedOutput);
});
it('should not do any alterations when an empty string or null value is passed in', () => {
expect(StringWrapper.stripRight('', 'S')).toEqual('');
expect(StringWrapper.stripRight(null, 'S')).toEqual(null);
});
});
describe('hasConstructor', () => {
it('should be true when the type matches',
() => { expect(hasConstructor(new MySuperclass(), MySuperclass)).toEqual(true); });
it('should be false for subtypes',
() => { expect(hasConstructor(new MySubclass(), MySuperclass)).toEqual(false); });
});
});
}