chore: move core modules into core directory
BREAKING CHANGE: This change moves the http module into angular2/, so its import path is now angular2/http instead of http/http. Many other modules have also been moved around inside of angular2, but the public API paths have not changed as of this commit.
This commit is contained in:
130
modules/angular2/src/core/pipes/async_pipe.ts
Normal file
130
modules/angular2/src/core/pipes/async_pipe.ts
Normal file
@ -0,0 +1,130 @@
|
||||
import {isBlank, isPresent, isPromise, CONST, BaseException} from 'angular2/src/facade/lang';
|
||||
import {Observable, Promise, ObservableWrapper} from 'angular2/src/facade/async';
|
||||
import {Injectable} from 'angular2/di';
|
||||
|
||||
import {PipeTransform, PipeOnDestroy, WrappedValue} from 'angular2/change_detection';
|
||||
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
|
||||
import {ChangeDetectorRef} from 'angular2/change_detection';
|
||||
|
||||
import {Pipe} from '../core/metadata';
|
||||
|
||||
|
||||
class ObservableStrategy {
|
||||
createSubscription(async: any, updateLatestValue: any): any {
|
||||
return ObservableWrapper.subscribe(async, updateLatestValue, e => { throw e; });
|
||||
}
|
||||
|
||||
dispose(subscription: any): void { ObservableWrapper.dispose(subscription); }
|
||||
|
||||
onDestroy(subscription: any): void { ObservableWrapper.dispose(subscription); }
|
||||
}
|
||||
|
||||
class PromiseStrategy {
|
||||
createSubscription(async: any, updateLatestValue: any): any {
|
||||
return async.then(updateLatestValue);
|
||||
}
|
||||
|
||||
dispose(subscription: any): void {}
|
||||
|
||||
onDestroy(subscription: any): void {}
|
||||
}
|
||||
|
||||
var _promiseStrategy = new PromiseStrategy();
|
||||
var _observableStrategy = new ObservableStrategy();
|
||||
|
||||
|
||||
/**
|
||||
* Implements async bindings to Observable and Promise.
|
||||
*
|
||||
* # Example
|
||||
*
|
||||
* In this example we bind the description observable to the DOM. The async pipe will convert an
|
||||
*observable to the
|
||||
* latest value it emitted. It will also request a change detection check when a new value is
|
||||
*emitted.
|
||||
*
|
||||
* ```
|
||||
* @Component({
|
||||
* selector: "task-cmp",
|
||||
* changeDetection: ON_PUSH
|
||||
* })
|
||||
* @View({
|
||||
* template: "Task Description {{ description | async }}"
|
||||
* })
|
||||
* class Task {
|
||||
* description:Observable<string>;
|
||||
* }
|
||||
*
|
||||
* ```
|
||||
*/
|
||||
@Pipe({name: 'async'})
|
||||
@Injectable()
|
||||
export class AsyncPipe implements PipeTransform, PipeOnDestroy {
|
||||
_latestValue: Object = null;
|
||||
_latestReturnedValue: Object = null;
|
||||
|
||||
_subscription: Object = null;
|
||||
_obj: Observable | Promise<any> = null;
|
||||
private _strategy: any = null;
|
||||
|
||||
constructor(public _ref: ChangeDetectorRef) {}
|
||||
|
||||
onDestroy(): void {
|
||||
if (isPresent(this._subscription)) {
|
||||
this._dispose();
|
||||
}
|
||||
}
|
||||
|
||||
transform(obj: Observable | Promise<any>, args?: any[]): any {
|
||||
if (isBlank(this._obj)) {
|
||||
if (isPresent(obj)) {
|
||||
this._subscribe(obj);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
if (obj !== this._obj) {
|
||||
this._dispose();
|
||||
return this.transform(obj);
|
||||
}
|
||||
|
||||
if (this._latestValue === this._latestReturnedValue) {
|
||||
return this._latestReturnedValue;
|
||||
} else {
|
||||
this._latestReturnedValue = this._latestValue;
|
||||
return WrappedValue.wrap(this._latestValue);
|
||||
}
|
||||
}
|
||||
|
||||
_subscribe(obj: Observable | Promise<any>): void {
|
||||
this._obj = obj;
|
||||
this._strategy = this._selectStrategy(obj);
|
||||
this._subscription =
|
||||
this._strategy.createSubscription(obj, value => this._updateLatestValue(obj, value));
|
||||
}
|
||||
|
||||
_selectStrategy(obj: Observable | Promise<any>): any {
|
||||
if (isPromise(obj)) {
|
||||
return _promiseStrategy;
|
||||
} else if (ObservableWrapper.isObservable(obj)) {
|
||||
return _observableStrategy;
|
||||
} else {
|
||||
throw new InvalidPipeArgumentException(AsyncPipe, obj);
|
||||
}
|
||||
}
|
||||
|
||||
_dispose(): void {
|
||||
this._strategy.dispose(this._subscription);
|
||||
this._latestValue = null;
|
||||
this._latestReturnedValue = null;
|
||||
this._subscription = null;
|
||||
this._obj = null;
|
||||
}
|
||||
|
||||
_updateLatestValue(async: any, value: Object) {
|
||||
if (async === this._obj) {
|
||||
this._latestValue = value;
|
||||
this._ref.requestCheck();
|
||||
}
|
||||
}
|
||||
}
|
114
modules/angular2/src/core/pipes/date_pipe.ts
Normal file
114
modules/angular2/src/core/pipes/date_pipe.ts
Normal file
@ -0,0 +1,114 @@
|
||||
import {
|
||||
isDate,
|
||||
isNumber,
|
||||
isPresent,
|
||||
Date,
|
||||
DateWrapper,
|
||||
CONST,
|
||||
isBlank,
|
||||
FunctionWrapper
|
||||
} from 'angular2/src/facade/lang';
|
||||
import {DateFormatter} from 'angular2/src/facade/intl';
|
||||
import {Injectable} from 'angular2/di';
|
||||
import {StringMapWrapper, ListWrapper} from 'angular2/src/facade/collection';
|
||||
|
||||
import {PipeTransform, WrappedValue} from 'angular2/change_detection';
|
||||
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
|
||||
|
||||
import {Pipe} from '../core/metadata';
|
||||
|
||||
// TODO: move to a global configable location along with other i18n components.
|
||||
var defaultLocale: string = 'en-US';
|
||||
|
||||
/**
|
||||
* WARNING: this pipe uses the Internationalization API.
|
||||
* Therefore it is only reliable in Chrome and Opera browsers.
|
||||
*
|
||||
* Formats a date value to a string based on the requested format.
|
||||
*
|
||||
* # Usage
|
||||
*
|
||||
* expression | date[:format]
|
||||
*
|
||||
* where `expression` is a date object or a number (milliseconds since UTC epoch) and
|
||||
* `format` indicates which date/time components to include:
|
||||
*
|
||||
* | Component | Symbol | Short Form | Long Form | Numeric | 2-digit |
|
||||
* |-----------|:------:|--------------|-------------------|-----------|-----------|
|
||||
* | era | G | G (AD) | GGGG (Anno Domini)| - | - |
|
||||
* | year | y | - | - | y (2015) | yy (15) |
|
||||
* | month | M | MMM (Sep) | MMMM (September) | M (9) | MM (09) |
|
||||
* | day | d | - | - | d (3) | dd (03) |
|
||||
* | weekday | E | EEE (Sun) | EEEE (Sunday) | - | - |
|
||||
* | hour | j | - | - | j (13) | jj (13) |
|
||||
* | hour12 | h | - | - | h (1 PM) | hh (01 PM)|
|
||||
* | hour24 | H | - | - | H (13) | HH (13) |
|
||||
* | minute | m | - | - | m (5) | mm (05) |
|
||||
* | second | s | - | - | s (9) | ss (09) |
|
||||
* | timezone | z | - | z (Pacific Standard Time)| - | - |
|
||||
* | timezone | Z | Z (GMT-8:00) | - | - | - |
|
||||
*
|
||||
* In javascript, only the components specified will be respected (not the ordering,
|
||||
* punctuations, ...) and details of the the formatting will be dependent on the locale.
|
||||
* On the other hand in Dart version, you can also include quoted text as well as some extra
|
||||
* date/time components such as quarter. For more information see:
|
||||
* https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/intl/intl.DateFormat.
|
||||
*
|
||||
* `format` can also be one of the following predefined formats:
|
||||
*
|
||||
* - `'medium'`: equivalent to `'yMMMdjms'` (e.g. Sep 3, 2010, 12:05:08 PM for en-US)
|
||||
* - `'short'`: equivalent to `'yMdjm'` (e.g. 9/3/2010, 12:05 PM for en-US)
|
||||
* - `'fullDate'`: equivalent to `'yMMMMEEEEd'` (e.g. Friday, September 3, 2010 for en-US)
|
||||
* - `'longDate'`: equivalent to `'yMMMMd'` (e.g. September 3, 2010)
|
||||
* - `'mediumDate'`: equivalent to `'yMMMd'` (e.g. Sep 3, 2010 for en-US)
|
||||
* - `'shortDate'`: equivalent to `'yMd'` (e.g. 9/3/2010 for en-US)
|
||||
* - `'mediumTime'`: equivalent to `'jms'` (e.g. 12:05:08 PM for en-US)
|
||||
* - `'shortTime'`: equivalent to `'jm'` (e.g. 12:05 PM for en-US)
|
||||
*
|
||||
* Timezone of the formatted text will be the local system timezone of the end-users machine.
|
||||
*
|
||||
* # Examples
|
||||
*
|
||||
* Assuming `dateObj` is (year: 2015, month: 6, day: 15, hour: 21, minute: 43, second: 11)
|
||||
* in the _local_ time and locale is 'en-US':
|
||||
*
|
||||
* {{ dateObj | date }} // output is 'Jun 15, 2015'
|
||||
* {{ dateObj | date:'medium' }} // output is 'Jun 15, 2015, 9:43:11 PM'
|
||||
* {{ dateObj | date:'shortTime' }} // output is '9:43 PM'
|
||||
* {{ dateObj | date:'mmss' }} // output is '43:11'
|
||||
*/
|
||||
@CONST()
|
||||
@Pipe({name: 'date'})
|
||||
@Injectable()
|
||||
export class DatePipe implements PipeTransform {
|
||||
static _ALIASES = {
|
||||
'medium': 'yMMMdjms',
|
||||
'short': 'yMdjm',
|
||||
'fullDate': 'yMMMMEEEEd',
|
||||
'longDate': 'yMMMMd',
|
||||
'mediumDate': 'yMMMd',
|
||||
'shortDate': 'yMd',
|
||||
'mediumTime': 'jms',
|
||||
'shortTime': 'jm'
|
||||
};
|
||||
|
||||
|
||||
transform(value: any, args: List<any>): string {
|
||||
if (isBlank(value)) return null;
|
||||
|
||||
if (!this.supports(value)) {
|
||||
throw new InvalidPipeArgumentException(DatePipe, value);
|
||||
}
|
||||
|
||||
var pattern: string = isPresent(args) && args.length > 0 ? args[0] : 'mediumDate';
|
||||
if (isNumber(value)) {
|
||||
value = DateWrapper.fromMillis(value);
|
||||
}
|
||||
if (StringMapWrapper.contains(DatePipe._ALIASES, pattern)) {
|
||||
pattern = <string>StringMapWrapper.get(DatePipe._ALIASES, pattern);
|
||||
}
|
||||
return DateFormatter.format(value, defaultLocale, pattern);
|
||||
}
|
||||
|
||||
private supports(obj: any): boolean { return isDate(obj) || isNumber(obj); }
|
||||
}
|
27
modules/angular2/src/core/pipes/default_pipes.ts
Normal file
27
modules/angular2/src/core/pipes/default_pipes.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import {AsyncPipe} from './async_pipe';
|
||||
import {UpperCasePipe} from './uppercase_pipe';
|
||||
import {LowerCasePipe} from './lowercase_pipe';
|
||||
import {JsonPipe} from './json_pipe';
|
||||
import {LimitToPipe} from './limit_to_pipe';
|
||||
import {DatePipe} from './date_pipe';
|
||||
import {DecimalPipe, PercentPipe, CurrencyPipe} from './number_pipe';
|
||||
|
||||
import {CONST_EXPR} from 'angular2/src/facade/lang';
|
||||
import {Binding, OpaqueToken} from 'angular2/di';
|
||||
|
||||
const DEFAULT_PIPES_LIST = CONST_EXPR([
|
||||
AsyncPipe,
|
||||
UpperCasePipe,
|
||||
LowerCasePipe,
|
||||
JsonPipe,
|
||||
LimitToPipe,
|
||||
DecimalPipe,
|
||||
PercentPipe,
|
||||
CurrencyPipe,
|
||||
DatePipe
|
||||
]);
|
||||
|
||||
export const DEFAULT_PIPES_TOKEN = CONST_EXPR(new OpaqueToken("Default Pipes"));
|
||||
|
||||
export const DEFAULT_PIPES =
|
||||
CONST_EXPR(new Binding(DEFAULT_PIPES_TOKEN, {toValue: DEFAULT_PIPES_LIST}));
|
@ -0,0 +1,7 @@
|
||||
import {ABSTRACT, BaseException, CONST, Type} from 'angular2/src/facade/lang';
|
||||
|
||||
export class InvalidPipeArgumentException extends BaseException {
|
||||
constructor(type: Type, value: Object) {
|
||||
super(`Invalid argument '${value}' for pipe '${type}'`);
|
||||
}
|
||||
}
|
36
modules/angular2/src/core/pipes/json_pipe.ts
Normal file
36
modules/angular2/src/core/pipes/json_pipe.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import {isBlank, isPresent, Json, CONST} from 'angular2/src/facade/lang';
|
||||
import {Injectable} from 'angular2/di';
|
||||
|
||||
import {PipeTransform, WrappedValue} from 'angular2/change_detection';
|
||||
|
||||
import {Pipe} from '../core/metadata';
|
||||
|
||||
/**
|
||||
* Implements json transforms to any object.
|
||||
*
|
||||
* # Example
|
||||
*
|
||||
* In this example we transform the user object to json.
|
||||
*
|
||||
* ```
|
||||
* @Component({
|
||||
* selector: "user-cmp"
|
||||
* })
|
||||
* @View({
|
||||
* template: "User: {{ user | json }}"
|
||||
* })
|
||||
* class Username {
|
||||
* user:Object
|
||||
* constructor() {
|
||||
* this.user = { name: "PatrickJS" };
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* ```
|
||||
*/
|
||||
@CONST()
|
||||
@Pipe({name: 'json'})
|
||||
@Injectable()
|
||||
export class JsonPipe implements PipeTransform {
|
||||
transform(value: any, args: List<any> = null): string { return Json.stringify(value); }
|
||||
}
|
81
modules/angular2/src/core/pipes/limit_to_pipe.ts
Normal file
81
modules/angular2/src/core/pipes/limit_to_pipe.ts
Normal file
@ -0,0 +1,81 @@
|
||||
import {
|
||||
isBlank,
|
||||
isString,
|
||||
isArray,
|
||||
StringWrapper,
|
||||
BaseException,
|
||||
CONST
|
||||
} from 'angular2/src/facade/lang';
|
||||
import {ListWrapper} from 'angular2/src/facade/collection';
|
||||
import {Math} from 'angular2/src/facade/math';
|
||||
import {Injectable} from 'angular2/di';
|
||||
|
||||
import {PipeTransform, WrappedValue} from 'angular2/change_detection';
|
||||
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
|
||||
|
||||
import {Pipe} from '../core/metadata';
|
||||
|
||||
/**
|
||||
* Creates a new List or String containing only a prefix/suffix of the
|
||||
* elements.
|
||||
*
|
||||
* The number of elements to return is specified by the `limitTo` parameter.
|
||||
*
|
||||
* # Usage
|
||||
*
|
||||
* expression | limitTo:number
|
||||
*
|
||||
* Where the input expression is a [List] or [String], and `limitTo` is:
|
||||
*
|
||||
* - **a positive integer**: return _number_ items from the beginning of the list or string
|
||||
* expression.
|
||||
* - **a negative integer**: return _number_ items from the end of the list or string expression.
|
||||
* - **`|limitTo|` greater than the size of the expression**: return the entire expression.
|
||||
*
|
||||
* When operating on a [List], the returned list is always a copy even when all
|
||||
* the elements are being returned.
|
||||
*
|
||||
* # Examples
|
||||
*
|
||||
* ## List Example
|
||||
*
|
||||
* Assuming `var collection = ['a', 'b', 'c']`, this `ng-for` directive:
|
||||
*
|
||||
* <li *ng-for="var i in collection | limitTo:2">{{i}}</li>
|
||||
*
|
||||
* produces the following:
|
||||
*
|
||||
* <li>a</li>
|
||||
* <li>b</li>
|
||||
*
|
||||
* ## String Examples
|
||||
*
|
||||
* {{ 'abcdefghij' | limitTo: 4 }} // output is 'abcd'
|
||||
* {{ 'abcdefghij' | limitTo: -4 }} // output is 'ghij'
|
||||
* {{ 'abcdefghij' | limitTo: -100 }} // output is 'abcdefghij'
|
||||
*/
|
||||
@Pipe({name: 'limitTo'})
|
||||
@Injectable()
|
||||
export class LimitToPipe implements PipeTransform {
|
||||
supports(obj: any): boolean { return isString(obj) || isArray(obj); }
|
||||
|
||||
transform(value: any, args: List<any> = null): any {
|
||||
if (isBlank(args) || args.length == 0) {
|
||||
throw new BaseException('limitTo pipe requires one argument');
|
||||
}
|
||||
if (!this.supports(value)) {
|
||||
throw new InvalidPipeArgumentException(LimitToPipe, value);
|
||||
}
|
||||
if (isBlank(value)) return value;
|
||||
var limit: number = args[0];
|
||||
var left = 0, right = Math.min(limit, value.length);
|
||||
if (limit < 0) {
|
||||
left = Math.max(0, value.length + limit);
|
||||
right = value.length;
|
||||
}
|
||||
if (isString(value)) {
|
||||
return StringWrapper.substring(value, left, right);
|
||||
}
|
||||
return ListWrapper.slice(value, left, right);
|
||||
}
|
||||
}
|
41
modules/angular2/src/core/pipes/lowercase_pipe.ts
Normal file
41
modules/angular2/src/core/pipes/lowercase_pipe.ts
Normal file
@ -0,0 +1,41 @@
|
||||
import {isString, StringWrapper, CONST, isBlank} from 'angular2/src/facade/lang';
|
||||
import {Injectable} from 'angular2/di';
|
||||
|
||||
import {PipeTransform, WrappedValue} from 'angular2/change_detection';
|
||||
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
|
||||
|
||||
|
||||
import {Pipe} from '../core/metadata';
|
||||
|
||||
/**
|
||||
* Implements lowercase transforms to text.
|
||||
*
|
||||
* # Example
|
||||
*
|
||||
* In this example we transform the user text lowercase.
|
||||
*
|
||||
* ```
|
||||
* @Component({
|
||||
* selector: "username-cmp"
|
||||
* })
|
||||
* @View({
|
||||
* template: "Username: {{ user | lowercase }}"
|
||||
* })
|
||||
* class Username {
|
||||
* user:string;
|
||||
* }
|
||||
*
|
||||
* ```
|
||||
*/
|
||||
@CONST()
|
||||
@Pipe({name: 'lowercase'})
|
||||
@Injectable()
|
||||
export class LowerCasePipe implements PipeTransform {
|
||||
transform(value: string, args: List<any> = null): string {
|
||||
if (isBlank(value)) return value;
|
||||
if (!isString(value)) {
|
||||
throw new InvalidPipeArgumentException(LowerCasePipe, value);
|
||||
}
|
||||
return StringWrapper.toLowerCase(value);
|
||||
}
|
||||
}
|
146
modules/angular2/src/core/pipes/number_pipe.ts
Normal file
146
modules/angular2/src/core/pipes/number_pipe.ts
Normal file
@ -0,0 +1,146 @@
|
||||
import {
|
||||
isNumber,
|
||||
isPresent,
|
||||
isBlank,
|
||||
StringWrapper,
|
||||
NumberWrapper,
|
||||
RegExpWrapper,
|
||||
BaseException,
|
||||
CONST,
|
||||
FunctionWrapper
|
||||
} from 'angular2/src/facade/lang';
|
||||
import {NumberFormatter, NumberFormatStyle} from 'angular2/src/facade/intl';
|
||||
import {Injectable} from 'angular2/di';
|
||||
import {ListWrapper} from 'angular2/src/facade/collection';
|
||||
|
||||
import {PipeTransform, WrappedValue} from 'angular2/change_detection';
|
||||
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
|
||||
|
||||
import {Pipe} from '../core/metadata';
|
||||
|
||||
var defaultLocale: string = 'en-US';
|
||||
var _re = RegExpWrapper.create('^(\\d+)?\\.((\\d+)(\\-(\\d+))?)?$');
|
||||
|
||||
@CONST()
|
||||
@Injectable()
|
||||
export class NumberPipe {
|
||||
static _format(value: number, style: NumberFormatStyle, digits: string, currency: string = null,
|
||||
currencyAsSymbol: boolean = false): string {
|
||||
if (isBlank(value)) return null;
|
||||
if (!isNumber(value)) {
|
||||
throw new InvalidPipeArgumentException(NumberPipe, value);
|
||||
}
|
||||
var minInt = 1, minFraction = 0, maxFraction = 3;
|
||||
if (isPresent(digits)) {
|
||||
var parts = RegExpWrapper.firstMatch(_re, digits);
|
||||
if (isBlank(parts)) {
|
||||
throw new BaseException(`${digits} is not a valid digit info for number pipes`);
|
||||
}
|
||||
if (isPresent(parts[1])) { // min integer digits
|
||||
minInt = NumberWrapper.parseIntAutoRadix(parts[1]);
|
||||
}
|
||||
if (isPresent(parts[3])) { // min fraction digits
|
||||
minFraction = NumberWrapper.parseIntAutoRadix(parts[3]);
|
||||
}
|
||||
if (isPresent(parts[5])) { // max fraction digits
|
||||
maxFraction = NumberWrapper.parseIntAutoRadix(parts[5]);
|
||||
}
|
||||
}
|
||||
return NumberFormatter.format(value, defaultLocale, style, {
|
||||
minimumIntegerDigits: minInt,
|
||||
minimumFractionDigits: minFraction,
|
||||
maximumFractionDigits: maxFraction,
|
||||
currency: currency,
|
||||
currencyAsSymbol: currencyAsSymbol
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* WARNING: this pipe uses the Internationalization API.
|
||||
* Therefore it is only reliable in Chrome and Opera browsers.
|
||||
*
|
||||
* Formats a number as local text. i.e. group sizing and seperator and other locale-specific
|
||||
* configurations are based on the active locale.
|
||||
*
|
||||
* # Usage
|
||||
*
|
||||
* expression | number[:digitInfo]
|
||||
*
|
||||
* where `expression` is a number and `digitInfo` has the following format:
|
||||
*
|
||||
* {minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}
|
||||
*
|
||||
* - minIntegerDigits is the minimum number of integer digits to use. Defaults to 1.
|
||||
* - minFractionDigits is the minimum number of digits after fraction. Defaults to 0.
|
||||
* - maxFractionDigits is the maximum number of digits after fraction. Defaults to 3.
|
||||
*
|
||||
* For more information on the acceptable range for each of these numbers and other
|
||||
* details see your native internationalization library.
|
||||
*
|
||||
* # Examples
|
||||
*
|
||||
* {{ 123 | number }} // output is 123
|
||||
* {{ 123.1 | number: '.2-3' }} // output is 123.10
|
||||
* {{ 1 | number: '2.2' }} // output is 01.00
|
||||
*/
|
||||
@CONST()
|
||||
@Pipe({name: 'number'})
|
||||
@Injectable()
|
||||
export class DecimalPipe extends NumberPipe implements PipeTransform {
|
||||
transform(value: any, args: any[]): string {
|
||||
var digits: string = ListWrapper.first(args);
|
||||
return NumberPipe._format(value, NumberFormatStyle.DECIMAL, digits);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* WARNING: this pipe uses the Internationalization API.
|
||||
* Therefore it is only reliable in Chrome and Opera browsers.
|
||||
*
|
||||
* Formats a number as local percent.
|
||||
*
|
||||
* # Usage
|
||||
*
|
||||
* expression | percent[:digitInfo]
|
||||
*
|
||||
* For more information about `digitInfo` see {@link DecimalPipe}
|
||||
*/
|
||||
@CONST()
|
||||
@Pipe({name: 'percent'})
|
||||
@Injectable()
|
||||
export class PercentPipe extends NumberPipe implements PipeTransform {
|
||||
transform(value: any, args: any[]): string {
|
||||
var digits: string = ListWrapper.first(args);
|
||||
return NumberPipe._format(value, NumberFormatStyle.PERCENT, digits);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* WARNING: this pipe uses the Internationalization API.
|
||||
* Therefore it is only reliable in Chrome and Opera browsers.
|
||||
*
|
||||
* Formats a number as local currency.
|
||||
*
|
||||
* # Usage
|
||||
*
|
||||
* expression | currency[:currencyCode[:symbolDisplay[:digitInfo]]]
|
||||
*
|
||||
* where `currencyCode` is the ISO 4217 currency code, such as "USD" for the US dollar and
|
||||
* "EUR" for the euro. `symbolDisplay` is a boolean indicating whether to use the currency
|
||||
* symbol (e.g. $) or the currency code (e.g. USD) in the output. The default for this value
|
||||
* is `false`.
|
||||
* For more information about `digitInfo` see {@link DecimalPipe}
|
||||
*/
|
||||
@CONST()
|
||||
@Pipe({name: 'currency'})
|
||||
@Injectable()
|
||||
export class CurrencyPipe extends NumberPipe implements PipeTransform {
|
||||
transform(value: any, args: any[]): string {
|
||||
var currencyCode: string = isPresent(args) && args.length > 0 ? args[0] : 'USD';
|
||||
var symbolDisplay: boolean = isPresent(args) && args.length > 1 ? args[1] : false;
|
||||
var digits: string = isPresent(args) && args.length > 2 ? args[2] : null;
|
||||
return NumberPipe._format(value, NumberFormatStyle.CURRENCY, digits, currencyCode,
|
||||
symbolDisplay);
|
||||
}
|
||||
}
|
40
modules/angular2/src/core/pipes/uppercase_pipe.ts
Normal file
40
modules/angular2/src/core/pipes/uppercase_pipe.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import {isString, StringWrapper, CONST, isBlank} from 'angular2/src/facade/lang';
|
||||
import {Injectable} from 'angular2/di';
|
||||
|
||||
import {PipeTransform, WrappedValue} from 'angular2/change_detection';
|
||||
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
|
||||
|
||||
import {Pipe} from '../core/metadata';
|
||||
|
||||
/**
|
||||
* Implements uppercase transforms to text.
|
||||
*
|
||||
* # Example
|
||||
*
|
||||
* In this example we transform the user text uppercase.
|
||||
*
|
||||
* ```
|
||||
* @Component({
|
||||
* selector: "username-cmp"
|
||||
* })
|
||||
* @View({
|
||||
* template: "Username: {{ user | uppercase }}"
|
||||
* })
|
||||
* class Username {
|
||||
* user:string;
|
||||
* }
|
||||
*
|
||||
* ```
|
||||
*/
|
||||
@CONST()
|
||||
@Pipe({name: 'uppercase'})
|
||||
@Injectable()
|
||||
export class UpperCasePipe implements PipeTransform {
|
||||
transform(value: string, args: List<any> = null): string {
|
||||
if (isBlank(value)) return value;
|
||||
if (!isString(value)) {
|
||||
throw new InvalidPipeArgumentException(UpperCasePipe, value);
|
||||
}
|
||||
return StringWrapper.toUpperCase(value);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user