From 832d018c1b624281e47428f607b4eb6d38af9f61 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Mon, 14 Dec 2015 18:31:17 +0200 Subject: [PATCH] Fix pipes and testcases --- lib/utils/pipes.js | 22 ++++++++++++++++------ tests/unit/pipes.spec.js | 24 ++++++++++++++++++++---- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/lib/utils/pipes.js b/lib/utils/pipes.js index ae75f0a1..5e8ea343 100644 --- a/lib/utils/pipes.js +++ b/lib/utils/pipes.js @@ -1,7 +1,8 @@ 'use strict'; -import {Pipe, InvalidPipeArgumentException} from 'angular2/core'; -import {isString} from 'angular2/src/facade/lang'; +import {Pipe} from 'angular2/core'; +import {isString, stringify, isBlank} from 'angular2/src/facade/lang'; +import {BaseException} from 'angular2/src/facade/exceptions'; import {JsonPointer} from './JsonPointer'; import marked from 'marked'; @@ -15,20 +16,27 @@ marked.setOptions({ smartypants: false }); +class InvalidPipeArgumentException extends BaseException { + constructor(type, value) { + super(`Invalid argument '${value}' for pipe '${stringify(type)}'`); + } +} @Pipe({ name: 'keys' }) export class KeysPipe { - transform(obj) { - if (typeof obj !== 'object') { - throw new InvalidPipeArgumentException(ValuesPipe, obj); + transform(value) { + if (isBlank(value)) return value; + if (typeof value !== 'object') { + throw new InvalidPipeArgumentException(ValuesPipe, value); } - return Object.keys(obj); + return Object.keys(value); } } @Pipe({ name: 'values' }) export class ValuesPipe { transform(value) { + if (isBlank(value)) return value; if (typeof value !== 'object') { throw new InvalidPipeArgumentException(ValuesPipe, value); } @@ -39,6 +47,7 @@ export class ValuesPipe { @Pipe({ name: 'jsonPointerEscape' }) export class JsonPointerEscapePipe { transform(value) { + if (isBlank(value)) return value; if (!isString(value)) { throw new InvalidPipeArgumentException(JsonPointerEscapePipe, value); } @@ -49,6 +58,7 @@ export class JsonPointerEscapePipe { @Pipe({ name: 'marked' }) export class MarkedPipe { transform(value) { + if (isBlank(value)) return value; if (!isString(value)) { throw new InvalidPipeArgumentException(JsonPointerEscapePipe, value); } diff --git a/tests/unit/pipes.spec.js b/tests/unit/pipes.spec.js index e0c9763c..9662892b 100644 --- a/tests/unit/pipes.spec.js +++ b/tests/unit/pipes.spec.js @@ -24,7 +24,11 @@ describe('KeysPipe and ValuesPipe', () => { it('should not support other objects', () => { (() => keysPipe.transform(45)).should.throw(); - (() => keysPipe.transform(null)).should.throw(); + (() => keysPipe.transform('45')).should.throw(); + }); + + it('should not throw on blank input', () => { + (() => valuesPipe.transform()).should.not.throw(); }); }); @@ -36,7 +40,11 @@ describe('KeysPipe and ValuesPipe', () => { it('should not support other objects', () => { (() => valuesPipe.transform(45)).should.throw(); - (() => valuesPipe.transform(null)).should.throw(); + (() => valuesPipe.transform('45')).should.throw(); + }); + + it('should not throw on blank input', () => { + (() => keysPipe.transform()).should.not.throw(); }); }); }); @@ -52,7 +60,7 @@ describe('JsonPointerEscapePipe', () => { pipe = new JsonPointerEscapePipe(); }); - describe('KeysPipe transform', () => { + describe('JsonPointerEscapePipe transform', () => { it('should escpae pointer', () => { var val = pipe.transform(unescaped); val.should.be.equal(escaped); @@ -62,6 +70,10 @@ describe('JsonPointerEscapePipe', () => { (() => pipe.transform(45)).should.throw(); (() => pipe.transform({})).should.throw(); }); + + it('should not throw on blank input', () => { + (() => pipe.transform()).should.not.throw(); + }); }); }); @@ -76,7 +88,7 @@ describe('MarkedPipe', () => { pipe = new MarkedPipe(); }); - describe('KeysPipe transform', () => { + describe('MarkedPipe transform', () => { it('should escpae pointer', () => { var val = pipe.transform(unmarked); val.should.be.equal(marked); @@ -86,5 +98,9 @@ describe('MarkedPipe', () => { (() => pipe.transform(45)).should.throw(); (() => pipe.transform({})).should.throw(); }); + + it('should not throw on blank input', () => { + (() => pipe.transform()).should.not.throw(); + }); }); });