Skip to content

Commit

Permalink
angularjs - fix signatures for filter service
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciuca, Alexandru committed Sep 22, 2015
1 parent d0a264d commit 21c2982
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 17 deletions.
75 changes: 67 additions & 8 deletions angularjs/angular-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
}

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<MyCustomFilter>('custom');
var filtered: string = filterCustom("test");
}
114 changes: 105 additions & 9 deletions angularjs/angular.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
<T>(name: string): T;
}

interface IFilterFunc {
<T>(array: T[], expression: string | IFilterPatternObject | IFilterPredicateFunc<T>, comparator?: IFilterComparatorFunc<T>|boolean): T[];
interface IFilterFilter {
<T>(array: T[], expression: string | IFilterFilterPatternObject | IFilterFilterPredicateFunc<T>, comparator?: IFilterFilterComparatorFunc<T>|boolean): T[];
}

interface IFilterPatternObject {
[name: string]: string;
interface IFilterFilterPatternObject {
[name: string]: any;
}

interface IFilterPredicateFunc<T> {
interface IFilterFilterPredicateFunc<T> {
(value: T, index: number, array: T[]): T[];
}

interface IFilterComparatorFunc<T> {
interface IFilterFilterComparatorFunc<T> {
(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.
*/
<T>(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.
*/
<T>(array: T[], expression: string|string[]|((value: T) => any)|((value: T) => any)[], reverse?: boolean): T[];
}

/**
* $filterProvider - $filter - provider in module ng
*
Expand Down

0 comments on commit 21c2982

Please sign in to comment.