From 6958ff597a0a42e9e0b0e0979f5cface638adec5 Mon Sep 17 00:00:00 2001 From: Malte Legenhausen Date: Thu, 29 Dec 2016 11:06:29 +0100 Subject: [PATCH] validArrayOfStrings handling of null and undefined values added --- src/js/utils.js | 4 ++-- test/utils.js | 41 +++++++++++++++++++++++++++++++++++++++++ www/index-android.js | 6 ++---- www/index-ios.js | 6 ++---- 4 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 test/utils.js diff --git a/src/js/utils.js b/src/js/utils.js index 6e00094..886b596 100644 --- a/src/js/utils.js +++ b/src/js/utils.js @@ -18,9 +18,9 @@ utils.errors = { }; utils.validArrayOfStrings = (val) => { - return (val && Array.isArray(val) && val.length > 0 && !val.find(i => !i.length || typeof i !== 'string')); + return (Array.isArray(val) && val.length > 0 && val.every(utils.validString)); }; utils.validString = (val) => { - return (val && val.length && typeof val === 'string'); + return (typeof val === 'string' && val.length > 0); }; diff --git a/test/utils.js b/test/utils.js new file mode 100644 index 0000000..697f31c --- /dev/null +++ b/test/utils.js @@ -0,0 +1,41 @@ +import { utils } from '../www/index-ios'; +import assert from 'assert'; + +describe('utils', () => { + describe('validArrayOfStrings', () => { + it('should return true on a valid non-empty string array', () => { + assert(utils.validArrayOfStrings(['product1']) === true); + assert(utils.validArrayOfStrings(['product1', 'product2']) === true); + }); + + it('should return false on a invalid non-empty string array', () => { + assert(utils.validArrayOfStrings([]) === false); + assert(utils.validArrayOfStrings(['']) === false); + assert(utils.validArrayOfStrings([null]) === false); + assert(utils.validArrayOfStrings([undefined]) === false); + assert(utils.validArrayOfStrings(['product1', null]) === false); + assert(utils.validArrayOfStrings(['product1', '']) === false); + assert(utils.validArrayOfStrings(null) === false); + assert(utils.validArrayOfStrings() === false); + assert(utils.validArrayOfStrings(false) === false); + assert(utils.validArrayOfStrings('product1') === false); + assert(utils.validArrayOfStrings(123) === false); + }); + }); + + describe('validString', () => { + it('should return true on a valid non-empty string', () => { + assert(utils.validString('abc') === true); + }); + + it('should return false on anyting else', () => { + assert(utils.validString() === false); + assert(utils.validString(null) === false); + assert(utils.validString('') === false); + assert(utils.validString(123) === false); + assert(utils.validString(true) === false); + assert(utils.validString([]) === false); + assert(utils.validString({}) === false); + }); + }); +}); diff --git a/www/index-android.js b/www/index-android.js index 4f4ade7..ac92fec 100644 --- a/www/index-android.js +++ b/www/index-android.js @@ -58,13 +58,11 @@ utils.errors = { }; utils.validArrayOfStrings = function (val) { - return val && Array.isArray(val) && val.length > 0 && !val.find(function (i) { - return !i.length || typeof i !== 'string'; - }); + return Array.isArray(val) && val.length > 0 && val.every(utils.validString); }; utils.validString = function (val) { - return val && val.length && typeof val === 'string'; + return typeof val === 'string' && val.length > 0; }; 'use strict'; diff --git a/www/index-ios.js b/www/index-ios.js index 7eb5566..1cc2821 100644 --- a/www/index-ios.js +++ b/www/index-ios.js @@ -20,13 +20,11 @@ utils.errors = { }; utils.validArrayOfStrings = function (val) { - return val && Array.isArray(val) && val.length > 0 && !val.find(function (i) { - return !i.length || typeof i !== 'string'; - }); + return Array.isArray(val) && val.length > 0 && val.every(utils.validString); }; utils.validString = function (val) { - return val && val.length && typeof val === 'string'; + return typeof val === 'string' && val.length > 0; }; 'use strict';