From 7e5d7bab97e12f791f56394c6ecd2269055ddad4 Mon Sep 17 00:00:00 2001 From: Alexander Date: Mon, 15 Feb 2016 16:12:19 +0300 Subject: [PATCH] trim and empty --- source/com/kemsky/filters/empty.as | 19 +++++++++++++++++ source/com/kemsky/filters/trim.as | 25 +++++++++++++++++++++++ testSrc/com/kemsky/filters/TestFilters.as | 21 +++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 source/com/kemsky/filters/empty.as create mode 100644 source/com/kemsky/filters/trim.as diff --git a/source/com/kemsky/filters/empty.as b/source/com/kemsky/filters/empty.as new file mode 100644 index 0000000..084e1fa --- /dev/null +++ b/source/com/kemsky/filters/empty.as @@ -0,0 +1,19 @@ +/* + * Copyright: (c) 2016. Turtsevich Alexander + * + * Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.html + */ + +package com.kemsky.filters +{ + import com.kemsky.support.toValue; + + public function empty(val:*):Function + { + return function (item:*):Boolean + { + var value:* = toValue(item, val); + return (value as String) == null || (value as String).length == 0; + }; + } +} diff --git a/source/com/kemsky/filters/trim.as b/source/com/kemsky/filters/trim.as new file mode 100644 index 0000000..7919cfb --- /dev/null +++ b/source/com/kemsky/filters/trim.as @@ -0,0 +1,25 @@ +/* + * Copyright: (c) 2016. Turtsevich Alexander + * + * Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.html + */ + +package com.kemsky.filters +{ + import com.kemsky.support.toValue; + + import mx.utils.StringUtil; + + public function trim(val:*):Function + { + return function (item:*):String + { + var value:* = toValue(item, val); + if(value is String) + { + return StringUtil.trim(value); + } + return ""; + }; + } +} diff --git a/testSrc/com/kemsky/filters/TestFilters.as b/testSrc/com/kemsky/filters/TestFilters.as index 5c80e00..7156df3 100644 --- a/testSrc/com/kemsky/filters/TestFilters.as +++ b/testSrc/com/kemsky/filters/TestFilters.as @@ -17,6 +17,27 @@ package com.kemsky.filters { } + [Test] + public function testTrim():void + { + var s:Stream = $("", null, " ", "test "); + var e:Stream = s.map(trim(_)); + assertEquals(e.length, 4); + assertEquals(e.first, ""); + assertEquals(e.second, ""); + assertEquals(e.third, ""); + assertEquals(e.fourth, "test"); + } + + [Test] + public function testEmpty():void + { + var s:Stream = $("", null, "", "test"); + var e:Stream = s.filter(not(empty(_))); + assertEquals(e.length, 1); + assertEquals(e.first, "test"); + } + [Test] public function testInvoke():void {