From 21c2982f944dce0d0c517a71f561baa781b3af16 Mon Sep 17 00:00:00 2001 From: "Ciuca, Alexandru" Date: Tue, 22 Sep 2015 14:21:29 +0300 Subject: [PATCH 1/2] angularjs - fix signatures for filter service --- angularjs/angular-tests.ts | 75 +++++++++++++++++++++--- angularjs/angular.d.ts | 116 +++++++++++++++++++++++++++++++++---- 2 files changed, 173 insertions(+), 18 deletions(-) diff --git a/angularjs/angular-tests.ts b/angularjs/angular-tests.ts index 0174a0d616..14e0dd7b4e 100644 --- a/angularjs/angular-tests.ts +++ b/angularjs/angular-tests.ts @@ -934,18 +934,77 @@ function NgModelControllerTyping() { }; } -function ngFilterTyping() { - var $filter: angular.IFilterService; - var items: string[]; +var $filter: angular.IFilterService; + +function testFilter() { - $filter("name")(items, "test"); - $filter("name")(items, {name: "test"}); - $filter("name")(items, (val, index, array) => { + var items: string[]; + $filter("filter")(items, "test"); + $filter("filter")(items, {name: "test"}); + $filter("filter")(items, (val, index, array) => { return array; }); - $filter("name")(items, (val, index, array) => { + $filter("filter")(items, (val, index, array) => { return array; }, (actual, expected) => { return actual == expected; }); -} \ No newline at end of file +} + +function testCurrency() { + $filter("currency")(126); + $filter("currency")(126, "$", 2); +} + +function testNumber() { + $filter("number")(167); + $filter("number")(167, 2); +} + +function testDate() { + $filter("date")(new Date()); + $filter("date")(new Date(), 'yyyyMMdd'); + $filter("date")(new Date(), 'yyyyMMdd', '+0430'); +} + +function testJson() { + var json: string = $filter("json")({test:true}, 2); +} + +function testLowercase() { + var lower: string = $filter("lowercase")('test'); +} + +function testUppercase() { + var lower: string = $filter("uppercase")('test'); +} + +function testLimitTo() { + var limitTo = $filter("limitTo"); + var filtered: number[] = $filter("limitTo")([1,2,3], 5); + filtered = $filter("limitTo")([1,2,3], 5, 2); + + var filteredString: string = $filter("limitTo")("124", 4); + filteredString = $filter("limitTo")(124, 4); +} + +function testOrderBy() { + var filtered: number[] = $filter("orderBy")([1,2,3], "test"); + filtered = $filter("orderBy")([1,2,3], "test", true); + filtered = $filter("orderBy")([1,2,3], ['prop1', 'prop2']); + filtered = $filter("orderBy")([1,2,3], (val: number) => 1); + var filtered2: string[] = $filter("orderBy")(["1","2","3"], (val: string) => 1); + filtered2 = $filter("orderBy")(["1","2","3"], [ + (val: string) => 1, + (val: string) => 2 + ]); +} + +interface MyCustomFilter { + (value: string): string; +} + +function testCustomFilter() { + var filterCustom = $filter('custom'); + var filtered: string = filterCustom("test"); +} diff --git a/angularjs/angular.d.ts b/angularjs/angular.d.ts index be0b0861a9..134facc4ad 100644 --- a/angularjs/angular.d.ts +++ b/angularjs/angular.d.ts @@ -776,31 +776,127 @@ declare module angular { * see https://docs.angularjs.org/api/ng/service/$filter */ interface IFilterService { + (name: 'filter'): IFilterFilter; + (name: 'currency'): IFilterCurrency; + (name: 'number'): IFilterNumber; + (name: 'date'): IFilterDate; + (name: 'json'): IFilterJson; + (name: 'lowercase'): IFilterLowercase; + (name: 'uppercase'): IFilterUppercase; + (name: 'limitTo'): IFilterLimitTo; + (name: 'orderBy'): IFilterOrderBy; /** * Usage: * $filter(name); * * @param name Name of the filter function to retrieve */ - (name: string): IFilterFunc; + (name: string): T; + } + + interface IFilterFilter { + (array: T[], expression: string | IFilterFilterPatternObject | IFilterFilterPredicateFunc, comparator?: IFilterFilterComparatorFunc|boolean): T[]; } - interface IFilterFunc { - (array: T[], expression: string | IFilterPatternObject | IFilterPredicateFunc, comparator?: IFilterComparatorFunc|boolean): T[]; + interface IFilterFilterPatternObject { + [name: string]: any; } - interface IFilterPatternObject { - [name: string]: string; - } - - interface IFilterPredicateFunc { + interface IFilterFilterPredicateFunc { (value: T, index: number, array: T[]): T[]; } - interface IFilterComparatorFunc { + interface IFilterFilterComparatorFunc { (actual: T, expected: T): boolean; } - + + interface IFilterCurrency { + /** + * Formats a number as a currency (ie $1,234.56). When no currency symbol is provided, default symbol for current locale is used. + * @param amount Input to filter. + * @param symbol Currency symbol or identifier to be displayed. + * @param fractionSize Number of decimal places to round the amount to, defaults to default max fraction size for current locale + * @return Formatted number + */ + (amount: number, symbol?: string, fractionSize?: number): string; + } + + interface IFilterNumber { + /** + * Formats a number as text. + * @param number Number to format. + * @param fractionSize Number of decimal places to round the number to. If this is not provided then the fraction size is computed from the current locale's number formatting pattern. In the case of the default locale, it will be 3. + * @return Number rounded to decimalPlaces and places a “,” after each third digit. + */ + (value: number|string, fractionSize?: number|string): string; + } + + interface IFilterDate { + /** + * Formats date to a string based on the requested format. + * + * @param date Date to format either as Date object, milliseconds (string or number) or various ISO 8601 datetime string formats (e.g. yyyy-MM-ddTHH:mm:ss.sssZ and its shorter versions like yyyy-MM-ddTHH:mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ). If no timezone is specified in the string input, the time is considered to be in the local timezone. + * @param format Formatting rules (see Description). If not specified, mediumDate is used. + * @param timezone Timezone to be used for formatting. It understands UTC/GMT and the continental US time zone abbreviations, but for general use, use a time zone offset, for example, '+0430' (4 hours, 30 minutes east of the Greenwich meridian) If not specified, the timezone of the browser will be used. + * @return Formatted string or the input if input is not recognized as date/millis. + */ + (date: Date | number | string, format?: string, timezone?: string): string; + } + + interface IFilterJson { + /** + * Allows you to convert a JavaScript object into JSON string. + * @param object Any JavaScript object (including arrays and primitive types) to filter. + * @param spacing The number of spaces to use per indentation, defaults to 2. + * @return JSON string. + */ + (object: any, spacing?: number): string; + } + + interface IFilterLowercase { + /** + * Converts string to lowercase. + */ + (value: string): string; + } + + interface IFilterUppercase { + /** + * Converts string to uppercase. + */ + (value: string): string; + } + + interface IFilterLimitTo { + /** + * Creates a new array containing only a specified number of elements. The elements are taken from either the beginning or the end of the source array, string or number, as specified by the value and sign (positive or negative) of limit. + * @param input Source array to be limited. + * @param limit The length of the returned array. If the limit number is positive, limit number of items from the beginning of the source array/string are copied. If the number is negative, limit number of items from the end of the source array are copied. The limit will be trimmed if it exceeds array.length. If limit is undefined, the input will be returned unchanged. + * @param begin Index at which to begin limitation. As a negative index, begin indicates an offset from the end of input. Defaults to 0. + * @return A new sub-array of length limit or less if input array had less than limit elements. + */ + (input: T[], limit: string|number, begin?: string|number): T[]; + /** + * Creates a new string containing only a specified number of elements. The elements are taken from either the beginning or the end of the source string or number, as specified by the value and sign (positive or negative) of limit. If a number is used as input, it is converted to a string. + * @param input Source string or number to be limited. + * @param limit The length of the returned string. If the limit number is positive, limit number of items from the beginning of the source string are copied. If the number is negative, limit number of items from the end of the source string are copied. The limit will be trimmed if it exceeds input.length. If limit is undefined, the input will be returned unchanged. + * @param begin Index at which to begin limitation. As a negative index, begin indicates an offset from the end of input. Defaults to 0. + * @return A new substring of length limit or less if input had less than limit elements. + */ + (input: string|number, limit: string|number, begin?: string|number): string; + } + + interface IFilterOrderBy { + /** + * Orders a specified array by the expression predicate. It is ordered alphabetically for strings and numerically for numbers. Note: if you notice numbers are not being sorted as expected, make sure they are actually being saved as numbers and not strings. + * @param array The array to sort. + * @param expression A predicate to be used by the comparator to determine the order of elements. + * @param reverse Reverse the order of the array. + * @return Reverse the order of the array. + */ + (array: T[], expression: string|string[]|((value: T) => any)|((value: T) => any)[], reverse?: boolean): T[]; + } + /** * $filterProvider - $filter - provider in module ng * From 221575e9823247b8c498210c986ab75b260ff78c Mon Sep 17 00:00:00 2001 From: Alexandru Ciuca Date: Mon, 28 Sep 2015 18:06:11 +0300 Subject: [PATCH 2/2] angularjs - Added some special filter use cases to tests --- angularjs/angular-tests.ts | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/angularjs/angular-tests.ts b/angularjs/angular-tests.ts index 14e0dd7b4e..88c348646a 100644 --- a/angularjs/angular-tests.ts +++ b/angularjs/angular-tests.ts @@ -387,31 +387,31 @@ module TestPromise { var tresult: TResult; var tresultPromise: ng.IPromise; - + var tother: TOther; var totherPromise: ng.IPromise; - + var promise: angular.IPromise; // promise.then result = >promise.then((result) => any); result = >promise.then((result) => any, (any) => any); result = >promise.then((result) => any, (any) => any, (any) => any); - + result = >promise.then((result) => result); result = >promise.then((result) => result, (any) => any); result = >promise.then((result) => result, (any) => any, (any) => any); result = >promise.then((result) => tresultPromise); result = >promise.then((result) => tresultPromise, (any) => any); result = >promise.then((result) => tresultPromise, (any) => any, (any) => any); - + result = >promise.then((result) => tother); result = >promise.then((result) => tother, (any) => any); result = >promise.then((result) => tother, (any) => any, (any) => any); result = >promise.then((result) => totherPromise); result = >promise.then((result) => totherPromise, (any) => any); result = >promise.then((result) => totherPromise, (any) => any, (any) => any); - + // promise.catch result = >promise.catch((err) => any); result = >promise.catch((err) => tresult); @@ -937,7 +937,7 @@ function NgModelControllerTyping() { var $filter: angular.IFilterService; function testFilter() { - + var items: string[]; $filter("filter")(items, "test"); $filter("filter")(items, {name: "test"}); @@ -983,7 +983,7 @@ function testLimitTo() { var limitTo = $filter("limitTo"); var filtered: number[] = $filter("limitTo")([1,2,3], 5); filtered = $filter("limitTo")([1,2,3], 5, 2); - + var filteredString: string = $filter("limitTo")("124", 4); filteredString = $filter("limitTo")(124, 4); } @@ -1000,10 +1000,22 @@ function testOrderBy() { ]); } +function testDynamicFilter() { + // Test with separate variables + var dateFilter = $filter("date"); + var myDate = new Date(); + dateFilter(myDate , "EEE, MMM d"); + + // Test with dynamic name + var filterName = 'date'; + var dynDateFilter = $filter(filterName); + dynDateFilter(new Date()); +} + interface MyCustomFilter { (value: string): string; } - + function testCustomFilter() { var filterCustom = $filter('custom'); var filtered: string = filterCustom("test");