Skip to content
This repository has been archived by the owner on Oct 19, 2020. It is now read-only.

validArrayOfStrings handling of null and undefined values added #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
41 changes: 41 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
6 changes: 2 additions & 4 deletions www/index-android.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
6 changes: 2 additions & 4 deletions www/index-ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down