-
Notifications
You must be signed in to change notification settings - Fork 25.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #1957
- Loading branch information
Showing
5 changed files
with
202 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import {isBlank, isPresent, CONST, Json} from 'angular2/src/facade/lang'; | ||
import {Pipe, PipeFactory} from './pipe'; | ||
|
||
// HACK: workaround for Traceur behavior. | ||
// It expects all transpiled modules to contain this marker. | ||
// TODO: remove this when we no longer use traceur | ||
export var __esModule = true; | ||
|
||
|
||
/** | ||
* 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" }; | ||
* } | ||
* } | ||
* | ||
* ``` | ||
* | ||
* @exportedAs angular2/pipes | ||
*/ | ||
export class JsonPipe extends Pipe { | ||
_latestRef: any; | ||
_latestValue: any; | ||
constructor() { | ||
super(); | ||
this._latestRef = null; | ||
this._latestValue = null; | ||
} | ||
|
||
onDestroy(): void { | ||
if (isPresent(this._latestValue)) { | ||
this._latestRef = null; | ||
this._latestValue = null; | ||
} | ||
} | ||
|
||
supports(obj): boolean { return true; } | ||
|
||
transform(value): any { | ||
if (value === this._latestRef) { | ||
return this._latestValue; | ||
} else { | ||
return this._prettyPrint(value); | ||
} | ||
} | ||
|
||
_prettyPrint(value) { | ||
this._latestRef = value; | ||
this._latestValue = Json.stringify(value); | ||
return this._latestValue; | ||
} | ||
} | ||
|
||
/** | ||
* Provides a factory for [JsonPipeFactory]. | ||
* | ||
* @exportedAs angular2/pipes | ||
*/ | ||
@CONST() | ||
export class JsonPipeFactory extends PipeFactory { | ||
constructor() { super(); } | ||
|
||
supports(obj): boolean { return true; } | ||
|
||
create(cdRef): Pipe { return new JsonPipe(); } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
modules/angular2/test/change_detection/pipes/json_pipe_spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach, | ||
AsyncTestCompleter, inject, proxy, SpyObject, IS_DARTIUM} from 'angular2/test_lib'; | ||
import {Json, RegExp, NumberWrapper, StringWrapper} from 'angular2/src/facade/lang'; | ||
|
||
import {JsonPipe} from 'angular2/src/change_detection/pipes/json_pipe'; | ||
|
||
export function main() { | ||
describe("JsonPipe", () => { | ||
var regNewLine = new RegExp('\n'); | ||
var canHasUndefined; // because Dart doesn't like undefined; | ||
var inceptionObj; | ||
var inceptionObjString; | ||
var catString; | ||
var pipe; | ||
|
||
function normalize(obj: string): string { | ||
return StringWrapper.replace(obj, regNewLine, ''); | ||
} | ||
|
||
beforeEach(() => { | ||
inceptionObj = { | ||
dream: { | ||
dream: { | ||
dream: 'Limbo' | ||
} | ||
} | ||
}; | ||
inceptionObjString = "{\n" + | ||
" \"dream\": {\n" + | ||
" \"dream\": {\n" + | ||
" \"dream\": \"Limbo\"\n" + | ||
" }\n" + | ||
" }\n" + | ||
"}"; | ||
|
||
|
||
catString = 'Inception Cat'; | ||
pipe = new JsonPipe(); | ||
}); | ||
|
||
describe("supports", () => { | ||
it("should support objects", () => { | ||
expect(pipe.supports(inceptionObj)).toBe(true); | ||
}); | ||
|
||
it("should support strings", () => { | ||
expect(pipe.supports(catString)).toBe(true); | ||
}); | ||
|
||
it("should support null", () => { | ||
expect(pipe.supports(null)).toBe(true); | ||
}); | ||
|
||
it("should support NaN", () => { | ||
expect(pipe.supports(NumberWrapper.NaN)).toBe(true); | ||
}); | ||
|
||
if (!IS_DARTIUM) { | ||
it("should support undefined", () => { | ||
expect(pipe.supports(canHasUndefined)).toBe(true); | ||
}); | ||
} | ||
|
||
}); | ||
|
||
describe("transform", () => { | ||
it("should return JSON-formatted string", () => { | ||
expect(pipe.transform(inceptionObj)).toEqual(inceptionObjString); | ||
}); | ||
|
||
it("should return JSON-formatted string even when normalized", () => { | ||
var dream1 = normalize(pipe.transform(inceptionObj)); | ||
var dream2 = normalize(inceptionObjString); | ||
expect(dream1).toEqual(dream2); | ||
}); | ||
|
||
it("should return JSON-formatted string similar to Json.stringify", () => { | ||
var dream1 = normalize(pipe.transform(inceptionObj)); | ||
var dream2 = normalize(Json.stringify(inceptionObj)); | ||
expect(dream1).toEqual(dream2); | ||
}); | ||
|
||
it("should return same value when nothing has changed since the last call", () => { | ||
expect(pipe.transform(inceptionObj)).toEqual(inceptionObjString); | ||
expect(pipe.transform(inceptionObj)).toEqual(inceptionObjString); | ||
}); | ||
|
||
}); | ||
|
||
describe("onDestroy", () => { | ||
it("should do nothing when no latest value", () => { | ||
expect(() => pipe.onDestroy()).not.toThrow(); | ||
}); | ||
}); | ||
|
||
}); | ||
} |
9860382
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PatrickJS damn, I was going to submit the exact same PR (except I missed the whole Dart thing :)
As a side note, you have an unused import
isBlank
injson_pipe.ts
and I was a bit stricter on the types (latestValue could be a string instead of any ?). Nice job!