feat: allow using KeyValueChanges as a pipe

This commit is contained in:
Pawel Kozlowski 2015-02-24 17:39:10 +01:00 committed by vsavkin
parent 33b503720a
commit 4a5d53c549
2 changed files with 29 additions and 9 deletions

View File

@ -1,8 +1,19 @@
import {ListWrapper, MapWrapper, StringMapWrapper} from 'angular2/src/facade/collection'; import {ListWrapper, MapWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
import {stringify, looseIdentical, isJsObject} from 'angular2/src/facade/lang'; import {stringify, looseIdentical, isJsObject} from 'angular2/src/facade/lang';
export class KeyValueChanges { import {NO_CHANGE, Pipe} from './pipe';
export class KeyValueChangesFactory {
supports(obj):boolean {
return KeyValueChanges.supportsObj(obj);
}
create():Pipe {
return new KeyValueChanges();
}
}
export class KeyValueChanges extends Pipe {
_records:Map; _records:Map;
_mapHead:KVChangeRecord; _mapHead:KVChangeRecord;
@ -15,6 +26,7 @@ export class KeyValueChanges {
_removalsTail:KVChangeRecord; _removalsTail:KVChangeRecord;
constructor() { constructor() {
super();
this._records = MapWrapper.create(); this._records = MapWrapper.create();
this._mapHead = null; this._mapHead = null;
this._previousMapHead = null; this._previousMapHead = null;
@ -26,12 +38,20 @@ export class KeyValueChanges {
this._removalsTail = null; this._removalsTail = null;
} }
static supports(obj):boolean { static supportsObj(obj):boolean {
return obj instanceof Map || isJsObject(obj); return obj instanceof Map || isJsObject(obj);
} }
supportsObj(obj):boolean { supports(obj):boolean {
return KeyValueChanges.supports(obj); return KeyValueChanges.supportsObj(obj);
}
transform(map){
if (this.check(map)) {
return this;
} else {
return NO_CHANGE;
}
} }
get isDirty():boolean { get isDirty():boolean {

View File

@ -131,10 +131,10 @@ export function main() {
if (isJsObject({})) { if (isJsObject({})) {
describe('JsObject changes', () => { describe('JsObject changes', () => {
it('should support JS Object', () => { it('should support JS Object', () => {
expect(KeyValueChanges.supports({})).toBeTruthy(); expect(KeyValueChanges.supportsObj({})).toBeTruthy();
expect(KeyValueChanges.supports("not supported")).toBeFalsy(); expect(KeyValueChanges.supportsObj("not supported")).toBeFalsy();
expect(KeyValueChanges.supports(0)).toBeFalsy(); expect(KeyValueChanges.supportsObj(0)).toBeFalsy();
expect(KeyValueChanges.supports(null)).toBeFalsy(); expect(KeyValueChanges.supportsObj(null)).toBeFalsy();
}); });
it('should do basic object watching', () => { it('should do basic object watching', () => {