refactor(common): remove deprecated ReplacePipe (#10772)
BREAKING CHANGE: previously deprecated ReplacePipe was removed
This commit is contained in:
parent
3329977ec9
commit
33ced7088f
|
@ -20,6 +20,5 @@ export {I18nSelectPipe} from './pipes/i18n_select_pipe';
|
|||
export {JsonPipe} from './pipes/json_pipe';
|
||||
export {LowerCasePipe} from './pipes/lowercase_pipe';
|
||||
export {CurrencyPipe, DecimalPipe, PercentPipe} from './pipes/number_pipe';
|
||||
export {ReplacePipe} from './pipes/replace_pipe';
|
||||
export {SlicePipe} from './pipes/slice_pipe';
|
||||
export {UpperCasePipe} from './pipes/uppercase_pipe';
|
||||
|
|
|
@ -18,7 +18,6 @@ import {I18nSelectPipe} from './i18n_select_pipe';
|
|||
import {JsonPipe} from './json_pipe';
|
||||
import {LowerCasePipe} from './lowercase_pipe';
|
||||
import {CurrencyPipe, DecimalPipe, PercentPipe} from './number_pipe';
|
||||
import {ReplacePipe} from './replace_pipe';
|
||||
import {SlicePipe} from './slice_pipe';
|
||||
import {UpperCasePipe} from './uppercase_pipe';
|
||||
|
||||
|
@ -42,7 +41,6 @@ export const COMMON_PIPES = [
|
|||
PercentPipe,
|
||||
CurrencyPipe,
|
||||
DatePipe,
|
||||
ReplacePipe,
|
||||
I18nPluralPipe,
|
||||
I18nSelectPipe,
|
||||
];
|
||||
|
|
|
@ -1,87 +0,0 @@
|
|||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Pipe, PipeTransform} from '@angular/core';
|
||||
import {StringWrapper, isBlank, isFunction, isNumber, isString} from '../facade/lang';
|
||||
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
|
||||
|
||||
/**
|
||||
* Creates a new String with some or all of the matches of a pattern replaced by
|
||||
* a replacement.
|
||||
*
|
||||
* The pattern to be matched is specified by the 'pattern' parameter.
|
||||
*
|
||||
* The replacement to be set is specified by the 'replacement' parameter.
|
||||
*
|
||||
* An optional 'flags' parameter can be set.
|
||||
*
|
||||
* ### Usage
|
||||
*
|
||||
* expression | replace:pattern:replacement
|
||||
*
|
||||
* All behavior is based on the expected behavior of the JavaScript API
|
||||
* String.prototype.replace() function.
|
||||
*
|
||||
* Where the input expression is a [String] or [Number] (to be treated as a string),
|
||||
* the `pattern` is a [String] or [RegExp],
|
||||
* the 'replacement' is a [String] or [Function].
|
||||
*
|
||||
* --Note--: The 'pattern' parameter will be converted to a RegExp instance. Make sure to escape the
|
||||
* string properly if you are matching for regular expression special characters like parenthesis,
|
||||
* brackets etc.
|
||||
*
|
||||
* @deprecated The current pipe has limited functionality. The pipe api is not meant to be able
|
||||
* express complex yet generic value transformations. We recommend that these transformations happen
|
||||
* in the component logic instead.
|
||||
*/
|
||||
|
||||
@Pipe({name: 'replace'})
|
||||
export class ReplacePipe implements PipeTransform {
|
||||
transform(value: any, pattern: string|RegExp, replacement: Function|string): any {
|
||||
if (isBlank(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (!this._supportedInput(value)) {
|
||||
throw new InvalidPipeArgumentException(ReplacePipe, value);
|
||||
}
|
||||
|
||||
var input = value.toString();
|
||||
|
||||
if (!this._supportedPattern(pattern)) {
|
||||
throw new InvalidPipeArgumentException(ReplacePipe, pattern);
|
||||
}
|
||||
if (!this._supportedReplacement(replacement)) {
|
||||
throw new InvalidPipeArgumentException(ReplacePipe, replacement);
|
||||
}
|
||||
|
||||
if (isFunction(replacement)) {
|
||||
const rgxPattern = isString(pattern) ? new RegExp(pattern, 'g') : pattern;
|
||||
|
||||
return StringWrapper.replaceAllMapped(
|
||||
input, rgxPattern, <(m: string[]) => string>replacement);
|
||||
}
|
||||
|
||||
if (pattern instanceof RegExp) {
|
||||
// use the replaceAll variant
|
||||
return StringWrapper.replaceAll(input, pattern, <string>replacement);
|
||||
}
|
||||
|
||||
return StringWrapper.replace(input, <string>pattern, <string>replacement);
|
||||
}
|
||||
|
||||
private _supportedInput(input: any): boolean { return isString(input) || isNumber(input); }
|
||||
|
||||
private _supportedPattern(pattern: any): boolean {
|
||||
return isString(pattern) || pattern instanceof RegExp;
|
||||
}
|
||||
|
||||
private _supportedReplacement(replacement: any): boolean {
|
||||
return isString(replacement) || isFunction(replacement);
|
||||
}
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {ReplacePipe} from '@angular/common';
|
||||
import {afterEach, beforeEach, ddescribe, describe, expect, iit, inject, it, xit} from '@angular/core/testing/testing_internal';
|
||||
|
||||
export function main() {
|
||||
describe('ReplacePipe', () => {
|
||||
var someNumber: number;
|
||||
var str: string;
|
||||
var pipe: ReplacePipe;
|
||||
|
||||
beforeEach(() => {
|
||||
someNumber = 42;
|
||||
str = 'Douglas Adams';
|
||||
pipe = new ReplacePipe();
|
||||
});
|
||||
|
||||
describe('transform', () => {
|
||||
|
||||
it('should not support input other than strings and numbers', () => {
|
||||
expect(() => pipe.transform({}, 'Douglas', 'Hugh')).toThrow();
|
||||
expect(() => pipe.transform([1, 2, 3], 'Douglas', 'Hugh')).toThrow();
|
||||
});
|
||||
|
||||
it('should not support patterns other than strings and regular expressions', () => {
|
||||
expect(() => pipe.transform(str, <any>{}, 'Hugh')).toThrow();
|
||||
expect(() => pipe.transform(str, <any>null, 'Hugh')).toThrow();
|
||||
expect(() => pipe.transform(str, <any>123, 'Hugh')).toThrow();
|
||||
});
|
||||
|
||||
it('should not support replacements other than strings and functions', () => {
|
||||
expect(() => pipe.transform(str, 'Douglas', <any>{})).toThrow();
|
||||
expect(() => pipe.transform(str, 'Douglas', <any>null)).toThrow();
|
||||
expect(() => pipe.transform(str, 'Douglas', <any>123)).toThrow();
|
||||
});
|
||||
|
||||
it('should return a new string with the pattern replaced', () => {
|
||||
var result1 = pipe.transform(str, 'Douglas', 'Hugh');
|
||||
|
||||
var result2 = pipe.transform(str, /a/g, '_');
|
||||
|
||||
var result3 = pipe.transform(str, /a/gi, '_');
|
||||
|
||||
var f = ((x: any) => { return 'Adams!'; });
|
||||
|
||||
var result4 = pipe.transform(str, 'Adams', f);
|
||||
|
||||
var result5 = pipe.transform(someNumber, '2', '4');
|
||||
|
||||
expect(result1).toEqual('Hugh Adams');
|
||||
expect(result2).toEqual('Dougl_s Ad_ms');
|
||||
expect(result3).toEqual('Dougl_s _d_ms');
|
||||
expect(result4).toEqual('Douglas Adams!');
|
||||
expect(result5).toEqual('44');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
}
|
|
@ -12,7 +12,7 @@ export declare class AsyncPipe implements OnDestroy {
|
|||
export declare const COMMON_DIRECTIVES: any[];
|
||||
|
||||
/** @experimental */
|
||||
export declare const COMMON_PIPES: (typeof AsyncPipe | typeof SlicePipe | typeof ReplacePipe | typeof I18nPluralPipe | typeof I18nSelectPipe)[];
|
||||
export declare const COMMON_PIPES: (typeof AsyncPipe | typeof SlicePipe | typeof I18nPluralPipe | typeof I18nSelectPipe)[];
|
||||
|
||||
/** @experimental */
|
||||
export declare class CommonModule {
|
||||
|
@ -232,11 +232,6 @@ export declare enum Plural {
|
|||
Other = 5,
|
||||
}
|
||||
|
||||
/** @deprecated */
|
||||
export declare class ReplacePipe implements PipeTransform {
|
||||
transform(value: any, pattern: string | RegExp, replacement: Function | string): any;
|
||||
}
|
||||
|
||||
/** @stable */
|
||||
export declare class SlicePipe implements PipeTransform {
|
||||
transform(value: any, start: number, end?: number): any;
|
||||
|
|
Loading…
Reference in New Issue