From 46e523b9a7ac80a0add23ef8b5ff22f88975668d Mon Sep 17 00:00:00 2001 From: Peter Moresi Date: Wed, 3 Dec 2014 16:49:30 -0800 Subject: [PATCH 1/2] Fixing SEARCH function to return error.value when text is not found --- lib/text.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/text.js b/lib/text.js index 1225ea04..6c1fe90d 100644 --- a/lib/text.js +++ b/lib/text.js @@ -235,11 +235,13 @@ exports.RIGHT = function(text, number) { }; exports.SEARCH = function(find_text, within_text, position) { + var foundAt; if (typeof find_text !== 'string' || typeof within_text !== 'string') { return error.value; } position = (position === undefined) ? 0 : position; - return within_text.toLowerCase().indexOf(find_text.toLowerCase(), position - 1) + 1; + foundAt = within_text.toLowerCase().indexOf(find_text.toLowerCase(), position - 1); + return (foundAt === -1)?error.value:foundAt; }; exports.SPLIT = function (text, separator) { From 3bac730aaebe9f7ca6e5765b963aee20ddc6ba01 Mon Sep 17 00:00:00 2001 From: Peter Moresi Date: Wed, 3 Dec 2014 19:50:29 -0800 Subject: [PATCH 2/2] Fix SEARCH including test cases --- lib/text.js | 4 ++-- test/text.js | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/text.js b/lib/text.js index 6c1fe90d..646b9243 100644 --- a/lib/text.js +++ b/lib/text.js @@ -240,8 +240,8 @@ exports.SEARCH = function(find_text, within_text, position) { return error.value; } position = (position === undefined) ? 0 : position; - foundAt = within_text.toLowerCase().indexOf(find_text.toLowerCase(), position - 1); - return (foundAt === -1)?error.value:foundAt; + foundAt = within_text.toLowerCase().indexOf(find_text.toLowerCase(), position - 1)+1; + return (foundAt === 0)?error.value:foundAt; }; exports.SPLIT = function (text, separator) { diff --git a/test/text.js b/test/text.js index 45f11037..570e8a3e 100644 --- a/test/text.js +++ b/test/text.js @@ -160,6 +160,8 @@ suite('Text', function() { text.SEARCH('e', 'Statements', 6).should.equal(7); text.SEARCH('margin', 'Profit Margin').should.equal(8); text.SEARCH(true, 'bool').should.equal(error.value); + text.SEARCH("foo", "bar").should.equal(error.value); + text.SEARCH("ba", "bar").should.equal(1); }); test('SPLIT', function() { @@ -219,4 +221,4 @@ suite('Text', function() { text.VALUE('16:48:00').should.equal(60480); text.VALUE(true).should.equal(error.value); }); -}); \ No newline at end of file +});