fix(compiler): properly implement pure pipes and change pipe syntax
Pure pipes as well as arrays and maps are implemented via proxy functions. This is faster than the previous implementation and also generates less code. BREAKING CHANGE: - pipes now take a variable number of arguments, and not an array that contains all arguments.
This commit is contained in:
@ -208,14 +208,14 @@ export function main() {
|
||||
describe('null', () => {
|
||||
it('should return null when given null', () => {
|
||||
var pipe = new AsyncPipe(null);
|
||||
expect(pipe.transform(null, [])).toEqual(null);
|
||||
expect(pipe.transform(null)).toEqual(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('other types', () => {
|
||||
it('should throw when given an invalid object', () => {
|
||||
var pipe = new AsyncPipe(null);
|
||||
expect(() => pipe.transform(<any>"some bogus object", [])).toThrowError();
|
||||
expect(() => pipe.transform(<any>"some bogus object")).toThrowError();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -42,39 +42,39 @@ export function main() {
|
||||
if (browserDetection.supportsIntlApi) {
|
||||
describe("transform", () => {
|
||||
it('should format each component correctly', () => {
|
||||
expect(pipe.transform(date, ['y'])).toEqual('2015');
|
||||
expect(pipe.transform(date, ['yy'])).toEqual('15');
|
||||
expect(pipe.transform(date, ['M'])).toEqual('6');
|
||||
expect(pipe.transform(date, ['MM'])).toEqual('06');
|
||||
expect(pipe.transform(date, ['MMM'])).toEqual('Jun');
|
||||
expect(pipe.transform(date, ['MMMM'])).toEqual('June');
|
||||
expect(pipe.transform(date, ['d'])).toEqual('15');
|
||||
expect(pipe.transform(date, ['E'])).toEqual('Mon');
|
||||
expect(pipe.transform(date, ['EEEE'])).toEqual('Monday');
|
||||
expect(pipe.transform(date, ['H'])).toEqual('21');
|
||||
expect(pipe.transform(date, ['j'])).toEqual('9 PM');
|
||||
expect(pipe.transform(date, ['m'])).toEqual('43');
|
||||
expect(pipe.transform(date, ['s'])).toEqual('11');
|
||||
expect(pipe.transform(date, 'y')).toEqual('2015');
|
||||
expect(pipe.transform(date, 'yy')).toEqual('15');
|
||||
expect(pipe.transform(date, 'M')).toEqual('6');
|
||||
expect(pipe.transform(date, 'MM')).toEqual('06');
|
||||
expect(pipe.transform(date, 'MMM')).toEqual('Jun');
|
||||
expect(pipe.transform(date, 'MMMM')).toEqual('June');
|
||||
expect(pipe.transform(date, 'd')).toEqual('15');
|
||||
expect(pipe.transform(date, 'E')).toEqual('Mon');
|
||||
expect(pipe.transform(date, 'EEEE')).toEqual('Monday');
|
||||
expect(pipe.transform(date, 'H')).toEqual('21');
|
||||
expect(pipe.transform(date, 'j')).toEqual('9 PM');
|
||||
expect(pipe.transform(date, 'm')).toEqual('43');
|
||||
expect(pipe.transform(date, 's')).toEqual('11');
|
||||
});
|
||||
|
||||
it('should format common multi component patterns', () => {
|
||||
expect(pipe.transform(date, ['yMEd'])).toEqual('Mon, 6/15/2015');
|
||||
expect(pipe.transform(date, ['MEd'])).toEqual('Mon, 6/15');
|
||||
expect(pipe.transform(date, ['MMMd'])).toEqual('Jun 15');
|
||||
expect(pipe.transform(date, ['yMMMMEEEEd'])).toEqual('Monday, June 15, 2015');
|
||||
expect(pipe.transform(date, ['jms'])).toEqual('9:43:11 PM');
|
||||
expect(pipe.transform(date, ['ms'])).toEqual('43:11');
|
||||
expect(pipe.transform(date, 'yMEd')).toEqual('Mon, 6/15/2015');
|
||||
expect(pipe.transform(date, 'MEd')).toEqual('Mon, 6/15');
|
||||
expect(pipe.transform(date, 'MMMd')).toEqual('Jun 15');
|
||||
expect(pipe.transform(date, 'yMMMMEEEEd')).toEqual('Monday, June 15, 2015');
|
||||
expect(pipe.transform(date, 'jms')).toEqual('9:43:11 PM');
|
||||
expect(pipe.transform(date, 'ms')).toEqual('43:11');
|
||||
});
|
||||
|
||||
it('should format with pattern aliases', () => {
|
||||
expect(pipe.transform(date, ['medium'])).toEqual('Jun 15, 2015, 9:43:11 PM');
|
||||
expect(pipe.transform(date, ['short'])).toEqual('6/15/2015, 9:43 PM');
|
||||
expect(pipe.transform(date, ['fullDate'])).toEqual('Monday, June 15, 2015');
|
||||
expect(pipe.transform(date, ['longDate'])).toEqual('June 15, 2015');
|
||||
expect(pipe.transform(date, ['mediumDate'])).toEqual('Jun 15, 2015');
|
||||
expect(pipe.transform(date, ['shortDate'])).toEqual('6/15/2015');
|
||||
expect(pipe.transform(date, ['mediumTime'])).toEqual('9:43:11 PM');
|
||||
expect(pipe.transform(date, ['shortTime'])).toEqual('9:43 PM');
|
||||
expect(pipe.transform(date, 'medium')).toEqual('Jun 15, 2015, 9:43:11 PM');
|
||||
expect(pipe.transform(date, 'short')).toEqual('6/15/2015, 9:43 PM');
|
||||
expect(pipe.transform(date, 'fullDate')).toEqual('Monday, June 15, 2015');
|
||||
expect(pipe.transform(date, 'longDate')).toEqual('June 15, 2015');
|
||||
expect(pipe.transform(date, 'mediumDate')).toEqual('Jun 15, 2015');
|
||||
expect(pipe.transform(date, 'shortDate')).toEqual('6/15/2015');
|
||||
expect(pipe.transform(date, 'mediumTime')).toEqual('9:43:11 PM');
|
||||
expect(pipe.transform(date, 'shortTime')).toEqual('9:43 PM');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@ -26,33 +26,33 @@ export function main() {
|
||||
|
||||
describe("transform", () => {
|
||||
it("should return 0 text if value is 0", () => {
|
||||
var val = pipe.transform(0, [mapping]);
|
||||
var val = pipe.transform(0, mapping);
|
||||
expect(val).toEqual('No messages.');
|
||||
});
|
||||
|
||||
it("should return 1 text if value is 1", () => {
|
||||
var val = pipe.transform(1, [mapping]);
|
||||
var val = pipe.transform(1, mapping);
|
||||
expect(val).toEqual('One message.');
|
||||
});
|
||||
|
||||
it("should return other text if value is anything other than 0 or 1", () => {
|
||||
var val = pipe.transform(6, [mapping]);
|
||||
var val = pipe.transform(6, mapping);
|
||||
expect(val).toEqual('There are some messages.');
|
||||
});
|
||||
|
||||
it("should interpolate the value into the text where indicated", () => {
|
||||
var val = pipe.transform(6, [interpolatedMapping]);
|
||||
var val = pipe.transform(6, interpolatedMapping);
|
||||
expect(val).toEqual('There are 6 messages, that is 6.');
|
||||
});
|
||||
|
||||
it("should use 'other' if value is undefined", () => {
|
||||
var messageLength;
|
||||
var val = pipe.transform(messageLength, [interpolatedMapping]);
|
||||
var val = pipe.transform(messageLength, interpolatedMapping);
|
||||
expect(val).toEqual('There are messages, that is .');
|
||||
});
|
||||
|
||||
it("should not support bad arguments",
|
||||
() => { expect(() => pipe.transform(0, ['hey'])).toThrowError(); });
|
||||
() => { expect(() => pipe.transform(0, 'hey')).toThrowError(); });
|
||||
});
|
||||
|
||||
});
|
||||
|
@ -24,28 +24,28 @@ export function main() {
|
||||
|
||||
describe("transform", () => {
|
||||
it("should return male text if value is male", () => {
|
||||
var val = pipe.transform('male', [mapping]);
|
||||
var val = pipe.transform('male', mapping);
|
||||
expect(val).toEqual('Invite him.');
|
||||
});
|
||||
|
||||
it("should return female text if value is female", () => {
|
||||
var val = pipe.transform('female', [mapping]);
|
||||
var val = pipe.transform('female', mapping);
|
||||
expect(val).toEqual('Invite her.');
|
||||
});
|
||||
|
||||
it("should return other text if value is anything other than male or female", () => {
|
||||
var val = pipe.transform('Anything else', [mapping]);
|
||||
var val = pipe.transform('Anything else', mapping);
|
||||
expect(val).toEqual('Invite them.');
|
||||
});
|
||||
|
||||
it("should use 'other' if value is undefined", () => {
|
||||
var gender;
|
||||
var val = pipe.transform(gender, [mapping]);
|
||||
var val = pipe.transform(gender, mapping);
|
||||
expect(val).toEqual('Invite them.');
|
||||
});
|
||||
|
||||
it("should not support bad arguments",
|
||||
() => { expect(() => pipe.transform('male', ['hey'])).toThrowError(); });
|
||||
() => { expect(() => pipe.transform('male', 'hey')).toThrowError(); });
|
||||
});
|
||||
|
||||
});
|
||||
|
@ -24,17 +24,17 @@ export function main() {
|
||||
|
||||
describe("transform", () => {
|
||||
it('should return correct value for numbers', () => {
|
||||
expect(pipe.transform(12345, [])).toEqual('12,345');
|
||||
expect(pipe.transform(123, ['.2'])).toEqual('123.00');
|
||||
expect(pipe.transform(1, ['3.'])).toEqual('001');
|
||||
expect(pipe.transform(1.1, ['3.4-5'])).toEqual('001.1000');
|
||||
expect(pipe.transform(12345)).toEqual('12,345');
|
||||
expect(pipe.transform(123, '.2')).toEqual('123.00');
|
||||
expect(pipe.transform(1, '3.')).toEqual('001');
|
||||
expect(pipe.transform(1.1, '3.4-5')).toEqual('001.1000');
|
||||
|
||||
expect(pipe.transform(1.123456, ['3.4-5'])).toEqual('001.12346');
|
||||
expect(pipe.transform(1.1234, [])).toEqual('1.123');
|
||||
expect(pipe.transform(1.123456, '3.4-5')).toEqual('001.12346');
|
||||
expect(pipe.transform(1.1234)).toEqual('1.123');
|
||||
});
|
||||
|
||||
it("should not support other objects",
|
||||
() => { expect(() => pipe.transform(new Object(), [])).toThrowError(); });
|
||||
() => { expect(() => pipe.transform(new Object())).toThrowError(); });
|
||||
});
|
||||
});
|
||||
|
||||
@ -45,12 +45,12 @@ export function main() {
|
||||
|
||||
describe("transform", () => {
|
||||
it('should return correct value for numbers', () => {
|
||||
expect(pipe.transform(1.23, [])).toEqual('123%');
|
||||
expect(pipe.transform(1.2, ['.2'])).toEqual('120.00%');
|
||||
expect(pipe.transform(1.23)).toEqual('123%');
|
||||
expect(pipe.transform(1.2, '.2')).toEqual('120.00%');
|
||||
});
|
||||
|
||||
it("should not support other objects",
|
||||
() => { expect(() => pipe.transform(new Object(), [])).toThrowError(); });
|
||||
() => { expect(() => pipe.transform(new Object())).toThrowError(); });
|
||||
});
|
||||
});
|
||||
|
||||
@ -61,12 +61,12 @@ export function main() {
|
||||
|
||||
describe("transform", () => {
|
||||
it('should return correct value for numbers', () => {
|
||||
expect(pipe.transform(123, [])).toEqual('USD123');
|
||||
expect(pipe.transform(12, ['EUR', false, '.2'])).toEqual('EUR12.00');
|
||||
expect(pipe.transform(123)).toEqual('USD123');
|
||||
expect(pipe.transform(12, 'EUR', false, '.2')).toEqual('EUR12.00');
|
||||
});
|
||||
|
||||
it("should not support other objects",
|
||||
() => { expect(() => pipe.transform(new Object(), [])).toThrowError(); });
|
||||
() => { expect(() => pipe.transform(new Object())).toThrowError(); });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@ -31,34 +31,34 @@ export function main() {
|
||||
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();
|
||||
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, [{}, "Hugh"])).toThrow();
|
||||
expect(() => pipe.transform(str, [null, "Hugh"])).toThrow();
|
||||
expect(() => pipe.transform(str, [123, "Hugh"])).toThrow();
|
||||
expect(() => pipe.transform(str, {}, "Hugh")).toThrow();
|
||||
expect(() => pipe.transform(str, null, "Hugh")).toThrow();
|
||||
expect(() => pipe.transform(str, 123, "Hugh")).toThrow();
|
||||
});
|
||||
|
||||
it("should not support replacements other than strings and functions", () => {
|
||||
expect(() => pipe.transform(str, ["Douglas", {}])).toThrow();
|
||||
expect(() => pipe.transform(str, ["Douglas", null])).toThrow();
|
||||
expect(() => pipe.transform(str, ["Douglas", 123])).toThrow();
|
||||
expect(() => pipe.transform(str, "Douglas", {})).toThrow();
|
||||
expect(() => pipe.transform(str, "Douglas", null)).toThrow();
|
||||
expect(() => pipe.transform(str, "Douglas", 123)).toThrow();
|
||||
});
|
||||
|
||||
it("should return a new string with the pattern replaced", () => {
|
||||
var result1 = pipe.transform(str, ["Douglas", "Hugh"]);
|
||||
var result1 = pipe.transform(str, "Douglas", "Hugh");
|
||||
|
||||
var result2 = pipe.transform(str, [RegExpWrapper.create("a"), "_"]);
|
||||
var result2 = pipe.transform(str, RegExpWrapper.create("a"), "_");
|
||||
|
||||
var result3 = pipe.transform(str, [RegExpWrapper.create("a", "i"), "_"]);
|
||||
var result3 = pipe.transform(str, RegExpWrapper.create("a", "i"), "_");
|
||||
|
||||
var f = (x => { return "Adams!"; });
|
||||
|
||||
var result4 = pipe.transform(str, ["Adams", f]);
|
||||
var result4 = pipe.transform(str, "Adams", f);
|
||||
|
||||
var result5 = pipe.transform(someNumber, ["2", "4"]);
|
||||
var result5 = pipe.transform(someNumber, "2", "4");
|
||||
|
||||
expect(result1).toEqual("Hugh Adams");
|
||||
expect(result2).toEqual("Dougl_s Ad_ms");
|
||||
|
@ -42,47 +42,47 @@ export function main() {
|
||||
|
||||
it('should return all items after START index when START is positive and END is omitted',
|
||||
() => {
|
||||
expect(pipe.transform(list, [3])).toEqual([4, 5]);
|
||||
expect(pipe.transform(str, [3])).toEqual('wxyz');
|
||||
expect(pipe.transform(list, 3)).toEqual([4, 5]);
|
||||
expect(pipe.transform(str, 3)).toEqual('wxyz');
|
||||
});
|
||||
|
||||
it('should return last START items when START is negative and END is omitted', () => {
|
||||
expect(pipe.transform(list, [-3])).toEqual([3, 4, 5]);
|
||||
expect(pipe.transform(str, [-3])).toEqual('xyz');
|
||||
expect(pipe.transform(list, -3)).toEqual([3, 4, 5]);
|
||||
expect(pipe.transform(str, -3)).toEqual('xyz');
|
||||
});
|
||||
|
||||
it('should return all items between START and END index when START and END are positive',
|
||||
() => {
|
||||
expect(pipe.transform(list, [1, 3])).toEqual([2, 3]);
|
||||
expect(pipe.transform(str, [1, 3])).toEqual('uv');
|
||||
expect(pipe.transform(list, 1, 3)).toEqual([2, 3]);
|
||||
expect(pipe.transform(str, 1, 3)).toEqual('uv');
|
||||
});
|
||||
|
||||
it('should return all items between START and END from the end when START and END are negative',
|
||||
() => {
|
||||
expect(pipe.transform(list, [-4, -2])).toEqual([2, 3]);
|
||||
expect(pipe.transform(str, [-4, -2])).toEqual('wx');
|
||||
expect(pipe.transform(list, -4, -2)).toEqual([2, 3]);
|
||||
expect(pipe.transform(str, -4, -2)).toEqual('wx');
|
||||
});
|
||||
|
||||
it('should return an empty value if START is greater than END', () => {
|
||||
expect(pipe.transform(list, [4, 2])).toEqual([]);
|
||||
expect(pipe.transform(str, [4, 2])).toEqual('');
|
||||
expect(pipe.transform(list, 4, 2)).toEqual([]);
|
||||
expect(pipe.transform(str, 4, 2)).toEqual('');
|
||||
});
|
||||
|
||||
it('should return an empty value if START greater than input length', () => {
|
||||
expect(pipe.transform(list, [99])).toEqual([]);
|
||||
expect(pipe.transform(str, [99])).toEqual('');
|
||||
expect(pipe.transform(list, 99)).toEqual([]);
|
||||
expect(pipe.transform(str, 99)).toEqual('');
|
||||
});
|
||||
|
||||
// Makes Edge to disconnect when running the full unit test campaign
|
||||
// TODO: remove when issue is solved: https://github.com/angular/angular/issues/4756
|
||||
if (!browserDetection.isEdge) {
|
||||
it('should return entire input if START is negative and greater than input length', () => {
|
||||
expect(pipe.transform(list, [-99])).toEqual([1, 2, 3, 4, 5]);
|
||||
expect(pipe.transform(str, [-99])).toEqual('tuvwxyz');
|
||||
expect(pipe.transform(list, -99)).toEqual([1, 2, 3, 4, 5]);
|
||||
expect(pipe.transform(str, -99)).toEqual('tuvwxyz');
|
||||
});
|
||||
|
||||
it('should not modify the input list', () => {
|
||||
expect(pipe.transform(list, [2])).toEqual([3, 4, 5]);
|
||||
expect(pipe.transform(list, 2)).toEqual([3, 4, 5]);
|
||||
expect(list).toEqual([1, 2, 3, 4, 5]);
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user