From a0a627f2f98146c2c0e37aca7b081096e18d8261 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Sat, 31 Oct 2015 13:04:26 -0700 Subject: [PATCH] refactor(StringWrapper): remove useless methods Closes #5039 --- modules/angular1_router/karma-router.conf.js | 1 + modules/angular1_router/lib/facades.es5 | 8 -------- .../src/common/pipes/lowercase_pipe.ts | 4 ++-- .../src/common/pipes/uppercase_pipe.ts | 4 ++-- .../angular2/src/compiler/template_parser.ts | 7 +++---- .../src/core/dom/generic_browser_adapter.ts | 4 ++-- modules/angular2/src/core/facade/lang.dart | 16 ---------------- modules/angular2/src/core/facade/lang.ts | 10 ---------- .../angular2/src/core/linker/event_config.ts | 5 ++--- modules/angular2/src/core/metadata/di.ts | 11 ++--------- .../src/http/backends/jsonp_backend.ts | 3 +-- .../angular2/src/http/url_search_params.ts | 6 +++--- .../angular2/src/router/path_recognizer.ts | 4 ++-- .../angular2/src/router/route_recognizer.ts | 8 +++----- modules/angular2/src/router/router.ts | 13 +++---------- modules/angular2/src/router/url_parser.ts | 12 +++--------- modules/angular2/src/testing/utils.ts | 2 +- .../angular2/test/core/facade/lang_spec.ts | 19 +------------------ .../src/webdriver/chrome_driver_extension.ts | 6 ++---- 19 files changed, 33 insertions(+), 110 deletions(-) diff --git a/modules/angular1_router/karma-router.conf.js b/modules/angular1_router/karma-router.conf.js index e9980a724f..f3cb92c5b7 100644 --- a/modules/angular1_router/karma-router.conf.js +++ b/modules/angular1_router/karma-router.conf.js @@ -9,6 +9,7 @@ module.exports = function (config) { frameworks: ['jasmine'], files: [ + '../../node_modules/es6-shim/es6-shim.js', '../../node_modules/angular/angular.js', '../../node_modules/angular-animate/angular-animate.js', '../../node_modules/angular-mocks/angular-mocks.js', diff --git a/modules/angular1_router/lib/facades.es5 b/modules/angular1_router/lib/facades.es5 index 34166736a1..84ddf2bdaa 100644 --- a/modules/angular1_router/lib/facades.es5 +++ b/modules/angular1_router/lib/facades.es5 @@ -247,18 +247,10 @@ var StringWrapper = { return s.split(re); }, - substring: function(s, start, end) { - return s.substr(start, end); - }, - replaceAll: function(s, from, replace) { return s.replace(from, replace); }, - startsWith: function(s, start) { - return s.substr(0, start.length) === start; - }, - replaceAllMapped: function(s, from, cb) { return s.replace(from, function(matches) { // Remove offset & string from the result array diff --git a/modules/angular2/src/common/pipes/lowercase_pipe.ts b/modules/angular2/src/common/pipes/lowercase_pipe.ts index 32fb499264..9af7d7f034 100644 --- a/modules/angular2/src/common/pipes/lowercase_pipe.ts +++ b/modules/angular2/src/common/pipes/lowercase_pipe.ts @@ -1,4 +1,4 @@ -import {isString, StringWrapper, CONST, isBlank} from 'angular2/src/core/facade/lang'; +import {isString, CONST, isBlank} from 'angular2/src/core/facade/lang'; import {Injectable} from 'angular2/src/core/di'; import {PipeTransform, WrappedValue} from 'angular2/src/core/change_detection'; import {Pipe} from 'angular2/src/core/metadata'; @@ -32,6 +32,6 @@ export class LowerCasePipe implements PipeTransform { if (!isString(value)) { throw new InvalidPipeArgumentException(LowerCasePipe, value); } - return StringWrapper.toLowerCase(value); + return value.toLowerCase(); } } diff --git a/modules/angular2/src/common/pipes/uppercase_pipe.ts b/modules/angular2/src/common/pipes/uppercase_pipe.ts index cb122e47b0..b438cd1505 100644 --- a/modules/angular2/src/common/pipes/uppercase_pipe.ts +++ b/modules/angular2/src/common/pipes/uppercase_pipe.ts @@ -1,4 +1,4 @@ -import {isString, StringWrapper, CONST, isBlank} from 'angular2/src/core/facade/lang'; +import {isString, CONST, isBlank} from 'angular2/src/core/facade/lang'; import {Pipe} from 'angular2/src/core/metadata'; import {Injectable} from 'angular2/src/core/di'; import {PipeTransform, WrappedValue} from 'angular2/src/core/change_detection'; @@ -31,6 +31,6 @@ export class UpperCasePipe implements PipeTransform { if (!isString(value)) { throw new InvalidPipeArgumentException(UpperCasePipe, value); } - return StringWrapper.toUpperCase(value); + return value.toUpperCase(); } } diff --git a/modules/angular2/src/compiler/template_parser.ts b/modules/angular2/src/compiler/template_parser.ts index 38fb2cd1c5..a4d0c66388 100644 --- a/modules/angular2/src/compiler/template_parser.ts +++ b/modules/angular2/src/compiler/template_parser.ts @@ -254,8 +254,8 @@ class TemplateParseVisitor implements HtmlAstVisitor { var templateBindingsSource = null; if (attr.name == TEMPLATE_ATTR) { templateBindingsSource = attr.value; - } else if (StringWrapper.startsWith(attr.name, TEMPLATE_ATTR_PREFIX)) { - var key = StringWrapper.substring(attr.name, TEMPLATE_ATTR_PREFIX.length); // remove the star + } else if (attr.name.startsWith(TEMPLATE_ATTR_PREFIX)) { + var key = attr.name.substring(TEMPLATE_ATTR_PREFIX.length); // remove the star templateBindingsSource = (attr.value.length == 0) ? key : key + ' ' + attr.value; } if (isPresent(templateBindingsSource)) { @@ -333,8 +333,7 @@ class TemplateParseVisitor implements HtmlAstVisitor { } private _normalizeAttributeName(attrName: string): string { - return StringWrapper.startsWith(attrName, 'data-') ? StringWrapper.substring(attrName, 5) : - attrName; + return attrName.startsWith('data-') ? attrName.substring(5) : attrName; } private _parseVariable(identifier: string, value: string, sourceInfo: any, diff --git a/modules/angular2/src/core/dom/generic_browser_adapter.ts b/modules/angular2/src/core/dom/generic_browser_adapter.ts index d1b7fe78fa..3bb84aaa91 100644 --- a/modules/angular2/src/core/dom/generic_browser_adapter.ts +++ b/modules/angular2/src/core/dom/generic_browser_adapter.ts @@ -1,5 +1,5 @@ import {ListWrapper, StringMapWrapper} from 'angular2/src/core/facade/collection'; -import {isPresent, isFunction, StringWrapper, Type} from 'angular2/src/core/facade/lang'; +import {isPresent, isFunction, Type} from 'angular2/src/core/facade/lang'; import {DomAdapter} from './dom_adapter'; import {XHRImpl} from 'angular2/src/compiler/xhr_impl'; @@ -20,7 +20,7 @@ export abstract class GenericBrowserDomAdapter extends DomAdapter { var domPrefixes = ['Webkit', 'Moz', 'O', 'ms']; for (var i = 0; i < domPrefixes.length; i++) { if (isPresent(this.getStyle(element, domPrefixes[i] + 'AnimationName'))) { - this._animationPrefix = '-' + StringWrapper.toLowerCase(domPrefixes[i]) + '-'; + this._animationPrefix = '-' + domPrefixes[i].toLowerCase() + '-'; break; } } diff --git a/modules/angular2/src/core/facade/lang.dart b/modules/angular2/src/core/facade/lang.dart index 44d343d493..ba3c5c71ba 100644 --- a/modules/angular2/src/core/facade/lang.dart +++ b/modules/angular2/src/core/facade/lang.dart @@ -80,18 +80,6 @@ class StringWrapper { return s.replaceAll(from, replace); } - static String toUpperCase(String s) { - return s.toUpperCase(); - } - - static String toLowerCase(String s) { - return s.toLowerCase(); - } - - static startsWith(String s, String start) { - return s.startsWith(start); - } - static String slice(String s, [int start = 0, int end]) { start = _startOffset(s, start); end = _endOffset(s, end); @@ -102,10 +90,6 @@ class StringWrapper { return s.substring(start, end); } - static String substring(String s, int start, [int end]) { - return s.substring(start, end); - } - static String replaceAllMapped(String s, RegExp from, Function cb) { return s.replaceAllMapped(from, cb); } diff --git a/modules/angular2/src/core/facade/lang.ts b/modules/angular2/src/core/facade/lang.ts index 4a6351bf7a..3fc2a8fcfc 100644 --- a/modules/angular2/src/core/facade/lang.ts +++ b/modules/angular2/src/core/facade/lang.ts @@ -176,16 +176,6 @@ export class StringWrapper { return s.slice(from, to === null ? undefined : to); } - static toUpperCase(s: string): string { return s.toUpperCase(); } - - static toLowerCase(s: string): string { return s.toLowerCase(); } - - static startsWith(s: string, start: string): boolean { return s.startsWith(start); } - - static substring(s: string, start: number, end: number = null): string { - return s.substring(start, end === null ? undefined : end); - } - static replaceAllMapped(s: string, from: RegExp, cb: Function): string { return s.replace(from, function(...matches) { // Remove offset & string from the result array diff --git a/modules/angular2/src/core/linker/event_config.ts b/modules/angular2/src/core/linker/event_config.ts index 618c7986b1..fdbcf2baa0 100644 --- a/modules/angular2/src/core/linker/event_config.ts +++ b/modules/angular2/src/core/linker/event_config.ts @@ -1,4 +1,3 @@ -import {StringWrapper} from 'angular2/src/core/facade/lang'; export const EVENT_TARGET_SEPARATOR = ':'; export class EventConfig { @@ -9,8 +8,8 @@ export class EventConfig { var separatorIdx = eventConfig.indexOf(EVENT_TARGET_SEPARATOR); if (separatorIdx > -1) { // long format: 'fieldName: eventName' - fieldName = StringWrapper.substring(eventConfig, 0, separatorIdx).trim(); - eventName = StringWrapper.substring(eventConfig, separatorIdx + 1).trim(); + fieldName = eventConfig.substring(0, separatorIdx).trim(); + eventName = eventConfig.substring(separatorIdx + 1).trim(); isLongForm = true; } return new EventConfig(fieldName, eventName, isLongForm); diff --git a/modules/angular2/src/core/metadata/di.ts b/modules/angular2/src/core/metadata/di.ts index 29c65fcc67..4474c15cf0 100644 --- a/modules/angular2/src/core/metadata/di.ts +++ b/modules/angular2/src/core/metadata/di.ts @@ -1,11 +1,4 @@ -import { - CONST, - Type, - stringify, - isPresent, - StringWrapper, - isString -} from 'angular2/src/core/facade/lang'; +import {CONST, Type, stringify, isPresent, isString} from 'angular2/src/core/facade/lang'; import {resolveForwardRef} from 'angular2/src/core/di'; import {DependencyMetadata} from 'angular2/src/core/di/metadata'; @@ -192,7 +185,7 @@ export class QueryMetadata extends DependencyMetadata { * returns a list of variable bindings this is querying for. * Only applicable if this is a variable bindings query. */ - get varBindings(): string[] { return StringWrapper.split(this.selector, new RegExp(",")); } + get varBindings(): string[] { return this.selector.split(','); } toString(): string { return `@Query(${stringify(this.selector)})`; } } diff --git a/modules/angular2/src/http/backends/jsonp_backend.ts b/modules/angular2/src/http/backends/jsonp_backend.ts index d3a56ea1ea..1a91366e89 100644 --- a/modules/angular2/src/http/backends/jsonp_backend.ts +++ b/modules/angular2/src/http/backends/jsonp_backend.ts @@ -46,8 +46,7 @@ export class JSONPConnection_ extends JSONPConnection { if (url.indexOf('=JSONP_CALLBACK&') > -1) { url = StringWrapper.replace(url, '=JSONP_CALLBACK&', `=${callback}&`); } else if (url.lastIndexOf('=JSONP_CALLBACK') === url.length - '=JSONP_CALLBACK'.length) { - url = - StringWrapper.substring(url, 0, url.length - '=JSONP_CALLBACK'.length) + `=${callback}`; + url = url.substring(0, url.length - '=JSONP_CALLBACK'.length) + `=${callback}`; } let script = this._script = _dom.build(url); diff --git a/modules/angular2/src/http/url_search_params.ts b/modules/angular2/src/http/url_search_params.ts index 96c23ed461..253674973d 100644 --- a/modules/angular2/src/http/url_search_params.ts +++ b/modules/angular2/src/http/url_search_params.ts @@ -1,4 +1,4 @@ -import {CONST_EXPR, isPresent, isBlank, StringWrapper} from 'angular2/src/core/facade/lang'; +import {CONST_EXPR, isPresent, isBlank} from 'angular2/src/core/facade/lang'; import { Map, MapWrapper, @@ -9,9 +9,9 @@ import { function paramParser(rawParams: string = ''): Map { var map = new Map(); if (rawParams.length > 0) { - var params: string[] = StringWrapper.split(rawParams, new RegExp('&')); + var params: string[] = rawParams.split('&'); params.forEach((param: string) => { - var split: string[] = StringWrapper.split(param, new RegExp('=')); + var split: string[] = param.split('='); var key = split[0]; var val = split[1]; var list = isPresent(map.get(key)) ? map.get(key) : []; diff --git a/modules/angular2/src/router/path_recognizer.ts b/modules/angular2/src/router/path_recognizer.ts index 7a167490b8..54d0961ad7 100644 --- a/modules/angular2/src/router/path_recognizer.ts +++ b/modules/angular2/src/router/path_recognizer.ts @@ -93,8 +93,8 @@ var wildcardMatcher = /^\*([^\/]+)$/g; function parsePathString(route: string): {[key: string]: any} { // normalize route as not starting with a "/". Recognition will // also normalize. - if (StringWrapper.startsWith(route, "/")) { - route = StringWrapper.substring(route, 1); + if (route.startsWith("/")) { + route = route.substring(1); } var segments = splitBySlash(route); diff --git a/modules/angular2/src/router/route_recognizer.ts b/modules/angular2/src/router/route_recognizer.ts index 42904412cf..5bd9e0b0a5 100644 --- a/modules/angular2/src/router/route_recognizer.ts +++ b/modules/angular2/src/router/route_recognizer.ts @@ -1,7 +1,6 @@ import { RegExp, RegExpWrapper, - StringWrapper, isBlank, isPresent, isType, @@ -46,8 +45,7 @@ export class RouteRecognizer { if (config instanceof AuxRoute) { handler = new SyncRouteHandler(config.component, config.data); - let path = - StringWrapper.startsWith(config.path, '/') ? config.path.substring(1) : config.path; + let path = config.path.startsWith('/') ? config.path.substring(1) : config.path; var recognizer = new PathRecognizer(config.path, handler); this.auxRoutes.set(path, recognizer); return recognizer.terminal; @@ -136,11 +134,11 @@ export class Redirector { toSegments: string[] = []; constructor(path: string, redirectTo: string) { - if (StringWrapper.startsWith(path, '/')) { + if (path.startsWith('/')) { path = path.substring(1); } this.segments = path.split('/'); - if (StringWrapper.startsWith(redirectTo, '/')) { + if (redirectTo.startsWith('/')) { redirectTo = redirectTo.substring(1); } this.toSegments = redirectTo.split('/'); diff --git a/modules/angular2/src/router/router.ts b/modules/angular2/src/router/router.ts index b91335c2b7..1bf7ce5069 100644 --- a/modules/angular2/src/router/router.ts +++ b/modules/angular2/src/router/router.ts @@ -5,14 +5,7 @@ import { ObservableWrapper } from 'angular2/src/core/facade/async'; import {Map, StringMapWrapper, MapWrapper, ListWrapper} from 'angular2/src/core/facade/collection'; -import { - isBlank, - isString, - StringWrapper, - isPresent, - Type, - isArray -} from 'angular2/src/core/facade/lang'; +import {isBlank, isString, isPresent, Type, isArray} from 'angular2/src/core/facade/lang'; import {BaseException, WrappedException} from 'angular2/src/core/facade/exceptions'; import {RouteRegistry} from './route_registry'; import { @@ -537,11 +530,11 @@ class ChildRouter extends Router { * Given: ['/a/b', {c: 2}] * Returns: ['', 'a', 'b', {c: 2}] */ -var SLASH = new RegExp('/'); function splitAndFlattenLinkParams(linkParams: any[]): any[] { return ListWrapper.reduce(linkParams, (accumulation, item) => { if (isString(item)) { - return accumulation.concat(StringWrapper.split(item, SLASH)); + let parts: String[] = item.split('/'); + return accumulation.concat(parts); } accumulation.push(item); return accumulation; diff --git a/modules/angular2/src/router/url_parser.ts b/modules/angular2/src/router/url_parser.ts index 4fc5f445d5..541021fbf0 100644 --- a/modules/angular2/src/router/url_parser.ts +++ b/modules/angular2/src/router/url_parser.ts @@ -1,11 +1,5 @@ import {StringMapWrapper} from 'angular2/src/core/facade/collection'; -import { - isPresent, - isBlank, - StringWrapper, - RegExpWrapper, - CONST_EXPR -} from 'angular2/src/core/facade/lang'; +import {isPresent, isBlank, RegExpWrapper, CONST_EXPR} from 'angular2/src/core/facade/lang'; import {BaseException, WrappedException} from 'angular2/src/core/facade/exceptions'; /** @@ -79,10 +73,10 @@ function matchUrlSegment(str: string): string { export class UrlParser { private _remaining: string; - peekStartsWith(str: string): boolean { return StringWrapper.startsWith(this._remaining, str); } + peekStartsWith(str: string): boolean { return this._remaining.startsWith(str); } capture(str: string): void { - if (!StringWrapper.startsWith(this._remaining, str)) { + if (!this._remaining.startsWith(str)) { throw new BaseException(`Expected "${str}".`); } this._remaining = this._remaining.substring(str.length); diff --git a/modules/angular2/src/testing/utils.ts b/modules/angular2/src/testing/utils.ts index 281c818a52..e6811d4718 100644 --- a/modules/angular2/src/testing/utils.ts +++ b/modules/angular2/src/testing/utils.ts @@ -99,7 +99,7 @@ var _singleTagWhitelist = ['br', 'hr', 'input']; export function stringifyElement(el): string { var result = ''; if (DOM.isElementNode(el)) { - var tagName = StringWrapper.toLowerCase(DOM.tagName(el)); + var tagName = DOM.tagName(el).toLowerCase(); // Opening tag result += `<${tagName}`; diff --git a/modules/angular2/test/core/facade/lang_spec.ts b/modules/angular2/test/core/facade/lang_spec.ts index 16b23d0af9..4a1a7b8f65 100644 --- a/modules/angular2/test/core/facade/lang_spec.ts +++ b/modules/angular2/test/core/facade/lang_spec.ts @@ -42,24 +42,7 @@ export function main() { }); describe('String', () => { - var upper, lower, s; - - beforeEach(() => { - upper = 'SOMETHING'; - lower = 'something'; - }); - - it('should upper case a string', () => { - var str = StringWrapper.toUpperCase(lower); - - expect(str).toEqual(upper); - }); - - it('should lower case a string', () => { - var str = StringWrapper.toLowerCase(upper); - - expect(str).toEqual(lower); - }); + var s; describe('slice', () => { beforeEach(() => { s = "abcdefghij"; }); diff --git a/modules/benchpress/src/webdriver/chrome_driver_extension.ts b/modules/benchpress/src/webdriver/chrome_driver_extension.ts index 90b45c902e..6c13e27f0a 100644 --- a/modules/benchpress/src/webdriver/chrome_driver_extension.ts +++ b/modules/benchpress/src/webdriver/chrome_driver_extension.ts @@ -41,7 +41,7 @@ export class ChromeDriverExtension extends WebDriverExtension { if (isBlank(v)) { return -1; } - v = StringWrapper.split(v, /\./g)[0]; + v = v.split('.')[0]; if (isBlank(v)) { return -1; } @@ -197,9 +197,7 @@ export class ChromeDriverExtension extends WebDriverExtension { return null; // nothing useful in this event } - private _parseCategories(categories: string): string[] { - return StringWrapper.split(categories, /,/g); - } + private _parseCategories(categories: string): string[] { return categories.split(','); } private _isEvent(eventCategories: string[], eventName: string, expectedCategories: string[], expectedName: string = null): boolean {